LLVM: lib/Bitstream/Reader/BitstreamReader.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
11#include
12#include
13#include
14
15using namespace llvm;
16
17
18
19
20
24
25
27
28 BlockScope.push_back(Block(CurCodeSize));
29 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
30
31
32 if (BlockInfo) {
34 BlockInfo->getBlockInfo(BlockID)) {
36 }
37 }
38
39
41 if (!MaybeVBR)
43 CurCodeSize = MaybeVBR.get();
44
47 std::errc::illegal_byte_sequence,
48 "can't read more than %zu at a time, trying to read %u", +MaxChunkSize,
49 CurCodeSize);
50
53 if (!MaybeNum)
56 if (NumWordsP)
57 *NumWordsP = NumWords;
58
59 if (CurCodeSize == 0)
61 std::errc::illegal_byte_sequence,
62 "can't enter sub-block: current code size is 0");
65 std::errc::illegal_byte_sequence,
66 "can't enter sub block: already at end of stream");
67
69}
70
73 assert(.isLiteral() && "Not to be used with literals!");
74
75
76 switch (Op.getEncoding()) {
81 assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
82 return Cursor.Read((unsigned)Op.getEncodingData());
84 assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
85 return Cursor.ReadVBR64((unsigned)Op.getEncodingData());
89 else
90 return Res.takeError();
91 }
93}
94
95
97
100 if (!MaybeCode)
102 unsigned Code = MaybeCode.get();
104 if (!MaybeVBR)
106 unsigned NumElts = MaybeVBR.get();
107 for (unsigned i = 0; i != NumElts; ++i)
109 ;
110 else
111 return Res.takeError();
112 return Code;
113 }
114
116 if (!MaybeAbbv)
118
121 unsigned Code;
124 else {
128 std::errc::illegal_byte_sequence,
129 "Abbreviation starts with an Array or a Blob");
131 if (!MaybeCode)
133 Code = MaybeCode.get();
134 }
135
138 if (Op.isLiteral())
139 continue;
140
144 continue;
145 else
146 return MaybeField.takeError();
147 }
148
150
152 if (!MaybeNum)
154 unsigned NumElts = MaybeNum.get();
155
156
157 assert(i+2 == e && "array op not second to last?");
159
160
161
163 default:
164 return error("Array element type can't be an Array or a Blob");
170 return Err;
171 break;
174 for (; NumElts; --NumElts)
177 ;
178 else
179 return Res.takeError();
180 break;
183 return Err;
184 break;
185 }
186 continue;
187 }
188
190
192 if (!MaybeNum)
194 unsigned NumElts = MaybeNum.get();
196
197
199
200
201
204 break;
205 }
206
207
209 return Err;
210 }
211 return Code;
212}
213
219 if (!MaybeCode)
223 if (!MaybeNumElts)
226 .c_str());
229 return error("Size is not plausible");
231
232 for (unsigned i = 0; i != NumElts; ++i)
235 else
236 return MaybeVal.takeError();
237 return Code;
238 }
239
241 if (!MaybeAbbv)
244
245
248 unsigned Code;
251 else {
254 return error("Abbreviation starts with an Array or a Blob");
256 Code = MaybeCode.get();
257 else
258 return MaybeCode.takeError();
259 }
260
263 if (Op.isLiteral()) {
265 continue;
266 }
267
272 else
273 return MaybeVal.takeError();
274 continue;
275 }
276
278
280 if (!MaybeNumElts)
283 .c_str());
286 return error("Size is not plausible");
288
289
290 if (i + 2 != e)
291 return error("Array op not second to last");
295 "Array element type has to be an encoding of a type");
296
297
299 default:
300 return error("Array element type can't be an Array or a Blob");
302 for (; NumElts; --NumElts)
306 else
307 return MaybeVal.takeError();
308 break;
310 for (; NumElts; --NumElts)
314 else
315 return MaybeVal.takeError();
316 break;
318 for (; NumElts; --NumElts)
321 else
322 return MaybeVal.takeError();
323 }
324 continue;
325 }
326
328
330 if (!MaybeNumElts)
334
335
337 const size_t NewEnd =
338 CurBitPos + static_cast<uint64_t>(alignTo(NumElts, 4)) * 8;
339
340
342 return error("Blob ends too soon");
343
344
345
346
348 return Err;
349 const char *Ptr = (const char *)getPointerToBit(CurBitPos, NumElts);
350
351
352 if (Blob) {
354 } else {
355
356 auto *UPtr = reinterpret_cast<const unsigned char *>(Ptr);
357 Vals.append(UPtr, UPtr + NumElts);
358 }
359 }
360
361 return Code;
362}
363
365 auto Abbv = std::make_shared();
367 if (!MaybeNumOpInfo)
368 return MaybeNumOpInfo.takeError();
369 unsigned NumOpInfo = MaybeNumOpInfo.get();
370 for (unsigned i = 0; i != NumOpInfo; ++i) {
372 if (!MaybeIsLiteral)
373 return MaybeIsLiteral.takeError();
374 bool IsLiteral = MaybeIsLiteral.get();
375 if (IsLiteral) {
377 if (!MaybeOp)
380 continue;
381 }
382
384 if (!MaybeEncoding)
385 return MaybeEncoding.takeError();
387 return error("Invalid encoding");
388
393 if (!MaybeData)
396
397
398
399
403 continue;
404 }
405
408 return error("Fixed or VBR abbrev record with size > MaxChunkData");
409
411 } else
413 }
414
415 if (Abbv->getNumOperandInfos() == 0)
416 return error("Abbrev record with no operands");
417 CurAbbrevs.push_back(std::move(Abbv));
418
420}
421
425 return Err;
426
428
431
432
433 while (true) {
436 if (!MaybeEntry)
439
440 switch (Entry.Kind) {
443 return std::nullopt;
445 return std::move(NewBlockInfo);
447
448 break;
449 }
450
451
453 if (!CurBlockInfo)
454 return std::nullopt;
456 return Err;
457
458
459
460 CurBlockInfo->Abbrevs.push_back(std::move(CurAbbrevs.back()));
461 CurAbbrevs.pop_back();
462 continue;
463 }
464
465
468 if (!MaybeBlockInfo)
469 return MaybeBlockInfo.takeError();
470 switch (MaybeBlockInfo.get()) {
471 default:
472 break;
474 if (Record.size() < 1)
475 return std::nullopt;
477 break;
479 if (!CurBlockInfo)
480 return std::nullopt;
481 if (!ReadBlockInfoNames)
482 break;
483 CurBlockInfo->Name = std::string(Record.begin(), Record.end());
484 break;
485 }
487 if (!CurBlockInfo)
488 return std::nullopt;
489 if (!ReadBlockInfoNames)
490 break;
493 break;
494 }
495 }
496 }
497}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static Expected< uint64_t > readAbbreviatedField(BitstreamCursor &Cursor, const BitCodeAbbrevOp &Op)
Definition BitstreamReader.cpp:71
BitCodeAbbrevOp - This describes one or more operands in an abbreviation.
uint64_t getLiteralValue() const
static bool isValidEncoding(uint64_t E)
bool hasEncodingData() const
Encoding getEncoding() const
static char DecodeChar6(unsigned V)
uint64_t getEncodingData() const
BitCodeAbbrev - This class represents an abbreviation record.
unsigned getNumOperandInfos() const
const BitCodeAbbrevOp & getOperandInfo(unsigned N) const
This class maintains the abbreviations read from a block info block.
BlockInfo & getOrCreateBlockInfo(unsigned BlockID)
This represents a position within a bitcode file, implemented on top of a SimpleBitstreamCursor.
Error JumpToBit(uint64_t BitNo)
Reset the stream to the specified bit number.
LLVM_ABI Error ReadAbbrevRecord()
Definition BitstreamReader.cpp:364
uint64_t GetCurrentBitNo() const
Return the bit # of the bit we are reading.
Expected< uint64_t > ReadVBR64(const unsigned NumBits)
static const size_t MaxChunkSize
Expected< BitstreamEntry > advanceSkippingSubblocks(unsigned Flags=0)
This is a convenience function for clients that don't expect any subblocks.
Expected< const BitCodeAbbrev * > getAbbrev(unsigned AbbrevID)
Return the abbreviation for the specified AbbrevId.
LLVM_ABI Expected< unsigned > readRecord(unsigned AbbrevID, SmallVectorImpl< uint64_t > &Vals, StringRef *Blob=nullptr)
Definition BitstreamReader.cpp:214
LLVM_ABI Error EnterSubBlock(unsigned BlockID, unsigned *NumWordsP=nullptr)
Having read the ENTER_SUBBLOCK abbrevid, and enter the block.
Definition BitstreamReader.cpp:26
LLVM_ABI Expected< unsigned > skipRecord(unsigned AbbrevID)
Read the current record and discard it, returning the code for the record.
Definition BitstreamReader.cpp:96
@ AF_DontAutoprocessAbbrevs
If this flag is used, abbrev entries are returned just like normal records.
void skipToEnd()
Skip to the end of the file.
LLVM_ABI Expected< std::optional< BitstreamBlockInfo > > ReadBlockInfoBlock(bool ReadBlockInfoNames=false)
Read and return a block info block from the bitstream.
Definition BitstreamReader.cpp:423
Expected< uint32_t > ReadVBR(const unsigned NumBits)
bool canSkipToPos(size_t pos) const
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.
const uint8_t * getPointerToBit(uint64_t BitNo, uint64_t NumBytes)
Get a pointer into the bitstream at the specified bit offset.
size_t word_t
This is the current data we have pulled from the stream but have not returned to the client.
void SkipToFourByteBoundary()
bool isSizePlausible(size_t Size) const
Check whether a reservation of Size elements is plausible.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void reserve(size_type N)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
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 llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ BLOCKINFO_CODE_BLOCKNAME
@ BLOCKINFO_CODE_SETRECORDNAME
@ BLOCKINFO_BLOCK_ID
BLOCKINFO_BLOCK is used to define metadata about blocks, for example, standard abbrevs that should be...
@ DEFINE_ABBREV
DEFINE_ABBREV - Defines an abbrev for the current block.
This is an optimization pass for GlobalISel generic memory operations.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
FunctionAddr VTableAddr uintptr_t uintptr_t Data
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
DWARFExpression::Operation Op
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
This contains information emitted to BLOCKINFO_BLOCK blocks.
std::vector< std::pair< unsigned, std::string > > RecordNames
std::vector< std::shared_ptr< BitCodeAbbrev > > Abbrevs
When advancing through a bitstream cursor, each advance can discover a few different kinds of entries...