OLD | NEW |
(Empty) | |
| 1 //===- NaClBitstreamReader.cpp --------------------------------------------===// |
| 2 // NaClBitstreamReader implementation |
| 3 // |
| 4 // The LLVM Compiler Infrastructure |
| 5 // |
| 6 // This file is distributed under the University of Illinois Open Source |
| 7 // License. See LICENSE.TXT for details. |
| 8 // |
| 9 //===----------------------------------------------------------------------===// |
| 10 |
| 11 #include "llvm/Bitcode/NaCl/NaClBitstreamReader.h" |
| 12 #include "llvm/Support/raw_ostream.h" |
| 13 |
| 14 using namespace llvm; |
| 15 |
| 16 //===----------------------------------------------------------------------===// |
| 17 // NaClBitstreamCursor implementation |
| 18 //===----------------------------------------------------------------------===// |
| 19 |
| 20 void NaClBitstreamCursor::freeState() { |
| 21 // Free all the Abbrevs. |
| 22 for (size_t i = 0, e = CurAbbrevs.size(); i != e; ++i) |
| 23 CurAbbrevs[i]->dropRef(); |
| 24 CurAbbrevs.clear(); |
| 25 |
| 26 // Free all the Abbrevs in the block scope. |
| 27 for (size_t S = 0, e = BlockScope.size(); S != e; ++S) { |
| 28 std::vector<NaClBitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs; |
| 29 for (size_t i = 0, e = Abbrevs.size(); i != e; ++i) |
| 30 Abbrevs[i]->dropRef(); |
| 31 } |
| 32 BlockScope.clear(); |
| 33 } |
| 34 |
| 35 /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, enter |
| 36 /// the block, and return true if the block has an error. |
| 37 bool NaClBitstreamCursor::EnterSubBlock(unsigned BlockID, unsigned *NumWordsP) { |
| 38 // Save the current block's state on BlockScope. |
| 39 BlockScope.push_back(Block(CurCodeSize)); |
| 40 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs); |
| 41 |
| 42 // Add the abbrevs specific to this block to the CurAbbrevs list. |
| 43 if (const NaClBitstreamReader::BlockInfo *Info = |
| 44 BitStream->getBlockInfo(BlockID)) { |
| 45 for (size_t i = 0, e = Info->Abbrevs.size(); i != e; ++i) { |
| 46 CurAbbrevs.push_back(Info->Abbrevs[i]); |
| 47 CurAbbrevs.back()->addRef(); |
| 48 } |
| 49 } |
| 50 |
| 51 // Get the codesize of this block. |
| 52 CurCodeSize.IsFixed = true; |
| 53 CurCodeSize.NumBits = ReadVBR(naclbitc::CodeLenWidth); |
| 54 SkipToFourByteBoundary(); |
| 55 unsigned NumWords = Read(naclbitc::BlockSizeWidth); |
| 56 if (NumWordsP) *NumWordsP = NumWords; |
| 57 |
| 58 // Validate that this block is sane. |
| 59 if (CurCodeSize.NumBits == 0 || AtEndOfStream()) |
| 60 return true; |
| 61 |
| 62 return false; |
| 63 } |
| 64 |
| 65 void NaClBitstreamCursor::skipAbbreviatedField(const NaClBitCodeAbbrevOp &Op) { |
| 66 // Decode the value as we are commanded. |
| 67 switch (Op.getEncoding()) { |
| 68 case NaClBitCodeAbbrevOp::Literal: |
| 69 report_fatal_error("Not to be used with literals!"); |
| 70 case NaClBitCodeAbbrevOp::Fixed: |
| 71 (void)Read((unsigned)Op.getValue()); |
| 72 break; |
| 73 case NaClBitCodeAbbrevOp::VBR: |
| 74 (void)ReadVBR64((unsigned)Op.getValue()); |
| 75 break; |
| 76 case NaClBitCodeAbbrevOp::Array: |
| 77 report_fatal_error("Bad array abbreviation encoding!"); |
| 78 case NaClBitCodeAbbrevOp::Char6: |
| 79 (void)Read(6); |
| 80 break; |
| 81 } |
| 82 } |
| 83 |
| 84 /// skipRecord - Read the current record and discard it. |
| 85 void NaClBitstreamCursor::skipRecord(unsigned AbbrevID) { |
| 86 // Skip unabbreviated records by reading past their entries. |
| 87 if (AbbrevID == naclbitc::UNABBREV_RECORD) { |
| 88 unsigned Code = ReadVBR(6); |
| 89 (void)Code; |
| 90 unsigned NumElts = ReadVBR(6); |
| 91 for (unsigned i = 0; i != NumElts; ++i) |
| 92 (void)ReadVBR64(6); |
| 93 return; |
| 94 } |
| 95 |
| 96 const NaClBitCodeAbbrev *Abbv = getAbbrev(AbbrevID); |
| 97 |
| 98 for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) { |
| 99 const NaClBitCodeAbbrevOp &Op = Abbv->getOperandInfo(i); |
| 100 switch (Op.getEncoding()) { |
| 101 default: |
| 102 skipAbbreviatedField(Op); |
| 103 break; |
| 104 case NaClBitCodeAbbrevOp::Literal: |
| 105 break; |
| 106 case NaClBitCodeAbbrevOp::Array: { |
| 107 // Array case. Read the number of elements as a vbr6. |
| 108 unsigned NumElts = ReadVBR(6); |
| 109 |
| 110 // Get the element encoding. |
| 111 const NaClBitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i); |
| 112 |
| 113 // Read all the elements. |
| 114 for (; NumElts; --NumElts) |
| 115 skipAbbreviatedField(EltEnc); |
| 116 break; |
| 117 } |
| 118 } |
| 119 } |
| 120 } |
| 121 |
| 122 bool NaClBitstreamCursor::readRecordAbbrevField( |
| 123 const NaClBitCodeAbbrevOp &Op, uint64_t &Value) { |
| 124 switch (Op.getEncoding()) { |
| 125 case NaClBitCodeAbbrevOp::Literal: |
| 126 Value = Op.getValue(); |
| 127 break; |
| 128 case NaClBitCodeAbbrevOp::Array: |
| 129 // Returns number of elements in the array. |
| 130 Value = ReadVBR(6); |
| 131 return true; |
| 132 case NaClBitCodeAbbrevOp::Fixed: |
| 133 Value = Read((unsigned)Op.getValue()); |
| 134 break; |
| 135 case NaClBitCodeAbbrevOp::VBR: |
| 136 Value = ReadVBR64((unsigned)Op.getValue()); |
| 137 break; |
| 138 case NaClBitCodeAbbrevOp::Char6: |
| 139 Value = NaClBitCodeAbbrevOp::DecodeChar6(Read(6)); |
| 140 break; |
| 141 } |
| 142 return false; |
| 143 } |
| 144 |
| 145 uint64_t NaClBitstreamCursor::readArrayAbbreviatedField( |
| 146 const NaClBitCodeAbbrevOp &Op) { |
| 147 // Decode the value as we are commanded. |
| 148 switch (Op.getEncoding()) { |
| 149 case NaClBitCodeAbbrevOp::Literal: |
| 150 report_fatal_error("Not to be used with literals!"); |
| 151 case NaClBitCodeAbbrevOp::Fixed: |
| 152 return Read((unsigned)Op.getValue()); |
| 153 case NaClBitCodeAbbrevOp::VBR: |
| 154 return ReadVBR64((unsigned)Op.getValue()); |
| 155 case NaClBitCodeAbbrevOp::Array: |
| 156 report_fatal_error("Bad array abbreviation encoding!"); |
| 157 case NaClBitCodeAbbrevOp::Char6: |
| 158 return NaClBitCodeAbbrevOp::DecodeChar6(Read(6)); |
| 159 } |
| 160 llvm_unreachable("unhandled NaClBitCodeAbbrevOp encoding"); |
| 161 } |
| 162 |
| 163 void NaClBitstreamCursor::readArrayAbbrev( |
| 164 const NaClBitCodeAbbrevOp &Op, unsigned NumArrayElements, |
| 165 SmallVectorImpl<uint64_t> &Vals) { |
| 166 for (; NumArrayElements; --NumArrayElements) { |
| 167 Vals.push_back(readArrayAbbreviatedField(Op)); |
| 168 } |
| 169 } |
| 170 |
| 171 unsigned NaClBitstreamCursor::readRecord(unsigned AbbrevID, |
| 172 SmallVectorImpl<uint64_t> &Vals) { |
| 173 if (AbbrevID == naclbitc::UNABBREV_RECORD) { |
| 174 unsigned Code = ReadVBR(6); |
| 175 unsigned NumElts = ReadVBR(6); |
| 176 for (unsigned i = 0; i != NumElts; ++i) |
| 177 Vals.push_back(ReadVBR64(6)); |
| 178 return Code; |
| 179 } |
| 180 |
| 181 const NaClBitCodeAbbrev *Abbv = getAbbrev(AbbrevID); |
| 182 unsigned NumOperands = Abbv->getNumOperandInfos(); |
| 183 assert(NumOperands > 0 && "Too few operands for abbreviation!"); |
| 184 |
| 185 uint64_t Value; |
| 186 |
| 187 // Read code. |
| 188 unsigned Code; |
| 189 if (readRecordAbbrevField(Abbv->getOperandInfo(0), Value)) { |
| 190 // Array found, use to read all elements. |
| 191 assert(Value > 0 && "No code found for record!"); |
| 192 const NaClBitCodeAbbrevOp &Op = Abbv->getOperandInfo(1); |
| 193 Code = readArrayAbbreviatedField(Op); |
| 194 readArrayAbbrev(Op, Value - 1, Vals); |
| 195 return Code; |
| 196 } |
| 197 Code = Value; |
| 198 |
| 199 // Read arguments. |
| 200 for (unsigned i = 1; i != NumOperands; ++i) { |
| 201 if (readRecordAbbrevField(Abbv->getOperandInfo(i), Value)) { |
| 202 ++i; |
| 203 readArrayAbbrev(Abbv->getOperandInfo(i), Value, Vals); |
| 204 return Code; |
| 205 } |
| 206 Vals.push_back(Value); |
| 207 } |
| 208 return Code; |
| 209 } |
| 210 |
| 211 |
| 212 namespace { |
| 213 |
| 214 static NaClBitCodeAbbrevOp::Encoding getEncoding(uint64_t Encoding) { |
| 215 if (!NaClBitCodeAbbrevOp::isValidEncoding(Encoding)) { |
| 216 std::string Buffer; |
| 217 raw_string_ostream StrBuf(Buffer); |
| 218 StrBuf << "Invalid abbreviation encoding " << Encoding |
| 219 << "specified in bitcode file"; |
| 220 report_fatal_error(StrBuf.str()); |
| 221 } |
| 222 return NaClBitCodeAbbrevOp::Encoding(Encoding); |
| 223 } |
| 224 |
| 225 } // end of anonymous space. |
| 226 |
| 227 void NaClBitstreamCursor::ReadAbbrevRecord(bool IsLocal, |
| 228 NaClAbbrevListener *Listener) { |
| 229 NaClBitCodeAbbrev *Abbv = new NaClBitCodeAbbrev(); |
| 230 unsigned NumOpInfo = ReadVBR(5); |
| 231 if (Listener) Listener->Values.push_back(NumOpInfo); |
| 232 for (unsigned i = 0; i != NumOpInfo; ++i) { |
| 233 bool IsLiteral = Read(1) ? true : false; |
| 234 if (Listener) Listener->Values.push_back(IsLiteral); |
| 235 if (IsLiteral) { |
| 236 uint64_t Value = ReadVBR64(8); |
| 237 if (Listener) Listener->Values.push_back(Value); |
| 238 Abbv->Add(NaClBitCodeAbbrevOp(Value)); |
| 239 continue; |
| 240 } |
| 241 NaClBitCodeAbbrevOp::Encoding E = getEncoding(Read(3)); |
| 242 if (Listener) Listener->Values.push_back(E); |
| 243 if (NaClBitCodeAbbrevOp::hasValue(E)) { |
| 244 unsigned Data = ReadVBR64(5); |
| 245 if (Listener) Listener->Values.push_back(Data); |
| 246 |
| 247 // As a special case, handle fixed(0) (i.e., a fixed field with zero bits) |
| 248 // and vbr(0) as a literal zero. This is decoded the same way, and avoids |
| 249 // a slow path in Read() to have to handle reading zero bits. |
| 250 if ((E == NaClBitCodeAbbrevOp::Fixed || E == NaClBitCodeAbbrevOp::VBR) && |
| 251 Data == 0) { |
| 252 if (Listener) Listener->Values.push_back(0); |
| 253 Abbv->Add(NaClBitCodeAbbrevOp(0)); |
| 254 continue; |
| 255 } |
| 256 if (!NaClBitCodeAbbrevOp::isValid(E, Data)) { |
| 257 std::string Buffer; |
| 258 raw_string_ostream StrBuf(Buffer); |
| 259 StrBuf << "Invalid abbreviation encoding (" |
| 260 << NaClBitCodeAbbrevOp::getEncodingName(E) |
| 261 << ", " << Data << ")"; |
| 262 report_fatal_error(StrBuf.str()); |
| 263 } |
| 264 Abbv->Add(NaClBitCodeAbbrevOp(E, Data)); |
| 265 } else { |
| 266 if (!NaClBitCodeAbbrevOp::isValid(E)) { |
| 267 std::string Buffer; |
| 268 raw_string_ostream StrBuf(Buffer); |
| 269 StrBuf << "Invalid abbreviation encoding (" |
| 270 << NaClBitCodeAbbrevOp::getEncodingName(E) << ")"; |
| 271 report_fatal_error(StrBuf.str()); |
| 272 } |
| 273 Abbv->Add(NaClBitCodeAbbrevOp(E)); |
| 274 } |
| 275 } |
| 276 if (!Abbv->isValid()) |
| 277 report_fatal_error("Invalid abbreviation specified in bitcode file"); |
| 278 CurAbbrevs.push_back(Abbv); |
| 279 if (Listener) { |
| 280 Listener->ProcessAbbreviation(Abbv, IsLocal); |
| 281 // Reset record information of the listener. |
| 282 Listener->Values.clear(); |
| 283 Listener->StartBit = GetCurrentBitNo(); |
| 284 } |
| 285 } |
| 286 |
| 287 void NaClBitstreamCursor::SkipAbbrevRecord() { |
| 288 unsigned NumOpInfo = ReadVBR(5); |
| 289 for (unsigned i = 0; i != NumOpInfo; ++i) { |
| 290 bool IsLiteral = Read(1) ? true : false; |
| 291 if (IsLiteral) { |
| 292 ReadVBR64(8); |
| 293 continue; |
| 294 } |
| 295 NaClBitCodeAbbrevOp::Encoding E = getEncoding(Read(3)); |
| 296 if (NaClBitCodeAbbrevOp::hasValue(E)) { |
| 297 ReadVBR64(5); |
| 298 } |
| 299 } |
| 300 } |
| 301 |
| 302 bool NaClBitstreamCursor::ReadBlockInfoBlock(NaClAbbrevListener *Listener) { |
| 303 // If this is the second stream to get to the block info block, skip it. |
| 304 if (BitStream->hasBlockInfoRecords()) |
| 305 return SkipBlock(); |
| 306 |
| 307 unsigned NumWords; |
| 308 if (EnterSubBlock(naclbitc::BLOCKINFO_BLOCK_ID, &NumWords)) return true; |
| 309 |
| 310 if (Listener) Listener->BeginBlockInfoBlock(NumWords); |
| 311 |
| 312 NaClBitcodeRecordVector Record; |
| 313 NaClBitstreamReader::BlockInfo *CurBlockInfo = 0; |
| 314 |
| 315 // Read records of the BlockInfo block. |
| 316 while (1) { |
| 317 if (Listener) Listener->StartBit = GetCurrentBitNo(); |
| 318 NaClBitstreamEntry Entry = advance(AF_DontAutoprocessAbbrevs, Listener); |
| 319 |
| 320 switch (Entry.Kind) { |
| 321 case llvm::NaClBitstreamEntry::SubBlock: // PNaCl doesn't allow! |
| 322 case llvm::NaClBitstreamEntry::Error: |
| 323 return true; |
| 324 case llvm::NaClBitstreamEntry::EndBlock: |
| 325 if (Listener) Listener->EndBlockInfoBlock(); |
| 326 return false; |
| 327 case llvm::NaClBitstreamEntry::Record: |
| 328 // The interesting case. |
| 329 break; |
| 330 } |
| 331 |
| 332 // Read abbrev records, associate them with CurBID. |
| 333 if (Entry.ID == naclbitc::DEFINE_ABBREV) { |
| 334 if (!CurBlockInfo) return true; |
| 335 ReadAbbrevRecord(false, Listener); |
| 336 |
| 337 // ReadAbbrevRecord installs the abbrev in CurAbbrevs. Move it to the |
| 338 // appropriate BlockInfo. |
| 339 NaClBitCodeAbbrev *Abbv = CurAbbrevs.back(); |
| 340 CurAbbrevs.pop_back(); |
| 341 CurBlockInfo->Abbrevs.push_back(Abbv); |
| 342 continue; |
| 343 } |
| 344 |
| 345 // Read a record. |
| 346 Record.clear(); |
| 347 switch (readRecord(Entry.ID, Record)) { |
| 348 default: |
| 349 // No other records should be found! |
| 350 return true; |
| 351 case naclbitc::BLOCKINFO_CODE_SETBID: |
| 352 if (Record.size() < 1) return true; |
| 353 CurBlockInfo = &BitStream->getOrCreateBlockInfo((unsigned)Record[0]); |
| 354 if (Listener) { |
| 355 Listener->Values = Record; |
| 356 Listener->SetBID(); |
| 357 } |
| 358 break; |
| 359 } |
| 360 } |
| 361 } |
OLD | NEW |