1 *9880d681SAndroid Build Coastguard Worker //===- BitstreamReader.cpp - BitstreamReader implementation ---------------===//
2 *9880d681SAndroid Build Coastguard Worker //
3 *9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4 *9880d681SAndroid Build Coastguard Worker //
5 *9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6 *9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7 *9880d681SAndroid Build Coastguard Worker //
8 *9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9 *9880d681SAndroid Build Coastguard Worker
10 *9880d681SAndroid Build Coastguard Worker #include "llvm/Bitcode/BitstreamReader.h"
11 *9880d681SAndroid Build Coastguard Worker
12 *9880d681SAndroid Build Coastguard Worker using namespace llvm;
13 *9880d681SAndroid Build Coastguard Worker
14 *9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
15 *9880d681SAndroid Build Coastguard Worker // BitstreamCursor implementation
16 *9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
17 *9880d681SAndroid Build Coastguard Worker
freeState()18 *9880d681SAndroid Build Coastguard Worker void BitstreamCursor::freeState() {
19 *9880d681SAndroid Build Coastguard Worker // Free all the Abbrevs.
20 *9880d681SAndroid Build Coastguard Worker CurAbbrevs.clear();
21 *9880d681SAndroid Build Coastguard Worker
22 *9880d681SAndroid Build Coastguard Worker // Free all the Abbrevs in the block scope.
23 *9880d681SAndroid Build Coastguard Worker BlockScope.clear();
24 *9880d681SAndroid Build Coastguard Worker }
25 *9880d681SAndroid Build Coastguard Worker
26 *9880d681SAndroid Build Coastguard Worker /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, enter
27 *9880d681SAndroid Build Coastguard Worker /// the block, and return true if the block has an error.
EnterSubBlock(unsigned BlockID,unsigned * NumWordsP)28 *9880d681SAndroid Build Coastguard Worker bool BitstreamCursor::EnterSubBlock(unsigned BlockID, unsigned *NumWordsP) {
29 *9880d681SAndroid Build Coastguard Worker // Save the current block's state on BlockScope.
30 *9880d681SAndroid Build Coastguard Worker BlockScope.push_back(Block(CurCodeSize));
31 *9880d681SAndroid Build Coastguard Worker BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
32 *9880d681SAndroid Build Coastguard Worker
33 *9880d681SAndroid Build Coastguard Worker // Add the abbrevs specific to this block to the CurAbbrevs list.
34 *9880d681SAndroid Build Coastguard Worker if (const BitstreamReader::BlockInfo *Info =
35 *9880d681SAndroid Build Coastguard Worker getBitStreamReader()->getBlockInfo(BlockID)) {
36 *9880d681SAndroid Build Coastguard Worker CurAbbrevs.insert(CurAbbrevs.end(), Info->Abbrevs.begin(),
37 *9880d681SAndroid Build Coastguard Worker Info->Abbrevs.end());
38 *9880d681SAndroid Build Coastguard Worker }
39 *9880d681SAndroid Build Coastguard Worker
40 *9880d681SAndroid Build Coastguard Worker // Get the codesize of this block.
41 *9880d681SAndroid Build Coastguard Worker CurCodeSize = ReadVBR(bitc::CodeLenWidth);
42 *9880d681SAndroid Build Coastguard Worker // We can't read more than MaxChunkSize at a time
43 *9880d681SAndroid Build Coastguard Worker if (CurCodeSize > MaxChunkSize)
44 *9880d681SAndroid Build Coastguard Worker return true;
45 *9880d681SAndroid Build Coastguard Worker
46 *9880d681SAndroid Build Coastguard Worker SkipToFourByteBoundary();
47 *9880d681SAndroid Build Coastguard Worker unsigned NumWords = Read(bitc::BlockSizeWidth);
48 *9880d681SAndroid Build Coastguard Worker if (NumWordsP) *NumWordsP = NumWords;
49 *9880d681SAndroid Build Coastguard Worker
50 *9880d681SAndroid Build Coastguard Worker // Validate that this block is sane.
51 *9880d681SAndroid Build Coastguard Worker return CurCodeSize == 0 || AtEndOfStream();
52 *9880d681SAndroid Build Coastguard Worker }
53 *9880d681SAndroid Build Coastguard Worker
readAbbreviatedField(BitstreamCursor & Cursor,const BitCodeAbbrevOp & Op)54 *9880d681SAndroid Build Coastguard Worker static uint64_t readAbbreviatedField(BitstreamCursor &Cursor,
55 *9880d681SAndroid Build Coastguard Worker const BitCodeAbbrevOp &Op) {
56 *9880d681SAndroid Build Coastguard Worker assert(!Op.isLiteral() && "Not to be used with literals!");
57 *9880d681SAndroid Build Coastguard Worker
58 *9880d681SAndroid Build Coastguard Worker // Decode the value as we are commanded.
59 *9880d681SAndroid Build Coastguard Worker switch (Op.getEncoding()) {
60 *9880d681SAndroid Build Coastguard Worker case BitCodeAbbrevOp::Array:
61 *9880d681SAndroid Build Coastguard Worker case BitCodeAbbrevOp::Blob:
62 *9880d681SAndroid Build Coastguard Worker llvm_unreachable("Should not reach here");
63 *9880d681SAndroid Build Coastguard Worker case BitCodeAbbrevOp::Fixed:
64 *9880d681SAndroid Build Coastguard Worker assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
65 *9880d681SAndroid Build Coastguard Worker return Cursor.Read((unsigned)Op.getEncodingData());
66 *9880d681SAndroid Build Coastguard Worker case BitCodeAbbrevOp::VBR:
67 *9880d681SAndroid Build Coastguard Worker assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
68 *9880d681SAndroid Build Coastguard Worker return Cursor.ReadVBR64((unsigned)Op.getEncodingData());
69 *9880d681SAndroid Build Coastguard Worker case BitCodeAbbrevOp::Char6:
70 *9880d681SAndroid Build Coastguard Worker return BitCodeAbbrevOp::DecodeChar6(Cursor.Read(6));
71 *9880d681SAndroid Build Coastguard Worker }
72 *9880d681SAndroid Build Coastguard Worker llvm_unreachable("invalid abbreviation encoding");
73 *9880d681SAndroid Build Coastguard Worker }
74 *9880d681SAndroid Build Coastguard Worker
skipAbbreviatedField(BitstreamCursor & Cursor,const BitCodeAbbrevOp & Op)75 *9880d681SAndroid Build Coastguard Worker static void skipAbbreviatedField(BitstreamCursor &Cursor,
76 *9880d681SAndroid Build Coastguard Worker const BitCodeAbbrevOp &Op) {
77 *9880d681SAndroid Build Coastguard Worker assert(!Op.isLiteral() && "Not to be used with literals!");
78 *9880d681SAndroid Build Coastguard Worker
79 *9880d681SAndroid Build Coastguard Worker // Decode the value as we are commanded.
80 *9880d681SAndroid Build Coastguard Worker switch (Op.getEncoding()) {
81 *9880d681SAndroid Build Coastguard Worker case BitCodeAbbrevOp::Array:
82 *9880d681SAndroid Build Coastguard Worker case BitCodeAbbrevOp::Blob:
83 *9880d681SAndroid Build Coastguard Worker llvm_unreachable("Should not reach here");
84 *9880d681SAndroid Build Coastguard Worker case BitCodeAbbrevOp::Fixed:
85 *9880d681SAndroid Build Coastguard Worker assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
86 *9880d681SAndroid Build Coastguard Worker Cursor.Read((unsigned)Op.getEncodingData());
87 *9880d681SAndroid Build Coastguard Worker break;
88 *9880d681SAndroid Build Coastguard Worker case BitCodeAbbrevOp::VBR:
89 *9880d681SAndroid Build Coastguard Worker assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
90 *9880d681SAndroid Build Coastguard Worker Cursor.ReadVBR64((unsigned)Op.getEncodingData());
91 *9880d681SAndroid Build Coastguard Worker break;
92 *9880d681SAndroid Build Coastguard Worker case BitCodeAbbrevOp::Char6:
93 *9880d681SAndroid Build Coastguard Worker Cursor.Read(6);
94 *9880d681SAndroid Build Coastguard Worker break;
95 *9880d681SAndroid Build Coastguard Worker }
96 *9880d681SAndroid Build Coastguard Worker }
97 *9880d681SAndroid Build Coastguard Worker
98 *9880d681SAndroid Build Coastguard Worker
99 *9880d681SAndroid Build Coastguard Worker
100 *9880d681SAndroid Build Coastguard Worker /// skipRecord - Read the current record and discard it.
skipRecord(unsigned AbbrevID)101 *9880d681SAndroid Build Coastguard Worker void BitstreamCursor::skipRecord(unsigned AbbrevID) {
102 *9880d681SAndroid Build Coastguard Worker // Skip unabbreviated records by reading past their entries.
103 *9880d681SAndroid Build Coastguard Worker if (AbbrevID == bitc::UNABBREV_RECORD) {
104 *9880d681SAndroid Build Coastguard Worker unsigned Code = ReadVBR(6);
105 *9880d681SAndroid Build Coastguard Worker (void)Code;
106 *9880d681SAndroid Build Coastguard Worker unsigned NumElts = ReadVBR(6);
107 *9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != NumElts; ++i)
108 *9880d681SAndroid Build Coastguard Worker (void)ReadVBR64(6);
109 *9880d681SAndroid Build Coastguard Worker return;
110 *9880d681SAndroid Build Coastguard Worker }
111 *9880d681SAndroid Build Coastguard Worker
112 *9880d681SAndroid Build Coastguard Worker const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
113 *9880d681SAndroid Build Coastguard Worker
114 *9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
115 *9880d681SAndroid Build Coastguard Worker const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
116 *9880d681SAndroid Build Coastguard Worker if (Op.isLiteral())
117 *9880d681SAndroid Build Coastguard Worker continue;
118 *9880d681SAndroid Build Coastguard Worker
119 *9880d681SAndroid Build Coastguard Worker if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
120 *9880d681SAndroid Build Coastguard Worker Op.getEncoding() != BitCodeAbbrevOp::Blob) {
121 *9880d681SAndroid Build Coastguard Worker skipAbbreviatedField(*this, Op);
122 *9880d681SAndroid Build Coastguard Worker continue;
123 *9880d681SAndroid Build Coastguard Worker }
124 *9880d681SAndroid Build Coastguard Worker
125 *9880d681SAndroid Build Coastguard Worker if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
126 *9880d681SAndroid Build Coastguard Worker // Array case. Read the number of elements as a vbr6.
127 *9880d681SAndroid Build Coastguard Worker unsigned NumElts = ReadVBR(6);
128 *9880d681SAndroid Build Coastguard Worker
129 *9880d681SAndroid Build Coastguard Worker // Get the element encoding.
130 *9880d681SAndroid Build Coastguard Worker assert(i+2 == e && "array op not second to last?");
131 *9880d681SAndroid Build Coastguard Worker const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
132 *9880d681SAndroid Build Coastguard Worker
133 *9880d681SAndroid Build Coastguard Worker // Read all the elements.
134 *9880d681SAndroid Build Coastguard Worker // Decode the value as we are commanded.
135 *9880d681SAndroid Build Coastguard Worker switch (EltEnc.getEncoding()) {
136 *9880d681SAndroid Build Coastguard Worker default:
137 *9880d681SAndroid Build Coastguard Worker report_fatal_error("Array element type can't be an Array or a Blob");
138 *9880d681SAndroid Build Coastguard Worker case BitCodeAbbrevOp::Fixed:
139 *9880d681SAndroid Build Coastguard Worker assert((unsigned)Op.getEncodingData() <= MaxChunkSize);
140 *9880d681SAndroid Build Coastguard Worker for (; NumElts; --NumElts)
141 *9880d681SAndroid Build Coastguard Worker Read((unsigned)EltEnc.getEncodingData());
142 *9880d681SAndroid Build Coastguard Worker break;
143 *9880d681SAndroid Build Coastguard Worker case BitCodeAbbrevOp::VBR:
144 *9880d681SAndroid Build Coastguard Worker assert((unsigned)Op.getEncodingData() <= MaxChunkSize);
145 *9880d681SAndroid Build Coastguard Worker for (; NumElts; --NumElts)
146 *9880d681SAndroid Build Coastguard Worker ReadVBR64((unsigned)EltEnc.getEncodingData());
147 *9880d681SAndroid Build Coastguard Worker break;
148 *9880d681SAndroid Build Coastguard Worker case BitCodeAbbrevOp::Char6:
149 *9880d681SAndroid Build Coastguard Worker for (; NumElts; --NumElts)
150 *9880d681SAndroid Build Coastguard Worker Read(6);
151 *9880d681SAndroid Build Coastguard Worker break;
152 *9880d681SAndroid Build Coastguard Worker }
153 *9880d681SAndroid Build Coastguard Worker continue;
154 *9880d681SAndroid Build Coastguard Worker }
155 *9880d681SAndroid Build Coastguard Worker
156 *9880d681SAndroid Build Coastguard Worker assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
157 *9880d681SAndroid Build Coastguard Worker // Blob case. Read the number of bytes as a vbr6.
158 *9880d681SAndroid Build Coastguard Worker unsigned NumElts = ReadVBR(6);
159 *9880d681SAndroid Build Coastguard Worker SkipToFourByteBoundary(); // 32-bit alignment
160 *9880d681SAndroid Build Coastguard Worker
161 *9880d681SAndroid Build Coastguard Worker // Figure out where the end of this blob will be including tail padding.
162 *9880d681SAndroid Build Coastguard Worker size_t NewEnd = GetCurrentBitNo()+((NumElts+3)&~3)*8;
163 *9880d681SAndroid Build Coastguard Worker
164 *9880d681SAndroid Build Coastguard Worker // If this would read off the end of the bitcode file, just set the
165 *9880d681SAndroid Build Coastguard Worker // record to empty and return.
166 *9880d681SAndroid Build Coastguard Worker if (!canSkipToPos(NewEnd/8)) {
167 *9880d681SAndroid Build Coastguard Worker skipToEnd();
168 *9880d681SAndroid Build Coastguard Worker break;
169 *9880d681SAndroid Build Coastguard Worker }
170 *9880d681SAndroid Build Coastguard Worker
171 *9880d681SAndroid Build Coastguard Worker // Skip over the blob.
172 *9880d681SAndroid Build Coastguard Worker JumpToBit(NewEnd);
173 *9880d681SAndroid Build Coastguard Worker }
174 *9880d681SAndroid Build Coastguard Worker }
175 *9880d681SAndroid Build Coastguard Worker
readRecord(unsigned AbbrevID,SmallVectorImpl<uint64_t> & Vals,StringRef * Blob)176 *9880d681SAndroid Build Coastguard Worker unsigned BitstreamCursor::readRecord(unsigned AbbrevID,
177 *9880d681SAndroid Build Coastguard Worker SmallVectorImpl<uint64_t> &Vals,
178 *9880d681SAndroid Build Coastguard Worker StringRef *Blob) {
179 *9880d681SAndroid Build Coastguard Worker if (AbbrevID == bitc::UNABBREV_RECORD) {
180 *9880d681SAndroid Build Coastguard Worker unsigned Code = ReadVBR(6);
181 *9880d681SAndroid Build Coastguard Worker unsigned NumElts = ReadVBR(6);
182 *9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != NumElts; ++i)
183 *9880d681SAndroid Build Coastguard Worker Vals.push_back(ReadVBR64(6));
184 *9880d681SAndroid Build Coastguard Worker return Code;
185 *9880d681SAndroid Build Coastguard Worker }
186 *9880d681SAndroid Build Coastguard Worker
187 *9880d681SAndroid Build Coastguard Worker const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
188 *9880d681SAndroid Build Coastguard Worker
189 *9880d681SAndroid Build Coastguard Worker // Read the record code first.
190 *9880d681SAndroid Build Coastguard Worker assert(Abbv->getNumOperandInfos() != 0 && "no record code in abbreviation?");
191 *9880d681SAndroid Build Coastguard Worker const BitCodeAbbrevOp &CodeOp = Abbv->getOperandInfo(0);
192 *9880d681SAndroid Build Coastguard Worker unsigned Code;
193 *9880d681SAndroid Build Coastguard Worker if (CodeOp.isLiteral())
194 *9880d681SAndroid Build Coastguard Worker Code = CodeOp.getLiteralValue();
195 *9880d681SAndroid Build Coastguard Worker else {
196 *9880d681SAndroid Build Coastguard Worker if (CodeOp.getEncoding() == BitCodeAbbrevOp::Array ||
197 *9880d681SAndroid Build Coastguard Worker CodeOp.getEncoding() == BitCodeAbbrevOp::Blob)
198 *9880d681SAndroid Build Coastguard Worker report_fatal_error("Abbreviation starts with an Array or a Blob");
199 *9880d681SAndroid Build Coastguard Worker Code = readAbbreviatedField(*this, CodeOp);
200 *9880d681SAndroid Build Coastguard Worker }
201 *9880d681SAndroid Build Coastguard Worker
202 *9880d681SAndroid Build Coastguard Worker for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i != e; ++i) {
203 *9880d681SAndroid Build Coastguard Worker const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
204 *9880d681SAndroid Build Coastguard Worker if (Op.isLiteral()) {
205 *9880d681SAndroid Build Coastguard Worker Vals.push_back(Op.getLiteralValue());
206 *9880d681SAndroid Build Coastguard Worker continue;
207 *9880d681SAndroid Build Coastguard Worker }
208 *9880d681SAndroid Build Coastguard Worker
209 *9880d681SAndroid Build Coastguard Worker if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
210 *9880d681SAndroid Build Coastguard Worker Op.getEncoding() != BitCodeAbbrevOp::Blob) {
211 *9880d681SAndroid Build Coastguard Worker Vals.push_back(readAbbreviatedField(*this, Op));
212 *9880d681SAndroid Build Coastguard Worker continue;
213 *9880d681SAndroid Build Coastguard Worker }
214 *9880d681SAndroid Build Coastguard Worker
215 *9880d681SAndroid Build Coastguard Worker if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
216 *9880d681SAndroid Build Coastguard Worker // Array case. Read the number of elements as a vbr6.
217 *9880d681SAndroid Build Coastguard Worker unsigned NumElts = ReadVBR(6);
218 *9880d681SAndroid Build Coastguard Worker
219 *9880d681SAndroid Build Coastguard Worker // Get the element encoding.
220 *9880d681SAndroid Build Coastguard Worker if (i + 2 != e)
221 *9880d681SAndroid Build Coastguard Worker report_fatal_error("Array op not second to last");
222 *9880d681SAndroid Build Coastguard Worker const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
223 *9880d681SAndroid Build Coastguard Worker if (!EltEnc.isEncoding())
224 *9880d681SAndroid Build Coastguard Worker report_fatal_error(
225 *9880d681SAndroid Build Coastguard Worker "Array element type has to be an encoding of a type");
226 *9880d681SAndroid Build Coastguard Worker
227 *9880d681SAndroid Build Coastguard Worker // Read all the elements.
228 *9880d681SAndroid Build Coastguard Worker switch (EltEnc.getEncoding()) {
229 *9880d681SAndroid Build Coastguard Worker default:
230 *9880d681SAndroid Build Coastguard Worker report_fatal_error("Array element type can't be an Array or a Blob");
231 *9880d681SAndroid Build Coastguard Worker case BitCodeAbbrevOp::Fixed:
232 *9880d681SAndroid Build Coastguard Worker for (; NumElts; --NumElts)
233 *9880d681SAndroid Build Coastguard Worker Vals.push_back(Read((unsigned)EltEnc.getEncodingData()));
234 *9880d681SAndroid Build Coastguard Worker break;
235 *9880d681SAndroid Build Coastguard Worker case BitCodeAbbrevOp::VBR:
236 *9880d681SAndroid Build Coastguard Worker for (; NumElts; --NumElts)
237 *9880d681SAndroid Build Coastguard Worker Vals.push_back(ReadVBR64((unsigned)EltEnc.getEncodingData()));
238 *9880d681SAndroid Build Coastguard Worker break;
239 *9880d681SAndroid Build Coastguard Worker case BitCodeAbbrevOp::Char6:
240 *9880d681SAndroid Build Coastguard Worker for (; NumElts; --NumElts)
241 *9880d681SAndroid Build Coastguard Worker Vals.push_back(BitCodeAbbrevOp::DecodeChar6(Read(6)));
242 *9880d681SAndroid Build Coastguard Worker }
243 *9880d681SAndroid Build Coastguard Worker continue;
244 *9880d681SAndroid Build Coastguard Worker }
245 *9880d681SAndroid Build Coastguard Worker
246 *9880d681SAndroid Build Coastguard Worker assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
247 *9880d681SAndroid Build Coastguard Worker // Blob case. Read the number of bytes as a vbr6.
248 *9880d681SAndroid Build Coastguard Worker unsigned NumElts = ReadVBR(6);
249 *9880d681SAndroid Build Coastguard Worker SkipToFourByteBoundary(); // 32-bit alignment
250 *9880d681SAndroid Build Coastguard Worker
251 *9880d681SAndroid Build Coastguard Worker // Figure out where the end of this blob will be including tail padding.
252 *9880d681SAndroid Build Coastguard Worker size_t CurBitPos = GetCurrentBitNo();
253 *9880d681SAndroid Build Coastguard Worker size_t NewEnd = CurBitPos+((NumElts+3)&~3)*8;
254 *9880d681SAndroid Build Coastguard Worker
255 *9880d681SAndroid Build Coastguard Worker // If this would read off the end of the bitcode file, just set the
256 *9880d681SAndroid Build Coastguard Worker // record to empty and return.
257 *9880d681SAndroid Build Coastguard Worker if (!canSkipToPos(NewEnd/8)) {
258 *9880d681SAndroid Build Coastguard Worker Vals.append(NumElts, 0);
259 *9880d681SAndroid Build Coastguard Worker skipToEnd();
260 *9880d681SAndroid Build Coastguard Worker break;
261 *9880d681SAndroid Build Coastguard Worker }
262 *9880d681SAndroid Build Coastguard Worker
263 *9880d681SAndroid Build Coastguard Worker // Otherwise, inform the streamer that we need these bytes in memory. Skip
264 *9880d681SAndroid Build Coastguard Worker // over tail padding first, in case jumping to NewEnd invalidates the Blob
265 *9880d681SAndroid Build Coastguard Worker // pointer.
266 *9880d681SAndroid Build Coastguard Worker JumpToBit(NewEnd);
267 *9880d681SAndroid Build Coastguard Worker const char *Ptr = (const char *)getPointerToBit(CurBitPos, NumElts);
268 *9880d681SAndroid Build Coastguard Worker
269 *9880d681SAndroid Build Coastguard Worker // If we can return a reference to the data, do so to avoid copying it.
270 *9880d681SAndroid Build Coastguard Worker if (Blob) {
271 *9880d681SAndroid Build Coastguard Worker *Blob = StringRef(Ptr, NumElts);
272 *9880d681SAndroid Build Coastguard Worker } else {
273 *9880d681SAndroid Build Coastguard Worker // Otherwise, unpack into Vals with zero extension.
274 *9880d681SAndroid Build Coastguard Worker for (; NumElts; --NumElts)
275 *9880d681SAndroid Build Coastguard Worker Vals.push_back((unsigned char)*Ptr++);
276 *9880d681SAndroid Build Coastguard Worker }
277 *9880d681SAndroid Build Coastguard Worker }
278 *9880d681SAndroid Build Coastguard Worker
279 *9880d681SAndroid Build Coastguard Worker return Code;
280 *9880d681SAndroid Build Coastguard Worker }
281 *9880d681SAndroid Build Coastguard Worker
282 *9880d681SAndroid Build Coastguard Worker
ReadAbbrevRecord()283 *9880d681SAndroid Build Coastguard Worker void BitstreamCursor::ReadAbbrevRecord() {
284 *9880d681SAndroid Build Coastguard Worker BitCodeAbbrev *Abbv = new BitCodeAbbrev();
285 *9880d681SAndroid Build Coastguard Worker unsigned NumOpInfo = ReadVBR(5);
286 *9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != NumOpInfo; ++i) {
287 *9880d681SAndroid Build Coastguard Worker bool IsLiteral = Read(1);
288 *9880d681SAndroid Build Coastguard Worker if (IsLiteral) {
289 *9880d681SAndroid Build Coastguard Worker Abbv->Add(BitCodeAbbrevOp(ReadVBR64(8)));
290 *9880d681SAndroid Build Coastguard Worker continue;
291 *9880d681SAndroid Build Coastguard Worker }
292 *9880d681SAndroid Build Coastguard Worker
293 *9880d681SAndroid Build Coastguard Worker BitCodeAbbrevOp::Encoding E = (BitCodeAbbrevOp::Encoding)Read(3);
294 *9880d681SAndroid Build Coastguard Worker if (BitCodeAbbrevOp::hasEncodingData(E)) {
295 *9880d681SAndroid Build Coastguard Worker uint64_t Data = ReadVBR64(5);
296 *9880d681SAndroid Build Coastguard Worker
297 *9880d681SAndroid Build Coastguard Worker // As a special case, handle fixed(0) (i.e., a fixed field with zero bits)
298 *9880d681SAndroid Build Coastguard Worker // and vbr(0) as a literal zero. This is decoded the same way, and avoids
299 *9880d681SAndroid Build Coastguard Worker // a slow path in Read() to have to handle reading zero bits.
300 *9880d681SAndroid Build Coastguard Worker if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
301 *9880d681SAndroid Build Coastguard Worker Data == 0) {
302 *9880d681SAndroid Build Coastguard Worker Abbv->Add(BitCodeAbbrevOp(0));
303 *9880d681SAndroid Build Coastguard Worker continue;
304 *9880d681SAndroid Build Coastguard Worker }
305 *9880d681SAndroid Build Coastguard Worker
306 *9880d681SAndroid Build Coastguard Worker if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
307 *9880d681SAndroid Build Coastguard Worker Data > MaxChunkSize)
308 *9880d681SAndroid Build Coastguard Worker report_fatal_error(
309 *9880d681SAndroid Build Coastguard Worker "Fixed or VBR abbrev record with size > MaxChunkData");
310 *9880d681SAndroid Build Coastguard Worker
311 *9880d681SAndroid Build Coastguard Worker Abbv->Add(BitCodeAbbrevOp(E, Data));
312 *9880d681SAndroid Build Coastguard Worker } else
313 *9880d681SAndroid Build Coastguard Worker Abbv->Add(BitCodeAbbrevOp(E));
314 *9880d681SAndroid Build Coastguard Worker }
315 *9880d681SAndroid Build Coastguard Worker
316 *9880d681SAndroid Build Coastguard Worker if (Abbv->getNumOperandInfos() == 0)
317 *9880d681SAndroid Build Coastguard Worker report_fatal_error("Abbrev record with no operands");
318 *9880d681SAndroid Build Coastguard Worker CurAbbrevs.push_back(Abbv);
319 *9880d681SAndroid Build Coastguard Worker }
320 *9880d681SAndroid Build Coastguard Worker
ReadBlockInfoBlock()321 *9880d681SAndroid Build Coastguard Worker bool BitstreamCursor::ReadBlockInfoBlock() {
322 *9880d681SAndroid Build Coastguard Worker // If this is the second stream to get to the block info block, skip it.
323 *9880d681SAndroid Build Coastguard Worker if (getBitStreamReader()->hasBlockInfoRecords())
324 *9880d681SAndroid Build Coastguard Worker return SkipBlock();
325 *9880d681SAndroid Build Coastguard Worker
326 *9880d681SAndroid Build Coastguard Worker if (EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID)) return true;
327 *9880d681SAndroid Build Coastguard Worker
328 *9880d681SAndroid Build Coastguard Worker SmallVector<uint64_t, 64> Record;
329 *9880d681SAndroid Build Coastguard Worker BitstreamReader::BlockInfo *CurBlockInfo = nullptr;
330 *9880d681SAndroid Build Coastguard Worker
331 *9880d681SAndroid Build Coastguard Worker // Read all the records for this module.
332 *9880d681SAndroid Build Coastguard Worker while (1) {
333 *9880d681SAndroid Build Coastguard Worker BitstreamEntry Entry = advanceSkippingSubblocks(AF_DontAutoprocessAbbrevs);
334 *9880d681SAndroid Build Coastguard Worker
335 *9880d681SAndroid Build Coastguard Worker switch (Entry.Kind) {
336 *9880d681SAndroid Build Coastguard Worker case llvm::BitstreamEntry::SubBlock: // Handled for us already.
337 *9880d681SAndroid Build Coastguard Worker case llvm::BitstreamEntry::Error:
338 *9880d681SAndroid Build Coastguard Worker return true;
339 *9880d681SAndroid Build Coastguard Worker case llvm::BitstreamEntry::EndBlock:
340 *9880d681SAndroid Build Coastguard Worker return false;
341 *9880d681SAndroid Build Coastguard Worker case llvm::BitstreamEntry::Record:
342 *9880d681SAndroid Build Coastguard Worker // The interesting case.
343 *9880d681SAndroid Build Coastguard Worker break;
344 *9880d681SAndroid Build Coastguard Worker }
345 *9880d681SAndroid Build Coastguard Worker
346 *9880d681SAndroid Build Coastguard Worker // Read abbrev records, associate them with CurBID.
347 *9880d681SAndroid Build Coastguard Worker if (Entry.ID == bitc::DEFINE_ABBREV) {
348 *9880d681SAndroid Build Coastguard Worker if (!CurBlockInfo) return true;
349 *9880d681SAndroid Build Coastguard Worker ReadAbbrevRecord();
350 *9880d681SAndroid Build Coastguard Worker
351 *9880d681SAndroid Build Coastguard Worker // ReadAbbrevRecord installs the abbrev in CurAbbrevs. Move it to the
352 *9880d681SAndroid Build Coastguard Worker // appropriate BlockInfo.
353 *9880d681SAndroid Build Coastguard Worker CurBlockInfo->Abbrevs.push_back(std::move(CurAbbrevs.back()));
354 *9880d681SAndroid Build Coastguard Worker CurAbbrevs.pop_back();
355 *9880d681SAndroid Build Coastguard Worker continue;
356 *9880d681SAndroid Build Coastguard Worker }
357 *9880d681SAndroid Build Coastguard Worker
358 *9880d681SAndroid Build Coastguard Worker // Read a record.
359 *9880d681SAndroid Build Coastguard Worker Record.clear();
360 *9880d681SAndroid Build Coastguard Worker switch (readRecord(Entry.ID, Record)) {
361 *9880d681SAndroid Build Coastguard Worker default: break; // Default behavior, ignore unknown content.
362 *9880d681SAndroid Build Coastguard Worker case bitc::BLOCKINFO_CODE_SETBID:
363 *9880d681SAndroid Build Coastguard Worker if (Record.size() < 1) return true;
364 *9880d681SAndroid Build Coastguard Worker CurBlockInfo =
365 *9880d681SAndroid Build Coastguard Worker &getBitStreamReader()->getOrCreateBlockInfo((unsigned)Record[0]);
366 *9880d681SAndroid Build Coastguard Worker break;
367 *9880d681SAndroid Build Coastguard Worker case bitc::BLOCKINFO_CODE_BLOCKNAME: {
368 *9880d681SAndroid Build Coastguard Worker if (!CurBlockInfo) return true;
369 *9880d681SAndroid Build Coastguard Worker if (getBitStreamReader()->isIgnoringBlockInfoNames())
370 *9880d681SAndroid Build Coastguard Worker break; // Ignore name.
371 *9880d681SAndroid Build Coastguard Worker std::string Name;
372 *9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = Record.size(); i != e; ++i)
373 *9880d681SAndroid Build Coastguard Worker Name += (char)Record[i];
374 *9880d681SAndroid Build Coastguard Worker CurBlockInfo->Name = Name;
375 *9880d681SAndroid Build Coastguard Worker break;
376 *9880d681SAndroid Build Coastguard Worker }
377 *9880d681SAndroid Build Coastguard Worker case bitc::BLOCKINFO_CODE_SETRECORDNAME: {
378 *9880d681SAndroid Build Coastguard Worker if (!CurBlockInfo) return true;
379 *9880d681SAndroid Build Coastguard Worker if (getBitStreamReader()->isIgnoringBlockInfoNames())
380 *9880d681SAndroid Build Coastguard Worker break; // Ignore name.
381 *9880d681SAndroid Build Coastguard Worker std::string Name;
382 *9880d681SAndroid Build Coastguard Worker for (unsigned i = 1, e = Record.size(); i != e; ++i)
383 *9880d681SAndroid Build Coastguard Worker Name += (char)Record[i];
384 *9880d681SAndroid Build Coastguard Worker CurBlockInfo->RecordNames.push_back(std::make_pair((unsigned)Record[0],
385 *9880d681SAndroid Build Coastguard Worker Name));
386 *9880d681SAndroid Build Coastguard Worker break;
387 *9880d681SAndroid Build Coastguard Worker }
388 *9880d681SAndroid Build Coastguard Worker }
389 *9880d681SAndroid Build Coastguard Worker }
390 *9880d681SAndroid Build Coastguard Worker }
391 *9880d681SAndroid Build Coastguard Worker
392