File: | build/source/llvm/include/llvm/Bitstream/BitstreamReader.h |
Warning: | line 217, column 39 The result of the right shift is undefined due to shifting by '64', which is greater or equal to the width of type 'word_t' |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===- BitstreamReader.cpp - BitstreamReader implementation ---------------===// | |||
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 | #include "llvm/Bitstream/BitstreamReader.h" | |||
10 | #include "llvm/ADT/StringRef.h" | |||
11 | #include <cassert> | |||
12 | #include <optional> | |||
13 | #include <string> | |||
14 | ||||
15 | using namespace llvm; | |||
16 | ||||
17 | //===----------------------------------------------------------------------===// | |||
18 | // BitstreamCursor implementation | |||
19 | //===----------------------------------------------------------------------===// | |||
20 | // | |||
21 | static Error error(const char *Message) { | |||
22 | return createStringError(std::errc::illegal_byte_sequence, Message); | |||
23 | } | |||
24 | ||||
25 | /// Having read the ENTER_SUBBLOCK abbrevid, enter the block. | |||
26 | Error BitstreamCursor::EnterSubBlock(unsigned BlockID, unsigned *NumWordsP) { | |||
27 | // Save the current block's state on BlockScope. | |||
28 | BlockScope.push_back(Block(CurCodeSize)); | |||
29 | BlockScope.back().PrevAbbrevs.swap(CurAbbrevs); | |||
30 | ||||
31 | // Add the abbrevs specific to this block to the CurAbbrevs list. | |||
32 | if (BlockInfo) { | |||
33 | if (const BitstreamBlockInfo::BlockInfo *Info = | |||
34 | BlockInfo->getBlockInfo(BlockID)) { | |||
35 | llvm::append_range(CurAbbrevs, Info->Abbrevs); | |||
36 | } | |||
37 | } | |||
38 | ||||
39 | // Get the codesize of this block. | |||
40 | Expected<uint32_t> MaybeVBR = ReadVBR(bitc::CodeLenWidth); | |||
41 | if (!MaybeVBR) | |||
42 | return MaybeVBR.takeError(); | |||
43 | CurCodeSize = MaybeVBR.get(); | |||
44 | ||||
45 | if (CurCodeSize > MaxChunkSize) | |||
46 | return llvm::createStringError( | |||
47 | std::errc::illegal_byte_sequence, | |||
48 | "can't read more than %zu at a time, trying to read %u", +MaxChunkSize, | |||
49 | CurCodeSize); | |||
50 | ||||
51 | SkipToFourByteBoundary(); | |||
52 | Expected<word_t> MaybeNum = Read(bitc::BlockSizeWidth); | |||
53 | if (!MaybeNum) | |||
54 | return MaybeNum.takeError(); | |||
55 | word_t NumWords = MaybeNum.get(); | |||
56 | if (NumWordsP) | |||
57 | *NumWordsP = NumWords; | |||
58 | ||||
59 | if (CurCodeSize == 0) | |||
60 | return llvm::createStringError( | |||
61 | std::errc::illegal_byte_sequence, | |||
62 | "can't enter sub-block: current code size is 0"); | |||
63 | if (AtEndOfStream()) | |||
64 | return llvm::createStringError( | |||
65 | std::errc::illegal_byte_sequence, | |||
66 | "can't enter sub block: already at end of stream"); | |||
67 | ||||
68 | return Error::success(); | |||
69 | } | |||
70 | ||||
71 | static Expected<uint64_t> readAbbreviatedField(BitstreamCursor &Cursor, | |||
72 | const BitCodeAbbrevOp &Op) { | |||
73 | assert(!Op.isLiteral() && "Not to be used with literals!")(static_cast <bool> (!Op.isLiteral() && "Not to be used with literals!" ) ? void (0) : __assert_fail ("!Op.isLiteral() && \"Not to be used with literals!\"" , "llvm/lib/Bitstream/Reader/BitstreamReader.cpp", 73, __extension__ __PRETTY_FUNCTION__)); | |||
74 | ||||
75 | // Decode the value as we are commanded. | |||
76 | switch (Op.getEncoding()) { | |||
77 | case BitCodeAbbrevOp::Array: | |||
78 | case BitCodeAbbrevOp::Blob: | |||
79 | llvm_unreachable("Should not reach here")::llvm::llvm_unreachable_internal("Should not reach here", "llvm/lib/Bitstream/Reader/BitstreamReader.cpp" , 79); | |||
80 | case BitCodeAbbrevOp::Fixed: | |||
81 | assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize)(static_cast <bool> ((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize) ? void (0) : __assert_fail ("(unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize" , "llvm/lib/Bitstream/Reader/BitstreamReader.cpp", 81, __extension__ __PRETTY_FUNCTION__)); | |||
82 | return Cursor.Read((unsigned)Op.getEncodingData()); | |||
83 | case BitCodeAbbrevOp::VBR: | |||
84 | assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize)(static_cast <bool> ((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize) ? void (0) : __assert_fail ("(unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize" , "llvm/lib/Bitstream/Reader/BitstreamReader.cpp", 84, __extension__ __PRETTY_FUNCTION__)); | |||
85 | return Cursor.ReadVBR64((unsigned)Op.getEncodingData()); | |||
86 | case BitCodeAbbrevOp::Char6: | |||
87 | if (Expected<unsigned> Res = Cursor.Read(6)) | |||
88 | return BitCodeAbbrevOp::DecodeChar6(Res.get()); | |||
89 | else | |||
90 | return Res.takeError(); | |||
91 | } | |||
92 | llvm_unreachable("invalid abbreviation encoding")::llvm::llvm_unreachable_internal("invalid abbreviation encoding" , "llvm/lib/Bitstream/Reader/BitstreamReader.cpp", 92); | |||
93 | } | |||
94 | ||||
95 | /// skipRecord - Read the current record and discard it. | |||
96 | Expected<unsigned> BitstreamCursor::skipRecord(unsigned AbbrevID) { | |||
97 | // Skip unabbreviated records by reading past their entries. | |||
98 | if (AbbrevID == bitc::UNABBREV_RECORD) { | |||
99 | Expected<uint32_t> MaybeCode = ReadVBR(6); | |||
100 | if (!MaybeCode) | |||
101 | return MaybeCode.takeError(); | |||
102 | unsigned Code = MaybeCode.get(); | |||
103 | Expected<uint32_t> MaybeVBR = ReadVBR(6); | |||
104 | if (!MaybeVBR) | |||
105 | return MaybeVBR.takeError(); | |||
106 | unsigned NumElts = MaybeVBR.get(); | |||
107 | for (unsigned i = 0; i != NumElts; ++i) | |||
108 | if (Expected<uint64_t> Res = ReadVBR64(6)) | |||
109 | ; // Skip! | |||
110 | else | |||
111 | return Res.takeError(); | |||
112 | return Code; | |||
113 | } | |||
114 | ||||
115 | Expected<const BitCodeAbbrev *> MaybeAbbv = getAbbrev(AbbrevID); | |||
116 | if (!MaybeAbbv) | |||
117 | return MaybeAbbv.takeError(); | |||
118 | ||||
119 | const BitCodeAbbrev *Abbv = MaybeAbbv.get(); | |||
120 | const BitCodeAbbrevOp &CodeOp = Abbv->getOperandInfo(0); | |||
121 | unsigned Code; | |||
122 | if (CodeOp.isLiteral()) | |||
123 | Code = CodeOp.getLiteralValue(); | |||
124 | else { | |||
125 | if (CodeOp.getEncoding() == BitCodeAbbrevOp::Array || | |||
126 | CodeOp.getEncoding() == BitCodeAbbrevOp::Blob) | |||
127 | return llvm::createStringError( | |||
128 | std::errc::illegal_byte_sequence, | |||
129 | "Abbreviation starts with an Array or a Blob"); | |||
130 | Expected<uint64_t> MaybeCode = readAbbreviatedField(*this, CodeOp); | |||
131 | if (!MaybeCode) | |||
132 | return MaybeCode.takeError(); | |||
133 | Code = MaybeCode.get(); | |||
134 | } | |||
135 | ||||
136 | for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i < e; ++i) { | |||
137 | const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i); | |||
138 | if (Op.isLiteral()) | |||
139 | continue; | |||
140 | ||||
141 | if (Op.getEncoding() != BitCodeAbbrevOp::Array && | |||
142 | Op.getEncoding() != BitCodeAbbrevOp::Blob) { | |||
143 | if (Expected<uint64_t> MaybeField = readAbbreviatedField(*this, Op)) | |||
144 | continue; | |||
145 | else | |||
146 | return MaybeField.takeError(); | |||
147 | } | |||
148 | ||||
149 | if (Op.getEncoding() == BitCodeAbbrevOp::Array) { | |||
150 | // Array case. Read the number of elements as a vbr6. | |||
151 | Expected<uint32_t> MaybeNum = ReadVBR(6); | |||
152 | if (!MaybeNum) | |||
153 | return MaybeNum.takeError(); | |||
154 | unsigned NumElts = MaybeNum.get(); | |||
155 | ||||
156 | // Get the element encoding. | |||
157 | assert(i+2 == e && "array op not second to last?")(static_cast <bool> (i+2 == e && "array op not second to last?" ) ? void (0) : __assert_fail ("i+2 == e && \"array op not second to last?\"" , "llvm/lib/Bitstream/Reader/BitstreamReader.cpp", 157, __extension__ __PRETTY_FUNCTION__)); | |||
158 | const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i); | |||
159 | ||||
160 | // Read all the elements. | |||
161 | // Decode the value as we are commanded. | |||
162 | switch (EltEnc.getEncoding()) { | |||
163 | default: | |||
164 | return error("Array element type can't be an Array or a Blob"); | |||
165 | case BitCodeAbbrevOp::Fixed: | |||
166 | assert((unsigned)EltEnc.getEncodingData() <= MaxChunkSize)(static_cast <bool> ((unsigned)EltEnc.getEncodingData() <= MaxChunkSize) ? void (0) : __assert_fail ("(unsigned)EltEnc.getEncodingData() <= MaxChunkSize" , "llvm/lib/Bitstream/Reader/BitstreamReader.cpp", 166, __extension__ __PRETTY_FUNCTION__)); | |||
167 | if (Error Err = | |||
168 | JumpToBit(GetCurrentBitNo() + static_cast<uint64_t>(NumElts) * | |||
169 | EltEnc.getEncodingData())) | |||
170 | return std::move(Err); | |||
171 | break; | |||
172 | case BitCodeAbbrevOp::VBR: | |||
173 | assert((unsigned)EltEnc.getEncodingData() <= MaxChunkSize)(static_cast <bool> ((unsigned)EltEnc.getEncodingData() <= MaxChunkSize) ? void (0) : __assert_fail ("(unsigned)EltEnc.getEncodingData() <= MaxChunkSize" , "llvm/lib/Bitstream/Reader/BitstreamReader.cpp", 173, __extension__ __PRETTY_FUNCTION__)); | |||
174 | for (; NumElts; --NumElts) | |||
175 | if (Expected<uint64_t> Res = | |||
176 | ReadVBR64((unsigned)EltEnc.getEncodingData())) | |||
177 | ; // Skip! | |||
178 | else | |||
179 | return Res.takeError(); | |||
180 | break; | |||
181 | case BitCodeAbbrevOp::Char6: | |||
182 | if (Error Err = JumpToBit(GetCurrentBitNo() + NumElts * 6)) | |||
183 | return std::move(Err); | |||
184 | break; | |||
185 | } | |||
186 | continue; | |||
187 | } | |||
188 | ||||
189 | assert(Op.getEncoding() == BitCodeAbbrevOp::Blob)(static_cast <bool> (Op.getEncoding() == BitCodeAbbrevOp ::Blob) ? void (0) : __assert_fail ("Op.getEncoding() == BitCodeAbbrevOp::Blob" , "llvm/lib/Bitstream/Reader/BitstreamReader.cpp", 189, __extension__ __PRETTY_FUNCTION__)); | |||
190 | // Blob case. Read the number of bytes as a vbr6. | |||
191 | Expected<uint32_t> MaybeNum = ReadVBR(6); | |||
192 | if (!MaybeNum) | |||
193 | return MaybeNum.takeError(); | |||
194 | unsigned NumElts = MaybeNum.get(); | |||
195 | SkipToFourByteBoundary(); // 32-bit alignment | |||
196 | ||||
197 | // Figure out where the end of this blob will be including tail padding. | |||
198 | const size_t NewEnd = GetCurrentBitNo() + alignTo(NumElts, 4) * 8; | |||
199 | ||||
200 | // If this would read off the end of the bitcode file, just set the | |||
201 | // record to empty and return. | |||
202 | if (!canSkipToPos(NewEnd/8)) { | |||
203 | skipToEnd(); | |||
204 | break; | |||
205 | } | |||
206 | ||||
207 | // Skip over the blob. | |||
208 | if (Error Err = JumpToBit(NewEnd)) | |||
209 | return std::move(Err); | |||
210 | } | |||
211 | return Code; | |||
212 | } | |||
213 | ||||
214 | Expected<unsigned> BitstreamCursor::readRecord(unsigned AbbrevID, | |||
215 | SmallVectorImpl<uint64_t> &Vals, | |||
216 | StringRef *Blob) { | |||
217 | if (AbbrevID == bitc::UNABBREV_RECORD) { | |||
218 | Expected<uint32_t> MaybeCode = ReadVBR(6); | |||
219 | if (!MaybeCode) | |||
220 | return MaybeCode.takeError(); | |||
221 | uint32_t Code = MaybeCode.get(); | |||
222 | Expected<uint32_t> MaybeNumElts = ReadVBR(6); | |||
223 | if (!MaybeNumElts) | |||
224 | return error( | |||
225 | ("Failed to read size: " + toString(MaybeNumElts.takeError())) | |||
226 | .c_str()); | |||
227 | uint32_t NumElts = MaybeNumElts.get(); | |||
228 | if (!isSizePlausible(NumElts)) | |||
229 | return error("Size is not plausible"); | |||
230 | Vals.reserve(Vals.size() + NumElts); | |||
231 | ||||
232 | for (unsigned i = 0; i != NumElts; ++i) | |||
233 | if (Expected<uint64_t> MaybeVal = ReadVBR64(6)) | |||
234 | Vals.push_back(MaybeVal.get()); | |||
235 | else | |||
236 | return MaybeVal.takeError(); | |||
237 | return Code; | |||
238 | } | |||
239 | ||||
240 | Expected<const BitCodeAbbrev *> MaybeAbbv = getAbbrev(AbbrevID); | |||
241 | if (!MaybeAbbv) | |||
242 | return MaybeAbbv.takeError(); | |||
243 | const BitCodeAbbrev *Abbv = MaybeAbbv.get(); | |||
244 | ||||
245 | // Read the record code first. | |||
246 | assert(Abbv->getNumOperandInfos() != 0 && "no record code in abbreviation?")(static_cast <bool> (Abbv->getNumOperandInfos() != 0 && "no record code in abbreviation?") ? void (0) : __assert_fail ("Abbv->getNumOperandInfos() != 0 && \"no record code in abbreviation?\"" , "llvm/lib/Bitstream/Reader/BitstreamReader.cpp", 246, __extension__ __PRETTY_FUNCTION__)); | |||
247 | const BitCodeAbbrevOp &CodeOp = Abbv->getOperandInfo(0); | |||
248 | unsigned Code; | |||
249 | if (CodeOp.isLiteral()) | |||
250 | Code = CodeOp.getLiteralValue(); | |||
251 | else { | |||
252 | if (CodeOp.getEncoding() == BitCodeAbbrevOp::Array || | |||
253 | CodeOp.getEncoding() == BitCodeAbbrevOp::Blob) | |||
254 | return error("Abbreviation starts with an Array or a Blob"); | |||
255 | if (Expected<uint64_t> MaybeCode = readAbbreviatedField(*this, CodeOp)) | |||
256 | Code = MaybeCode.get(); | |||
257 | else | |||
258 | return MaybeCode.takeError(); | |||
259 | } | |||
260 | ||||
261 | for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i != e; ++i) { | |||
262 | const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i); | |||
263 | if (Op.isLiteral()) { | |||
264 | Vals.push_back(Op.getLiteralValue()); | |||
265 | continue; | |||
266 | } | |||
267 | ||||
268 | if (Op.getEncoding() != BitCodeAbbrevOp::Array && | |||
269 | Op.getEncoding() != BitCodeAbbrevOp::Blob) { | |||
270 | if (Expected<uint64_t> MaybeVal = readAbbreviatedField(*this, Op)) | |||
271 | Vals.push_back(MaybeVal.get()); | |||
272 | else | |||
273 | return MaybeVal.takeError(); | |||
274 | continue; | |||
275 | } | |||
276 | ||||
277 | if (Op.getEncoding() == BitCodeAbbrevOp::Array) { | |||
278 | // Array case. Read the number of elements as a vbr6. | |||
279 | Expected<uint32_t> MaybeNumElts = ReadVBR(6); | |||
280 | if (!MaybeNumElts) | |||
281 | return error( | |||
282 | ("Failed to read size: " + toString(MaybeNumElts.takeError())) | |||
283 | .c_str()); | |||
284 | uint32_t NumElts = MaybeNumElts.get(); | |||
285 | if (!isSizePlausible(NumElts)) | |||
286 | return error("Size is not plausible"); | |||
287 | Vals.reserve(Vals.size() + NumElts); | |||
288 | ||||
289 | // Get the element encoding. | |||
290 | if (i + 2 != e) | |||
291 | return error("Array op not second to last"); | |||
292 | const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i); | |||
293 | if (!EltEnc.isEncoding()) | |||
294 | return error( | |||
295 | "Array element type has to be an encoding of a type"); | |||
296 | ||||
297 | // Read all the elements. | |||
298 | switch (EltEnc.getEncoding()) { | |||
299 | default: | |||
300 | return error("Array element type can't be an Array or a Blob"); | |||
301 | case BitCodeAbbrevOp::Fixed: | |||
302 | for (; NumElts; --NumElts) | |||
303 | if (Expected<SimpleBitstreamCursor::word_t> MaybeVal = | |||
304 | Read((unsigned)EltEnc.getEncodingData())) | |||
305 | Vals.push_back(MaybeVal.get()); | |||
306 | else | |||
307 | return MaybeVal.takeError(); | |||
308 | break; | |||
309 | case BitCodeAbbrevOp::VBR: | |||
310 | for (; NumElts; --NumElts) | |||
311 | if (Expected<uint64_t> MaybeVal = | |||
312 | ReadVBR64((unsigned)EltEnc.getEncodingData())) | |||
313 | Vals.push_back(MaybeVal.get()); | |||
314 | else | |||
315 | return MaybeVal.takeError(); | |||
316 | break; | |||
317 | case BitCodeAbbrevOp::Char6: | |||
318 | for (; NumElts; --NumElts) | |||
319 | if (Expected<SimpleBitstreamCursor::word_t> MaybeVal = Read(6)) | |||
320 | Vals.push_back(BitCodeAbbrevOp::DecodeChar6(MaybeVal.get())); | |||
321 | else | |||
322 | return MaybeVal.takeError(); | |||
323 | } | |||
324 | continue; | |||
325 | } | |||
326 | ||||
327 | assert(Op.getEncoding() == BitCodeAbbrevOp::Blob)(static_cast <bool> (Op.getEncoding() == BitCodeAbbrevOp ::Blob) ? void (0) : __assert_fail ("Op.getEncoding() == BitCodeAbbrevOp::Blob" , "llvm/lib/Bitstream/Reader/BitstreamReader.cpp", 327, __extension__ __PRETTY_FUNCTION__)); | |||
328 | // Blob case. Read the number of bytes as a vbr6. | |||
329 | Expected<uint32_t> MaybeNumElts = ReadVBR(6); | |||
330 | if (!MaybeNumElts) | |||
331 | return MaybeNumElts.takeError(); | |||
332 | uint32_t NumElts = MaybeNumElts.get(); | |||
333 | SkipToFourByteBoundary(); // 32-bit alignment | |||
334 | ||||
335 | // Figure out where the end of this blob will be including tail padding. | |||
336 | size_t CurBitPos = GetCurrentBitNo(); | |||
337 | const size_t NewEnd = CurBitPos + alignTo(NumElts, 4) * 8; | |||
338 | ||||
339 | // Make sure the bitstream is large enough to contain the blob. | |||
340 | if (!canSkipToPos(NewEnd/8)) | |||
341 | return error("Blob ends too soon"); | |||
342 | ||||
343 | // Otherwise, inform the streamer that we need these bytes in memory. Skip | |||
344 | // over tail padding first, in case jumping to NewEnd invalidates the Blob | |||
345 | // pointer. | |||
346 | if (Error Err = JumpToBit(NewEnd)) | |||
347 | return std::move(Err); | |||
348 | const char *Ptr = (const char *)getPointerToBit(CurBitPos, NumElts); | |||
349 | ||||
350 | // If we can return a reference to the data, do so to avoid copying it. | |||
351 | if (Blob) { | |||
352 | *Blob = StringRef(Ptr, NumElts); | |||
353 | } else { | |||
354 | // Otherwise, unpack into Vals with zero extension. | |||
355 | auto *UPtr = reinterpret_cast<const unsigned char *>(Ptr); | |||
356 | Vals.append(UPtr, UPtr + NumElts); | |||
357 | } | |||
358 | } | |||
359 | ||||
360 | return Code; | |||
361 | } | |||
362 | ||||
363 | Error BitstreamCursor::ReadAbbrevRecord() { | |||
364 | auto Abbv = std::make_shared<BitCodeAbbrev>(); | |||
365 | Expected<uint32_t> MaybeNumOpInfo = ReadVBR(5); | |||
366 | if (!MaybeNumOpInfo) | |||
367 | return MaybeNumOpInfo.takeError(); | |||
368 | unsigned NumOpInfo = MaybeNumOpInfo.get(); | |||
369 | for (unsigned i = 0; i != NumOpInfo; ++i) { | |||
370 | Expected<word_t> MaybeIsLiteral = Read(1); | |||
371 | if (!MaybeIsLiteral) | |||
372 | return MaybeIsLiteral.takeError(); | |||
373 | bool IsLiteral = MaybeIsLiteral.get(); | |||
374 | if (IsLiteral) { | |||
375 | Expected<uint64_t> MaybeOp = ReadVBR64(8); | |||
376 | if (!MaybeOp) | |||
377 | return MaybeOp.takeError(); | |||
378 | Abbv->Add(BitCodeAbbrevOp(MaybeOp.get())); | |||
379 | continue; | |||
380 | } | |||
381 | ||||
382 | Expected<word_t> MaybeEncoding = Read(3); | |||
383 | if (!MaybeEncoding) | |||
384 | return MaybeEncoding.takeError(); | |||
385 | if (!BitCodeAbbrevOp::isValidEncoding(MaybeEncoding.get())) | |||
386 | return error("Invalid encoding"); | |||
387 | ||||
388 | BitCodeAbbrevOp::Encoding E = | |||
389 | (BitCodeAbbrevOp::Encoding)MaybeEncoding.get(); | |||
390 | if (BitCodeAbbrevOp::hasEncodingData(E)) { | |||
391 | Expected<uint64_t> MaybeData = ReadVBR64(5); | |||
392 | if (!MaybeData) | |||
393 | return MaybeData.takeError(); | |||
394 | uint64_t Data = MaybeData.get(); | |||
395 | ||||
396 | // As a special case, handle fixed(0) (i.e., a fixed field with zero bits) | |||
397 | // and vbr(0) as a literal zero. This is decoded the same way, and avoids | |||
398 | // a slow path in Read() to have to handle reading zero bits. | |||
399 | if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) && | |||
400 | Data == 0) { | |||
401 | Abbv->Add(BitCodeAbbrevOp(0)); | |||
402 | continue; | |||
403 | } | |||
404 | ||||
405 | if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) && | |||
406 | Data > MaxChunkSize) | |||
407 | return error("Fixed or VBR abbrev record with size > MaxChunkData"); | |||
408 | ||||
409 | Abbv->Add(BitCodeAbbrevOp(E, Data)); | |||
410 | } else | |||
411 | Abbv->Add(BitCodeAbbrevOp(E)); | |||
412 | } | |||
413 | ||||
414 | if (Abbv->getNumOperandInfos() == 0) | |||
415 | return error("Abbrev record with no operands"); | |||
416 | CurAbbrevs.push_back(std::move(Abbv)); | |||
417 | ||||
418 | return Error::success(); | |||
419 | } | |||
420 | ||||
421 | Expected<std::optional<BitstreamBlockInfo>> | |||
422 | BitstreamCursor::ReadBlockInfoBlock(bool ReadBlockInfoNames) { | |||
423 | if (llvm::Error Err = EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID)) | |||
| ||||
424 | return std::move(Err); | |||
425 | ||||
426 | BitstreamBlockInfo NewBlockInfo; | |||
427 | ||||
428 | SmallVector<uint64_t, 64> Record; | |||
429 | BitstreamBlockInfo::BlockInfo *CurBlockInfo = nullptr; | |||
430 | ||||
431 | // Read all the records for this module. | |||
432 | while (true) { | |||
433 | Expected<BitstreamEntry> MaybeEntry = | |||
434 | advanceSkippingSubblocks(AF_DontAutoprocessAbbrevs); | |||
435 | if (!MaybeEntry) | |||
436 | return MaybeEntry.takeError(); | |||
437 | BitstreamEntry Entry = MaybeEntry.get(); | |||
438 | ||||
439 | switch (Entry.Kind) { | |||
440 | case llvm::BitstreamEntry::SubBlock: // Handled for us already. | |||
441 | case llvm::BitstreamEntry::Error: | |||
442 | return std::nullopt; | |||
443 | case llvm::BitstreamEntry::EndBlock: | |||
444 | return std::move(NewBlockInfo); | |||
445 | case llvm::BitstreamEntry::Record: | |||
446 | // The interesting case. | |||
447 | break; | |||
448 | } | |||
449 | ||||
450 | // Read abbrev records, associate them with CurBID. | |||
451 | if (Entry.ID == bitc::DEFINE_ABBREV) { | |||
452 | if (!CurBlockInfo) | |||
453 | return std::nullopt; | |||
454 | if (Error Err = ReadAbbrevRecord()) | |||
455 | return std::move(Err); | |||
456 | ||||
457 | // ReadAbbrevRecord installs the abbrev in CurAbbrevs. Move it to the | |||
458 | // appropriate BlockInfo. | |||
459 | CurBlockInfo->Abbrevs.push_back(std::move(CurAbbrevs.back())); | |||
460 | CurAbbrevs.pop_back(); | |||
461 | continue; | |||
462 | } | |||
463 | ||||
464 | // Read a record. | |||
465 | Record.clear(); | |||
466 | Expected<unsigned> MaybeBlockInfo = readRecord(Entry.ID, Record); | |||
467 | if (!MaybeBlockInfo) | |||
468 | return MaybeBlockInfo.takeError(); | |||
469 | switch (MaybeBlockInfo.get()) { | |||
470 | default: | |||
471 | break; // Default behavior, ignore unknown content. | |||
472 | case bitc::BLOCKINFO_CODE_SETBID: | |||
473 | if (Record.size() < 1) | |||
474 | return std::nullopt; | |||
475 | CurBlockInfo = &NewBlockInfo.getOrCreateBlockInfo((unsigned)Record[0]); | |||
476 | break; | |||
477 | case bitc::BLOCKINFO_CODE_BLOCKNAME: { | |||
478 | if (!CurBlockInfo) | |||
479 | return std::nullopt; | |||
480 | if (!ReadBlockInfoNames) | |||
481 | break; // Ignore name. | |||
482 | CurBlockInfo->Name = std::string(Record.begin(), Record.end()); | |||
483 | break; | |||
484 | } | |||
485 | case bitc::BLOCKINFO_CODE_SETRECORDNAME: { | |||
486 | if (!CurBlockInfo) | |||
487 | return std::nullopt; | |||
488 | if (!ReadBlockInfoNames) | |||
489 | break; // Ignore name. | |||
490 | CurBlockInfo->RecordNames.emplace_back( | |||
491 | (unsigned)Record[0], std::string(Record.begin() + 1, Record.end())); | |||
492 | break; | |||
493 | } | |||
494 | } | |||
495 | } | |||
496 | } |
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_BITSTREAM_BITSTREAMREADER_H | ||||
15 | #define LLVM_BITSTREAM_BITSTREAMREADER_H | ||||
16 | |||||
17 | #include "llvm/ADT/ArrayRef.h" | ||||
18 | #include "llvm/ADT/SmallVector.h" | ||||
19 | #include "llvm/Bitstream/BitCodes.h" | ||||
20 | #include "llvm/Support/Endian.h" | ||||
21 | #include "llvm/Support/Error.h" | ||||
22 | #include "llvm/Support/MemoryBufferRef.h" | ||||
23 | #include <algorithm> | ||||
24 | #include <cassert> | ||||
25 | #include <climits> | ||||
26 | #include <cstddef> | ||||
27 | #include <cstdint> | ||||
28 | #include <memory> | ||||
29 | #include <optional> | ||||
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. | ||||
37 | class BitstreamBlockInfo { | ||||
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 = 0; | ||||
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 (const BlockInfo &BI : BlockInfoRecords) | ||||
60 | if (BI.BlockID == BlockID) | ||||
61 | return &BI; | ||||
62 | return nullptr; | ||||
63 | } | ||||
64 | |||||
65 | BlockInfo &getOrCreateBlockInfo(unsigned BlockID) { | ||||
66 | if (const BlockInfo *BI = getBlockInfo(BlockID)) | ||||
67 | return *const_cast<BlockInfo*>(BI); | ||||
68 | |||||
69 | // Otherwise, add a new record. | ||||
70 | BlockInfoRecords.emplace_back(); | ||||
71 | BlockInfoRecords.back().BlockID = BlockID; | ||||
72 | return BlockInfoRecords.back(); | ||||
73 | } | ||||
74 | }; | ||||
75 | |||||
76 | /// This represents a position within a bitstream. There may be multiple | ||||
77 | /// independent cursors reading within one bitstream, each maintaining their | ||||
78 | /// own local state. | ||||
79 | class SimpleBitstreamCursor { | ||||
80 | ArrayRef<uint8_t> BitcodeBytes; | ||||
81 | size_t NextChar = 0; | ||||
82 | |||||
83 | public: | ||||
84 | /// This is the current data we have pulled from the stream but have not | ||||
85 | /// returned to the client. This is specifically and intentionally defined to | ||||
86 | /// follow the word size of the host machine for efficiency. We use word_t in | ||||
87 | /// places that are aware of this to make it perfectly explicit what is going | ||||
88 | /// on. | ||||
89 | using word_t = size_t; | ||||
90 | |||||
91 | private: | ||||
92 | word_t CurWord = 0; | ||||
93 | |||||
94 | /// This is the number of bits in CurWord that are valid. This is always from | ||||
95 | /// [0...bits_of(size_t)-1] inclusive. | ||||
96 | unsigned BitsInCurWord = 0; | ||||
97 | |||||
98 | public: | ||||
99 | SimpleBitstreamCursor() = default; | ||||
100 | explicit SimpleBitstreamCursor(ArrayRef<uint8_t> BitcodeBytes) | ||||
101 | : BitcodeBytes(BitcodeBytes) {} | ||||
102 | explicit SimpleBitstreamCursor(StringRef BitcodeBytes) | ||||
103 | : BitcodeBytes(arrayRefFromStringRef(BitcodeBytes)) {} | ||||
104 | explicit SimpleBitstreamCursor(MemoryBufferRef BitcodeBytes) | ||||
105 | : SimpleBitstreamCursor(BitcodeBytes.getBuffer()) {} | ||||
106 | |||||
107 | bool canSkipToPos(size_t pos) const { | ||||
108 | // pos can be skipped to if it is a valid address or one byte past the end. | ||||
109 | return pos <= BitcodeBytes.size(); | ||||
110 | } | ||||
111 | |||||
112 | bool AtEndOfStream() { | ||||
113 | return BitsInCurWord == 0 && BitcodeBytes.size() <= NextChar; | ||||
114 | } | ||||
115 | |||||
116 | /// Return the bit # of the bit we are reading. | ||||
117 | uint64_t GetCurrentBitNo() const { | ||||
118 | return NextChar*CHAR_BIT8 - BitsInCurWord; | ||||
119 | } | ||||
120 | |||||
121 | // Return the byte # of the current bit. | ||||
122 | uint64_t getCurrentByteNo() const { return GetCurrentBitNo() / 8; } | ||||
123 | |||||
124 | ArrayRef<uint8_t> getBitcodeBytes() const { return BitcodeBytes; } | ||||
125 | |||||
126 | /// Reset the stream to the specified bit number. | ||||
127 | Error JumpToBit(uint64_t BitNo) { | ||||
128 | size_t ByteNo = size_t(BitNo/8) & ~(sizeof(word_t)-1); | ||||
129 | unsigned WordBitNo = unsigned(BitNo & (sizeof(word_t)*8-1)); | ||||
130 | assert(canSkipToPos(ByteNo) && "Invalid location")(static_cast <bool> (canSkipToPos(ByteNo) && "Invalid location" ) ? void (0) : __assert_fail ("canSkipToPos(ByteNo) && \"Invalid location\"" , "llvm/include/llvm/Bitstream/BitstreamReader.h", 130, __extension__ __PRETTY_FUNCTION__)); | ||||
131 | |||||
132 | // Move the cursor to the right word. | ||||
133 | NextChar = ByteNo; | ||||
134 | BitsInCurWord = 0; | ||||
135 | |||||
136 | // Skip over any bits that are already consumed. | ||||
137 | if (WordBitNo) { | ||||
138 | if (Expected<word_t> Res = Read(WordBitNo)) | ||||
139 | return Error::success(); | ||||
140 | else | ||||
141 | return Res.takeError(); | ||||
142 | } | ||||
143 | |||||
144 | return Error::success(); | ||||
145 | } | ||||
146 | |||||
147 | /// Get a pointer into the bitstream at the specified byte offset. | ||||
148 | const uint8_t *getPointerToByte(uint64_t ByteNo, uint64_t NumBytes) { | ||||
149 | return BitcodeBytes.data() + ByteNo; | ||||
150 | } | ||||
151 | |||||
152 | /// Get a pointer into the bitstream at the specified bit offset. | ||||
153 | /// | ||||
154 | /// The bit offset must be on a byte boundary. | ||||
155 | const uint8_t *getPointerToBit(uint64_t BitNo, uint64_t NumBytes) { | ||||
156 | assert(!(BitNo % 8) && "Expected bit on byte boundary")(static_cast <bool> (!(BitNo % 8) && "Expected bit on byte boundary" ) ? void (0) : __assert_fail ("!(BitNo % 8) && \"Expected bit on byte boundary\"" , "llvm/include/llvm/Bitstream/BitstreamReader.h", 156, __extension__ __PRETTY_FUNCTION__)); | ||||
157 | return getPointerToByte(BitNo / 8, NumBytes); | ||||
158 | } | ||||
159 | |||||
160 | Error fillCurWord() { | ||||
161 | if (NextChar >= BitcodeBytes.size()) | ||||
162 | return createStringError(std::errc::io_error, | ||||
163 | "Unexpected end of file reading %u of %u bytes", | ||||
164 | NextChar, BitcodeBytes.size()); | ||||
165 | |||||
166 | // Read the next word from the stream. | ||||
167 | const uint8_t *NextCharPtr = BitcodeBytes.data() + NextChar; | ||||
168 | unsigned BytesRead; | ||||
169 | if (BitcodeBytes.size() >= NextChar + sizeof(word_t)) { | ||||
170 | BytesRead = sizeof(word_t); | ||||
171 | CurWord = | ||||
172 | support::endian::read<word_t, support::little, support::unaligned>( | ||||
173 | NextCharPtr); | ||||
174 | } else { | ||||
175 | // Short read. | ||||
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; | ||||
183 | return Error::success(); | ||||
184 | } | ||||
185 | |||||
186 | Expected<word_t> Read(unsigned NumBits) { | ||||
187 | static const unsigned BitsInWord = sizeof(word_t) * 8; | ||||
188 | |||||
189 | assert(NumBits && NumBits <= BitsInWord &&(static_cast <bool> (NumBits && NumBits <= BitsInWord && "Cannot return zero or more than BitsInWord bits!" ) ? void (0) : __assert_fail ("NumBits && NumBits <= BitsInWord && \"Cannot return zero or more than BitsInWord bits!\"" , "llvm/include/llvm/Bitstream/BitstreamReader.h", 190, __extension__ __PRETTY_FUNCTION__)) | ||||
190 | "Cannot return zero or more than BitsInWord bits!")(static_cast <bool> (NumBits && NumBits <= BitsInWord && "Cannot return zero or more than BitsInWord bits!" ) ? void (0) : __assert_fail ("NumBits && NumBits <= BitsInWord && \"Cannot return zero or more than BitsInWord bits!\"" , "llvm/include/llvm/Bitstream/BitstreamReader.h", 190, __extension__ __PRETTY_FUNCTION__)); | ||||
191 | |||||
192 | static const unsigned Mask = sizeof(word_t) > 4 ? 0x3f : 0x1f; | ||||
193 | |||||
194 | // If the field is fully contained by CurWord, return it quickly. | ||||
195 | if (BitsInCurWord >= NumBits) { | ||||
196 | word_t R = CurWord & (~word_t(0) >> (BitsInWord - NumBits)); | ||||
197 | |||||
198 | // Use a mask to avoid undefined behavior. | ||||
199 | CurWord >>= (NumBits & Mask); | ||||
200 | |||||
201 | BitsInCurWord -= NumBits; | ||||
202 | return R; | ||||
203 | } | ||||
204 | |||||
205 | word_t R = BitsInCurWord
| ||||
206 | unsigned BitsLeft = NumBits - BitsInCurWord; | ||||
207 | |||||
208 | if (Error fillResult = fillCurWord()) | ||||
209 | return std::move(fillResult); | ||||
210 | |||||
211 | // If we run out of data, abort. | ||||
212 | if (BitsLeft > BitsInCurWord) | ||||
213 | return createStringError(std::errc::io_error, | ||||
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 | // Use a mask to avoid undefined behavior. | ||||
220 | CurWord >>= (BitsLeft & Mask); | ||||
221 | |||||
222 | BitsInCurWord -= BitsLeft; | ||||
223 | |||||
224 | R |= R2 << (NumBits - BitsLeft); | ||||
225 | |||||
226 | return R; | ||||
227 | } | ||||
228 | |||||
229 | Expected<uint32_t> ReadVBR(const unsigned NumBits) { | ||||
230 | Expected<unsigned> MaybeRead = Read(NumBits); | ||||
231 | if (!MaybeRead) | ||||
232 | return MaybeRead; | ||||
233 | uint32_t Piece = MaybeRead.get(); | ||||
234 | |||||
235 | assert(NumBits <= 32 && NumBits >= 1 && "Invalid NumBits value")(static_cast <bool> (NumBits <= 32 && NumBits >= 1 && "Invalid NumBits value") ? void (0) : __assert_fail ("NumBits <= 32 && NumBits >= 1 && \"Invalid NumBits value\"" , "llvm/include/llvm/Bitstream/BitstreamReader.h", 235, __extension__ __PRETTY_FUNCTION__)); | ||||
236 | const uint32_t MaskBitOrder = (NumBits - 1); | ||||
237 | const uint32_t Mask = 1UL << MaskBitOrder; | ||||
238 | |||||
239 | if ((Piece & Mask) == 0) | ||||
240 | return Piece; | ||||
241 | |||||
242 | uint32_t Result = 0; | ||||
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) | ||||
252 | return createStringError(std::errc::illegal_byte_sequence, | ||||
253 | "Unterminated VBR"); | ||||
254 | |||||
255 | MaybeRead = Read(NumBits); | ||||
256 | if (!MaybeRead) | ||||
257 | return MaybeRead; | ||||
258 | Piece = MaybeRead.get(); | ||||
259 | } | ||||
260 | } | ||||
261 | |||||
262 | // Read a VBR that may have a value up to 64-bits in size. The chunk size of | ||||
263 | // the VBR must still be <= 32 bits though. | ||||
264 | Expected<uint64_t> ReadVBR64(const unsigned NumBits) { | ||||
265 | Expected<uint64_t> MaybeRead = Read(NumBits); | ||||
266 | if (!MaybeRead) | ||||
267 | return MaybeRead; | ||||
268 | uint32_t Piece = MaybeRead.get(); | ||||
269 | assert(NumBits <= 32 && NumBits >= 1 && "Invalid NumBits value")(static_cast <bool> (NumBits <= 32 && NumBits >= 1 && "Invalid NumBits value") ? void (0) : __assert_fail ("NumBits <= 32 && NumBits >= 1 && \"Invalid NumBits value\"" , "llvm/include/llvm/Bitstream/BitstreamReader.h", 269, __extension__ __PRETTY_FUNCTION__)); | ||||
270 | const uint32_t MaskBitOrder = (NumBits - 1); | ||||
271 | const uint32_t Mask = 1UL << MaskBitOrder; | ||||
272 | |||||
273 | if ((Piece & Mask) == 0) | ||||
274 | return uint64_t(Piece); | ||||
275 | |||||
276 | uint64_t Result = 0; | ||||
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) | ||||
286 | return createStringError(std::errc::illegal_byte_sequence, | ||||
287 | "Unterminated VBR"); | ||||
288 | |||||
289 | MaybeRead = Read(NumBits); | ||||
290 | if (!MaybeRead) | ||||
291 | return MaybeRead; | ||||
292 | Piece = MaybeRead.get(); | ||||
293 | } | ||||
294 | } | ||||
295 | |||||
296 | void SkipToFourByteBoundary() { | ||||
297 | // If word_t is 64-bits and if we've read less than 32 bits, just dump | ||||
298 | // the bits we have up to the next 32-bit boundary. | ||||
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 | /// Return the size of the stream in bytes. | ||||
310 | size_t SizeInBytes() const { return BitcodeBytes.size(); } | ||||
311 | |||||
312 | /// Skip to the end of the file. | ||||
313 | void skipToEnd() { NextChar = BitcodeBytes.size(); } | ||||
314 | |||||
315 | /// Check whether a reservation of Size elements is plausible. | ||||
316 | bool isSizePlausible(size_t Size) const { | ||||
317 | // Don't allow reserving more elements than the number of bits, assuming | ||||
318 | // at least one bit is needed to encode an element. | ||||
319 | return Size < BitcodeBytes.size() * 8; | ||||
320 | } | ||||
321 | }; | ||||
322 | |||||
323 | /// When advancing through a bitstream cursor, each advance can discover a few | ||||
324 | /// different kinds of entries: | ||||
325 | struct BitstreamEntry { | ||||
326 | enum { | ||||
327 | Error, // Malformed bitcode was found. | ||||
328 | EndBlock, // We've reached the end of the current block, (or the end of the | ||||
329 | // file, which is treated like a series of EndBlock records. | ||||
330 | SubBlock, // This is the start of a new subblock of a specific ID. | ||||
331 | Record // This is a record with a specific AbbrevID. | ||||
332 | } Kind; | ||||
333 | |||||
334 | unsigned ID; | ||||
335 | |||||
336 | static BitstreamEntry getError() { | ||||
337 | BitstreamEntry E; E.Kind = Error; return E; | ||||
338 | } | ||||
339 | |||||
340 | static BitstreamEntry getEndBlock() { | ||||
341 | BitstreamEntry E; E.Kind = EndBlock; return E; | ||||
342 | } | ||||
343 | |||||
344 | static BitstreamEntry getSubBlock(unsigned ID) { | ||||
345 | BitstreamEntry E; E.Kind = SubBlock; E.ID = ID; return E; | ||||
346 | } | ||||
347 | |||||
348 | static BitstreamEntry getRecord(unsigned AbbrevID) { | ||||
349 | BitstreamEntry E; E.Kind = Record; E.ID = AbbrevID; return E; | ||||
350 | } | ||||
351 | }; | ||||
352 | |||||
353 | /// This represents a position within a bitcode file, implemented on top of a | ||||
354 | /// SimpleBitstreamCursor. | ||||
355 | /// | ||||
356 | /// Unlike iterators, BitstreamCursors are heavy-weight objects that should not | ||||
357 | /// be passed by value. | ||||
358 | class BitstreamCursor : SimpleBitstreamCursor { | ||||
359 | // This is the declared size of code values used for the current block, in | ||||
360 | // bits. | ||||
361 | unsigned CurCodeSize = 2; | ||||
362 | |||||
363 | /// Abbrevs installed at in this block. | ||||
364 | std::vector<std::shared_ptr<BitCodeAbbrev>> CurAbbrevs; | ||||
365 | |||||
366 | struct Block { | ||||
367 | unsigned PrevCodeSize; | ||||
368 | std::vector<std::shared_ptr<BitCodeAbbrev>> PrevAbbrevs; | ||||
369 | |||||
370 | explicit Block(unsigned PCS) : PrevCodeSize(PCS) {} | ||||
371 | }; | ||||
372 | |||||
373 | /// This tracks the codesize of parent blocks. | ||||
374 | SmallVector<Block, 8> BlockScope; | ||||
375 | |||||
376 | BitstreamBlockInfo *BlockInfo = nullptr; | ||||
377 | |||||
378 | public: | ||||
379 | static const size_t MaxChunkSize = 32; | ||||
380 | |||||
381 | BitstreamCursor() = default; | ||||
382 | explicit BitstreamCursor(ArrayRef<uint8_t> BitcodeBytes) | ||||
383 | : SimpleBitstreamCursor(BitcodeBytes) {} | ||||
384 | explicit BitstreamCursor(StringRef BitcodeBytes) | ||||
385 | : SimpleBitstreamCursor(BitcodeBytes) {} | ||||
386 | explicit BitstreamCursor(MemoryBufferRef BitcodeBytes) | ||||
387 | : SimpleBitstreamCursor(BitcodeBytes) {} | ||||
388 | |||||
389 | using SimpleBitstreamCursor::AtEndOfStream; | ||||
390 | using SimpleBitstreamCursor::canSkipToPos; | ||||
391 | using SimpleBitstreamCursor::fillCurWord; | ||||
392 | using SimpleBitstreamCursor::getBitcodeBytes; | ||||
393 | using SimpleBitstreamCursor::GetCurrentBitNo; | ||||
394 | using SimpleBitstreamCursor::getCurrentByteNo; | ||||
395 | using SimpleBitstreamCursor::getPointerToByte; | ||||
396 | using SimpleBitstreamCursor::JumpToBit; | ||||
397 | using SimpleBitstreamCursor::Read; | ||||
398 | using SimpleBitstreamCursor::ReadVBR; | ||||
399 | using SimpleBitstreamCursor::ReadVBR64; | ||||
400 | using SimpleBitstreamCursor::SizeInBytes; | ||||
401 | using SimpleBitstreamCursor::skipToEnd; | ||||
402 | |||||
403 | /// Return the number of bits used to encode an abbrev #. | ||||
404 | unsigned getAbbrevIDWidth() const { return CurCodeSize; } | ||||
405 | |||||
406 | /// Flags that modify the behavior of advance(). | ||||
407 | enum { | ||||
408 | /// If this flag is used, the advance() method does not automatically pop | ||||
409 | /// the block scope when the end of a block is reached. | ||||
410 | AF_DontPopBlockAtEnd = 1, | ||||
411 | |||||
412 | /// If this flag is used, abbrev entries are returned just like normal | ||||
413 | /// records. | ||||
414 | AF_DontAutoprocessAbbrevs = 2 | ||||
415 | }; | ||||
416 | |||||
417 | /// Advance the current bitstream, returning the next entry in the stream. | ||||
418 | Expected<BitstreamEntry> advance(unsigned Flags = 0) { | ||||
419 | while (true) { | ||||
420 | if (AtEndOfStream()) | ||||
421 | return BitstreamEntry::getError(); | ||||
422 | |||||
423 | Expected<unsigned> MaybeCode = ReadCode(); | ||||
424 | if (!MaybeCode) | ||||
425 | return MaybeCode.takeError(); | ||||
426 | unsigned Code = MaybeCode.get(); | ||||
427 | |||||
428 | if (Code == bitc::END_BLOCK) { | ||||
429 | // Pop the end of the block unless Flags tells us not to. | ||||
430 | if (!(Flags & AF_DontPopBlockAtEnd) && ReadBlockEnd()) | ||||
431 | return BitstreamEntry::getError(); | ||||
432 | return BitstreamEntry::getEndBlock(); | ||||
433 | } | ||||
434 | |||||
435 | if (Code == bitc::ENTER_SUBBLOCK) { | ||||
436 | if (Expected<unsigned> MaybeSubBlock = ReadSubBlockID()) | ||||
437 | return BitstreamEntry::getSubBlock(MaybeSubBlock.get()); | ||||
438 | else | ||||
439 | return MaybeSubBlock.takeError(); | ||||
440 | } | ||||
441 | |||||
442 | if (Code == bitc::DEFINE_ABBREV && | ||||
443 | !(Flags & AF_DontAutoprocessAbbrevs)) { | ||||
444 | // We read and accumulate abbrev's, the client can't do anything with | ||||
445 | // them anyway. | ||||
446 | if (Error Err = ReadAbbrevRecord()) | ||||
447 | return std::move(Err); | ||||
448 | continue; | ||||
449 | } | ||||
450 | |||||
451 | return BitstreamEntry::getRecord(Code); | ||||
452 | } | ||||
453 | } | ||||
454 | |||||
455 | /// This is a convenience function for clients that don't expect any | ||||
456 | /// subblocks. This just skips over them automatically. | ||||
457 | Expected<BitstreamEntry> advanceSkippingSubblocks(unsigned Flags = 0) { | ||||
458 | while (true) { | ||||
459 | // If we found a normal entry, return it. | ||||
460 | Expected<BitstreamEntry> MaybeEntry = advance(Flags); | ||||
461 | if (!MaybeEntry) | ||||
462 | return MaybeEntry; | ||||
463 | BitstreamEntry Entry = MaybeEntry.get(); | ||||
464 | |||||
465 | if (Entry.Kind != BitstreamEntry::SubBlock) | ||||
466 | return Entry; | ||||
467 | |||||
468 | // If we found a sub-block, just skip over it and check the next entry. | ||||
469 | if (Error Err = SkipBlock()) | ||||
470 | return std::move(Err); | ||||
471 | } | ||||
472 | } | ||||
473 | |||||
474 | Expected<unsigned> ReadCode() { return Read(CurCodeSize); } | ||||
475 | |||||
476 | // Block header: | ||||
477 | // [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen] | ||||
478 | |||||
479 | /// Having read the ENTER_SUBBLOCK code, read the BlockID for the block. | ||||
480 | Expected<unsigned> ReadSubBlockID() { return ReadVBR(bitc::BlockIDWidth); } | ||||
481 | |||||
482 | /// Having read the ENTER_SUBBLOCK abbrevid and a BlockID, skip over the body | ||||
483 | /// of this block. | ||||
484 | Error SkipBlock() { | ||||
485 | // Read and ignore the codelen value. | ||||
486 | if (Expected<uint32_t> Res = ReadVBR(bitc::CodeLenWidth)) | ||||
487 | ; // Since we are skipping this block, we don't care what code widths are | ||||
488 | // used inside of it. | ||||
489 | else | ||||
490 | return Res.takeError(); | ||||
491 | |||||
492 | SkipToFourByteBoundary(); | ||||
493 | Expected<unsigned> MaybeNum = Read(bitc::BlockSizeWidth); | ||||
494 | if (!MaybeNum) | ||||
495 | return MaybeNum.takeError(); | ||||
496 | size_t NumFourBytes = MaybeNum.get(); | ||||
497 | |||||
498 | // Check that the block wasn't partially defined, and that the offset isn't | ||||
499 | // bogus. | ||||
500 | size_t SkipTo = GetCurrentBitNo() + NumFourBytes * 4 * 8; | ||||
501 | if (AtEndOfStream()) | ||||
502 | return createStringError(std::errc::illegal_byte_sequence, | ||||
503 | "can't skip block: already at end of stream"); | ||||
504 | if (!canSkipToPos(SkipTo / 8)) | ||||
505 | return createStringError(std::errc::illegal_byte_sequence, | ||||
506 | "can't skip to bit %zu from %" PRIu64"l" "u", SkipTo, | ||||
507 | GetCurrentBitNo()); | ||||
508 | |||||
509 | if (Error Res = JumpToBit(SkipTo)) | ||||
510 | return Res; | ||||
511 | |||||
512 | return Error::success(); | ||||
513 | } | ||||
514 | |||||
515 | /// Having read the ENTER_SUBBLOCK abbrevid, and enter the block. | ||||
516 | Error EnterSubBlock(unsigned BlockID, unsigned *NumWordsP = nullptr); | ||||
517 | |||||
518 | bool ReadBlockEnd() { | ||||
519 | if (BlockScope.empty()) return true; | ||||
520 | |||||
521 | // Block tail: | ||||
522 | // [END_BLOCK, <align4bytes>] | ||||
523 | SkipToFourByteBoundary(); | ||||
524 | |||||
525 | popBlockScope(); | ||||
526 | return false; | ||||
527 | } | ||||
528 | |||||
529 | private: | ||||
530 | void popBlockScope() { | ||||
531 | CurCodeSize = BlockScope.back().PrevCodeSize; | ||||
532 | |||||
533 | CurAbbrevs = std::move(BlockScope.back().PrevAbbrevs); | ||||
534 | BlockScope.pop_back(); | ||||
535 | } | ||||
536 | |||||
537 | //===--------------------------------------------------------------------===// | ||||
538 | // Record Processing | ||||
539 | //===--------------------------------------------------------------------===// | ||||
540 | |||||
541 | public: | ||||
542 | /// Return the abbreviation for the specified AbbrevId. | ||||
543 | Expected<const BitCodeAbbrev *> getAbbrev(unsigned AbbrevID) { | ||||
544 | unsigned AbbrevNo = AbbrevID - bitc::FIRST_APPLICATION_ABBREV; | ||||
545 | if (AbbrevNo >= CurAbbrevs.size()) | ||||
546 | return createStringError( | ||||
547 | std::errc::illegal_byte_sequence, "Invalid abbrev number"); | ||||
548 | return CurAbbrevs[AbbrevNo].get(); | ||||
549 | } | ||||
550 | |||||
551 | /// Read the current record and discard it, returning the code for the record. | ||||
552 | Expected<unsigned> skipRecord(unsigned AbbrevID); | ||||
553 | |||||
554 | Expected<unsigned> readRecord(unsigned AbbrevID, | ||||
555 | SmallVectorImpl<uint64_t> &Vals, | ||||
556 | StringRef *Blob = nullptr); | ||||
557 | |||||
558 | //===--------------------------------------------------------------------===// | ||||
559 | // Abbrev Processing | ||||
560 | //===--------------------------------------------------------------------===// | ||||
561 | Error ReadAbbrevRecord(); | ||||
562 | |||||
563 | /// Read and return a block info block from the bitstream. If an error was | ||||
564 | /// encountered, return std::nullopt. | ||||
565 | /// | ||||
566 | /// \param ReadBlockInfoNames Whether to read block/record name information in | ||||
567 | /// the BlockInfo block. Only llvm-bcanalyzer uses this. | ||||
568 | Expected<std::optional<BitstreamBlockInfo>> | ||||
569 | ReadBlockInfoBlock(bool ReadBlockInfoNames = false); | ||||
570 | |||||
571 | /// Set the block info to be used by this BitstreamCursor to interpret | ||||
572 | /// abbreviated records. | ||||
573 | void setBlockInfo(BitstreamBlockInfo *BI) { BlockInfo = BI; } | ||||
574 | }; | ||||
575 | |||||
576 | } // end llvm namespace | ||||
577 | |||||
578 | #endif // LLVM_BITSTREAM_BITSTREAMREADER_H |