| OLD | NEW |
| 1 //===- subzero/src/PNaClTranslator.cpp - ICE from bitcode -----------------===// | 1 //===- subzero/src/PNaClTranslator.cpp - ICE from bitcode -----------------===// |
| 2 // | 2 // |
| 3 // The Subzero Code Generator | 3 // The Subzero Code Generator |
| 4 // | 4 // |
| 5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
| 7 // | 7 // |
| 8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
| 9 // | 9 // |
| 10 // This file implements the PNaCl bitcode file to Ice, to machine code | 10 // This file implements the PNaCl bitcode file to Ice, to machine code |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 | 155 |
| 156 // Top-level class to read PNaCl bitcode files, and translate to ICE. | 156 // Top-level class to read PNaCl bitcode files, and translate to ICE. |
| 157 class TopLevelParser : public NaClBitcodeParser { | 157 class TopLevelParser : public NaClBitcodeParser { |
| 158 TopLevelParser() = delete; | 158 TopLevelParser() = delete; |
| 159 TopLevelParser(const TopLevelParser &) = delete; | 159 TopLevelParser(const TopLevelParser &) = delete; |
| 160 TopLevelParser &operator=(const TopLevelParser &) = delete; | 160 TopLevelParser &operator=(const TopLevelParser &) = delete; |
| 161 | 161 |
| 162 public: | 162 public: |
| 163 typedef std::vector<Ice::FunctionDeclaration *> FunctionDeclarationListType; | 163 typedef std::vector<Ice::FunctionDeclaration *> FunctionDeclarationListType; |
| 164 | 164 |
| 165 TopLevelParser(Ice::Translator &Translator, NaClBitcodeHeader &Header, | 165 TopLevelParser(Ice::Translator &Translator, NaClBitstreamCursor &Cursor, |
| 166 NaClBitstreamCursor &Cursor, Ice::ErrorCode &ErrorStatus) | 166 Ice::ErrorCode &ErrorStatus) |
| 167 : NaClBitcodeParser(Cursor), Translator(Translator), Header(Header), | 167 : NaClBitcodeParser(Cursor), Translator(Translator), |
| 168 ErrorStatus(ErrorStatus), NumErrors(0), NextDefiningFunctionID(0), | 168 ErrorStatus(ErrorStatus), NumErrors(0), NextDefiningFunctionID(0), |
| 169 VariableDeclarations(new Ice::VariableDeclarationList()), | 169 VariableDeclarations(new Ice::VariableDeclarationList()), |
| 170 BlockParser(nullptr), StubbedConstCallValue(nullptr) {} | 170 BlockParser(nullptr), StubbedConstCallValue(nullptr) {} |
| 171 | 171 |
| 172 ~TopLevelParser() override {} | 172 ~TopLevelParser() override {} |
| 173 | 173 |
| 174 Ice::Translator &getTranslator() const { return Translator; } | 174 Ice::Translator &getTranslator() const { return Translator; } |
| 175 | 175 |
| 176 void setBlockParser(BlockParserBaseClass *NewBlockParser) { | 176 void setBlockParser(BlockParserBaseClass *NewBlockParser) { |
| 177 BlockParser = NewBlockParser; | 177 BlockParser = NewBlockParser; |
| 178 } | 178 } |
| 179 | 179 |
| 180 // Generates error with given Message. Always returns true. | 180 /// Generates error with given Message, occurring at BitPosition |
| 181 bool Error(const std::string &Message) override; | 181 /// within the bitcode file. Always returns true. |
| 182 bool ErrorAt(uint64_t BitPosition, const std::string &Message) final; |
| 182 | 183 |
| 183 // Generates error message with respect to the current block parser. | 184 /// Generates error message with respect to the current block parser. |
| 184 bool BlockError(const std::string &Message); | 185 bool BlockError(const std::string &Message); |
| 185 | 186 |
| 186 /// Returns the number of errors found while parsing the bitcode | 187 /// Returns the number of errors found while parsing the bitcode |
| 187 /// file. | 188 /// file. |
| 188 unsigned getNumErrors() const { return NumErrors; } | 189 unsigned getNumErrors() const { return NumErrors; } |
| 189 | 190 |
| 190 /// Returns the number of bytes in the bitcode header. | |
| 191 size_t getHeaderSize() const { return Header.getHeaderSize(); } | |
| 192 | |
| 193 /// Changes the size of the type list to the given size. | 191 /// Changes the size of the type list to the given size. |
| 194 void resizeTypeIDValues(unsigned NewSize) { TypeIDValues.resize(NewSize); } | 192 void resizeTypeIDValues(unsigned NewSize) { TypeIDValues.resize(NewSize); } |
| 195 | 193 |
| 196 /// Returns true if generation of Subzero IR is disabled. | 194 /// Returns true if generation of Subzero IR is disabled. |
| 197 bool isIRGenerationDisabled() const { | 195 bool isIRGenerationDisabled() const { |
| 198 return Translator.getFlags().getDisableIRGeneration(); | 196 return Translator.getFlags().getDisableIRGeneration(); |
| 199 } | 197 } |
| 200 | 198 |
| 201 /// Returns the undefined type associated with type ID. | 199 /// Returns the undefined type associated with type ID. |
| 202 /// Note: Returns extended type ready to be defined. | 200 /// Note: Returns extended type ready to be defined. |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 | 234 |
| 237 /// Returns the value id that should be associated with the the | 235 /// Returns the value id that should be associated with the the |
| 238 /// current function block. Increments internal counters during call | 236 /// current function block. Increments internal counters during call |
| 239 /// so that it will be in correct position for next function block. | 237 /// so that it will be in correct position for next function block. |
| 240 size_t getNextFunctionBlockValueID() { | 238 size_t getNextFunctionBlockValueID() { |
| 241 size_t NumDeclaredFunctions = FunctionDeclarationList.size(); | 239 size_t NumDeclaredFunctions = FunctionDeclarationList.size(); |
| 242 while (NextDefiningFunctionID < NumDeclaredFunctions && | 240 while (NextDefiningFunctionID < NumDeclaredFunctions && |
| 243 FunctionDeclarationList[NextDefiningFunctionID]->isProto()) | 241 FunctionDeclarationList[NextDefiningFunctionID]->isProto()) |
| 244 ++NextDefiningFunctionID; | 242 ++NextDefiningFunctionID; |
| 245 if (NextDefiningFunctionID >= NumDeclaredFunctions) | 243 if (NextDefiningFunctionID >= NumDeclaredFunctions) |
| 246 report_fatal_error( | 244 Fatal("More function blocks than defined function addresses"); |
| 247 "More function blocks than defined function addresses"); | |
| 248 return NextDefiningFunctionID++; | 245 return NextDefiningFunctionID++; |
| 249 } | 246 } |
| 250 | 247 |
| 251 /// Returns the function associated with ID. | 248 /// Returns the function associated with ID. |
| 252 Ice::FunctionDeclaration *getFunctionByID(unsigned ID) { | 249 Ice::FunctionDeclaration *getFunctionByID(unsigned ID) { |
| 253 if (ID < FunctionDeclarationList.size()) | 250 if (ID < FunctionDeclarationList.size()) |
| 254 return FunctionDeclarationList[ID]; | 251 return FunctionDeclarationList[ID]; |
| 255 return reportGetFunctionByIDError(ID); | 252 return reportGetFunctionByIDError(ID); |
| 256 } | 253 } |
| 257 | 254 |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 354 // Before returning, check that ValidIDConstants has already been | 351 // Before returning, check that ValidIDConstants has already been |
| 355 // built. | 352 // built. |
| 356 assert(!VariableDeclarations || | 353 assert(!VariableDeclarations || |
| 357 VariableDeclarations->size() <= ValueIDConstants.size()); | 354 VariableDeclarations->size() <= ValueIDConstants.size()); |
| 358 return std::move(VariableDeclarations); | 355 return std::move(VariableDeclarations); |
| 359 } | 356 } |
| 360 | 357 |
| 361 private: | 358 private: |
| 362 // The translator associated with the parser. | 359 // The translator associated with the parser. |
| 363 Ice::Translator &Translator; | 360 Ice::Translator &Translator; |
| 364 // The bitcode header. | |
| 365 NaClBitcodeHeader &Header; | |
| 366 // The exit status that should be set to true if an error occurs. | 361 // The exit status that should be set to true if an error occurs. |
| 367 Ice::ErrorCode &ErrorStatus; | 362 Ice::ErrorCode &ErrorStatus; |
| 368 // The number of errors reported. | 363 // The number of errors reported. |
| 369 unsigned NumErrors; | 364 unsigned NumErrors; |
| 370 // The types associated with each type ID. | 365 // The types associated with each type ID. |
| 371 std::vector<ExtendedType> TypeIDValues; | 366 std::vector<ExtendedType> TypeIDValues; |
| 372 // The set of functions (prototype and defined). | 367 // The set of functions (prototype and defined). |
| 373 FunctionDeclarationListType FunctionDeclarationList; | 368 FunctionDeclarationListType FunctionDeclarationList; |
| 374 // The ID of the next possible defined function ID in | 369 // The ID of the next possible defined function ID in |
| 375 // FunctionDeclarationList. FunctionDeclarationList is filled | 370 // FunctionDeclarationList. FunctionDeclarationList is filled |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 498 | 493 |
| 499 // Reports that there is not global variable declaration for | 494 // Reports that there is not global variable declaration for |
| 500 // ID. Returns an error recovery value to use. | 495 // ID. Returns an error recovery value to use. |
| 501 Ice::VariableDeclaration *reportGetGlobalVariableByIDError(unsigned Index); | 496 Ice::VariableDeclaration *reportGetGlobalVariableByIDError(unsigned Index); |
| 502 | 497 |
| 503 // Reports that there is no corresponding ICE type for LLVMTy, and | 498 // Reports that there is no corresponding ICE type for LLVMTy, and |
| 504 // returns ICE::IceType_void. | 499 // returns ICE::IceType_void. |
| 505 Ice::Type convertToIceTypeError(Type *LLVMTy); | 500 Ice::Type convertToIceTypeError(Type *LLVMTy); |
| 506 }; | 501 }; |
| 507 | 502 |
| 508 bool TopLevelParser::Error(const std::string &Message) { | 503 bool TopLevelParser::ErrorAt(uint64_t Bit, const std::string &Message) { |
| 509 ErrorStatus.assign(Ice::EC_Bitcode); | 504 ErrorStatus.assign(Ice::EC_Bitcode); |
| 510 ++NumErrors; | 505 ++NumErrors; |
| 511 Ice::GlobalContext *Context = Translator.getContext(); | 506 Ice::GlobalContext *Context = Translator.getContext(); |
| 512 Ice::OstreamLocker L(Context); | 507 Ice::OstreamLocker L(Context); |
| 513 raw_ostream &OldErrStream = setErrStream(Context->getStrDump()); | 508 raw_ostream &OldErrStream = setErrStream(Context->getStrDump()); |
| 514 NaClBitcodeParser::Error(Message); | 509 NaClBitcodeParser::ErrorAt(Bit, Message); |
| 515 setErrStream(OldErrStream); | 510 setErrStream(OldErrStream); |
| 516 if (!Translator.getFlags().getAllowErrorRecovery()) | 511 if (!Translator.getFlags().getAllowErrorRecovery()) |
| 517 report_fatal_error("Unable to continue"); | 512 Fatal(); |
| 518 return true; | 513 return true; |
| 519 } | 514 } |
| 520 | 515 |
| 521 void TopLevelParser::reportBadTypeIDAs(unsigned ID, const ExtendedType *Ty, | 516 void TopLevelParser::reportBadTypeIDAs(unsigned ID, const ExtendedType *Ty, |
| 522 ExtendedType::TypeKind WantedType) { | 517 ExtendedType::TypeKind WantedType) { |
| 523 std::string Buffer; | 518 std::string Buffer; |
| 524 raw_string_ostream StrBuf(Buffer); | 519 raw_string_ostream StrBuf(Buffer); |
| 525 if (Ty == nullptr) { | 520 if (Ty == nullptr) { |
| 526 StrBuf << "Can't find extended type for type id: " << ID; | 521 StrBuf << "Can't find extended type for type id: " << ID; |
| 527 } else { | 522 } else { |
| 528 StrBuf << "Type id " << ID << " not " << WantedType << ". Found: " << *Ty; | 523 StrBuf << "Type id " << ID << " not " << WantedType << ". Found: " << *Ty; |
| 529 } | 524 } |
| 530 BlockError(StrBuf.str()); | 525 BlockError(StrBuf.str()); |
| 531 } | 526 } |
| 532 | 527 |
| 533 Ice::FunctionDeclaration * | 528 Ice::FunctionDeclaration * |
| 534 TopLevelParser::reportGetFunctionByIDError(unsigned ID) { | 529 TopLevelParser::reportGetFunctionByIDError(unsigned ID) { |
| 535 std::string Buffer; | 530 std::string Buffer; |
| 536 raw_string_ostream StrBuf(Buffer); | 531 raw_string_ostream StrBuf(Buffer); |
| 537 StrBuf << "Function index " << ID | 532 StrBuf << "Function index " << ID |
| 538 << " not allowed. Out of range. Must be less than " | 533 << " not allowed. Out of range. Must be less than " |
| 539 << FunctionDeclarationList.size(); | 534 << FunctionDeclarationList.size(); |
| 540 BlockError(StrBuf.str()); | 535 BlockError(StrBuf.str()); |
| 541 // TODO(kschimpf) Remove error recovery once implementation complete. | 536 // TODO(kschimpf) Remove error recovery once implementation complete. |
| 542 if (!FunctionDeclarationList.empty()) | 537 if (!FunctionDeclarationList.empty()) |
| 543 return FunctionDeclarationList[0]; | 538 return FunctionDeclarationList[0]; |
| 544 report_fatal_error("Unable to continue"); | 539 Fatal(); |
| 545 } | 540 } |
| 546 | 541 |
| 547 Ice::VariableDeclaration * | 542 Ice::VariableDeclaration * |
| 548 TopLevelParser::reportGetGlobalVariableByIDError(unsigned Index) { | 543 TopLevelParser::reportGetGlobalVariableByIDError(unsigned Index) { |
| 549 std::string Buffer; | 544 std::string Buffer; |
| 550 raw_string_ostream StrBuf(Buffer); | 545 raw_string_ostream StrBuf(Buffer); |
| 551 StrBuf << "Global index " << Index | 546 StrBuf << "Global index " << Index |
| 552 << " not allowed. Out of range. Must be less than " | 547 << " not allowed. Out of range. Must be less than " |
| 553 << VariableDeclarations->size(); | 548 << VariableDeclarations->size(); |
| 554 BlockError(StrBuf.str()); | 549 BlockError(StrBuf.str()); |
| 555 // TODO(kschimpf) Remove error recovery once implementation complete. | 550 // TODO(kschimpf) Remove error recovery once implementation complete. |
| 556 if (!VariableDeclarations->empty()) | 551 if (!VariableDeclarations->empty()) |
| 557 return VariableDeclarations->at(0); | 552 return VariableDeclarations->at(0); |
| 558 report_fatal_error("Unable to continue"); | 553 Fatal(); |
| 559 } | 554 } |
| 560 | 555 |
| 561 Ice::Type TopLevelParser::convertToIceTypeError(Type *LLVMTy) { | 556 Ice::Type TopLevelParser::convertToIceTypeError(Type *LLVMTy) { |
| 562 std::string Buffer; | 557 std::string Buffer; |
| 563 raw_string_ostream StrBuf(Buffer); | 558 raw_string_ostream StrBuf(Buffer); |
| 564 StrBuf << "Invalid LLVM type: " << *LLVMTy; | 559 StrBuf << "Invalid LLVM type: " << *LLVMTy; |
| 565 Error(StrBuf.str()); | 560 Error(StrBuf.str()); |
| 566 return Ice::IceType_void; | 561 return Ice::IceType_void; |
| 567 } | 562 } |
| 568 | 563 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 583 } | 578 } |
| 584 | 579 |
| 585 ~BlockParserBaseClass() override { Context->setBlockParser(nullptr); } | 580 ~BlockParserBaseClass() override { Context->setBlockParser(nullptr); } |
| 586 | 581 |
| 587 // Returns the printable name of the type of block being parsed. | 582 // Returns the printable name of the type of block being parsed. |
| 588 virtual const char *getBlockName() const { | 583 virtual const char *getBlockName() const { |
| 589 // If this class is used, it is parsing an unknown block. | 584 // If this class is used, it is parsing an unknown block. |
| 590 return "unknown"; | 585 return "unknown"; |
| 591 } | 586 } |
| 592 | 587 |
| 593 // Generates an error Message with the bit address prefixed to it. | 588 // Generates an error Message with the Bit address prefixed to it. |
| 594 bool Error(const std::string &Message) override; | 589 bool ErrorAt(uint64_t Bit, const std::string &Message) final; |
| 595 | 590 |
| 596 protected: | 591 protected: |
| 597 // The context parser that contains the decoded state. | 592 // The context parser that contains the decoded state. |
| 598 TopLevelParser *Context; | 593 TopLevelParser *Context; |
| 599 | 594 |
| 600 // Constructor for nested block parsers. | 595 // Constructor for nested block parsers. |
| 601 BlockParserBaseClass(unsigned BlockID, BlockParserBaseClass *EnclosingParser) | 596 BlockParserBaseClass(unsigned BlockID, BlockParserBaseClass *EnclosingParser) |
| 602 : NaClBitcodeParser(BlockID, EnclosingParser), | 597 : NaClBitcodeParser(BlockID, EnclosingParser), |
| 603 Context(EnclosingParser->Context) {} | 598 Context(EnclosingParser->Context) {} |
| 604 | 599 |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 671 }; | 666 }; |
| 672 | 667 |
| 673 bool TopLevelParser::BlockError(const std::string &Message) { | 668 bool TopLevelParser::BlockError(const std::string &Message) { |
| 674 if (BlockParser) | 669 if (BlockParser) |
| 675 return BlockParser->Error(Message); | 670 return BlockParser->Error(Message); |
| 676 else | 671 else |
| 677 return Error(Message); | 672 return Error(Message); |
| 678 } | 673 } |
| 679 | 674 |
| 680 // Generates an error Message with the bit address prefixed to it. | 675 // Generates an error Message with the bit address prefixed to it. |
| 681 bool BlockParserBaseClass::Error(const std::string &Message) { | 676 bool BlockParserBaseClass::ErrorAt(uint64_t Bit, const std::string &Message) { |
| 682 uint64_t Bit = Record.GetStartBit() + Context->getHeaderSize() * 8; | |
| 683 std::string Buffer; | 677 std::string Buffer; |
| 684 raw_string_ostream StrBuf(Buffer); | 678 raw_string_ostream StrBuf(Buffer); |
| 685 StrBuf << "(" << format("%" PRIu64 ":%u", (Bit / 8), | |
| 686 static_cast<unsigned>(Bit % 8)) << ") "; | |
| 687 // Note: If dump routines have been turned off, the error messages | 679 // Note: If dump routines have been turned off, the error messages |
| 688 // will not be readable. Hence, replace with simple error. We also | 680 // will not be readable. Hence, replace with simple error. We also |
| 689 // use the simple form for unit tests. | 681 // use the simple form for unit tests. |
| 690 if (getFlags().getGenerateUnitTestMessages()) { | 682 if (getFlags().getGenerateUnitTestMessages()) { |
| 691 StrBuf << "Invalid " << getBlockName() << " record: <" << Record.GetCode(); | 683 StrBuf << "Invalid " << getBlockName() << " record: <" << Record.GetCode(); |
| 692 for (const uint64_t Val : Record.GetValues()) { | 684 for (const uint64_t Val : Record.GetValues()) { |
| 693 StrBuf << " " << Val; | 685 StrBuf << " " << Val; |
| 694 } | 686 } |
| 695 StrBuf << ">"; | 687 StrBuf << ">"; |
| 696 } else { | 688 } else { |
| 697 StrBuf << Message; | 689 StrBuf << Message; |
| 698 } | 690 } |
| 699 return Context->Error(StrBuf.str()); | 691 return Context->ErrorAt(Bit, StrBuf.str()); |
| 700 } | 692 } |
| 701 | 693 |
| 702 void BlockParserBaseClass::ReportRecordSizeError(unsigned ExpectedSize, | 694 void BlockParserBaseClass::ReportRecordSizeError(unsigned ExpectedSize, |
| 703 const char *RecordName, | 695 const char *RecordName, |
| 704 const char *ContextMessage) { | 696 const char *ContextMessage) { |
| 705 std::string Buffer; | 697 std::string Buffer; |
| 706 raw_string_ostream StrBuf(Buffer); | 698 raw_string_ostream StrBuf(Buffer); |
| 707 const char *BlockName = getBlockName(); | 699 const char *BlockName = getBlockName(); |
| 708 const char FirstChar = toupper(*BlockName); | 700 const char FirstChar = toupper(*BlockName); |
| 709 StrBuf << FirstChar << (BlockName + 1) << " " << RecordName | 701 StrBuf << FirstChar << (BlockName + 1) << " " << RecordName |
| (...skipping 518 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1228 // Returns the value referenced by the given value Index. | 1220 // Returns the value referenced by the given value Index. |
| 1229 Ice::Operand *getOperand(uint32_t Index) { | 1221 Ice::Operand *getOperand(uint32_t Index) { |
| 1230 if (Index < CachedNumGlobalValueIDs) { | 1222 if (Index < CachedNumGlobalValueIDs) { |
| 1231 return Context->getGlobalConstantByID(Index); | 1223 return Context->getGlobalConstantByID(Index); |
| 1232 } | 1224 } |
| 1233 uint32_t LocalIndex = Index - CachedNumGlobalValueIDs; | 1225 uint32_t LocalIndex = Index - CachedNumGlobalValueIDs; |
| 1234 if (LocalIndex >= LocalOperands.size()) { | 1226 if (LocalIndex >= LocalOperands.size()) { |
| 1235 std::string Buffer; | 1227 std::string Buffer; |
| 1236 raw_string_ostream StrBuf(Buffer); | 1228 raw_string_ostream StrBuf(Buffer); |
| 1237 StrBuf << "Value index " << Index << " not defined!"; | 1229 StrBuf << "Value index " << Index << " not defined!"; |
| 1238 Error(StrBuf.str()); | 1230 Fatal(StrBuf.str()); |
| 1239 report_fatal_error("Unable to continue"); | |
| 1240 } | 1231 } |
| 1241 Ice::Operand *Op = LocalOperands[LocalIndex]; | 1232 Ice::Operand *Op = LocalOperands[LocalIndex]; |
| 1242 if (Op == nullptr) { | 1233 if (Op == nullptr) { |
| 1243 if (isIRGenerationDisabled()) | 1234 if (isIRGenerationDisabled()) |
| 1244 return nullptr; | 1235 return nullptr; |
| 1245 std::string Buffer; | 1236 std::string Buffer; |
| 1246 raw_string_ostream StrBuf(Buffer); | 1237 raw_string_ostream StrBuf(Buffer); |
| 1247 StrBuf << "Value index " << Index << " not defined!"; | 1238 StrBuf << "Value index " << Index << " not defined!"; |
| 1248 Error(StrBuf.str()); | 1239 Fatal(StrBuf.str()); |
| 1249 report_fatal_error("Unable to continue"); | |
| 1250 } | 1240 } |
| 1251 return Op; | 1241 return Op; |
| 1252 } | 1242 } |
| 1253 | 1243 |
| 1254 private: | 1244 private: |
| 1255 Ice::TimerMarker Timer; | 1245 Ice::TimerMarker Timer; |
| 1256 // The corresponding ICE function defined by the function block. | 1246 // The corresponding ICE function defined by the function block. |
| 1257 std::unique_ptr<Ice::Cfg> Func; | 1247 std::unique_ptr<Ice::Cfg> Func; |
| 1258 // The index to the current basic block being built. | 1248 // The index to the current basic block being built. |
| 1259 uint32_t CurrentBbIndex; | 1249 uint32_t CurrentBbIndex; |
| (...skipping 1758 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3018 void PNaClTranslator::translateBuffer(const std::string &IRFilename, | 3008 void PNaClTranslator::translateBuffer(const std::string &IRFilename, |
| 3019 MemoryBuffer *MemBuf) { | 3009 MemoryBuffer *MemBuf) { |
| 3020 if (MemBuf->getBufferSize() % 4 != 0) { | 3010 if (MemBuf->getBufferSize() % 4 != 0) { |
| 3021 errs() << IRFilename | 3011 errs() << IRFilename |
| 3022 << ": Bitcode stream should be a multiple of 4 bytes in length.\n"; | 3012 << ": Bitcode stream should be a multiple of 4 bytes in length.\n"; |
| 3023 ErrorStatus.assign(EC_Bitcode); | 3013 ErrorStatus.assign(EC_Bitcode); |
| 3024 return; | 3014 return; |
| 3025 } | 3015 } |
| 3026 | 3016 |
| 3027 const unsigned char *BufPtr = (const unsigned char *)MemBuf->getBufferStart(); | 3017 const unsigned char *BufPtr = (const unsigned char *)MemBuf->getBufferStart(); |
| 3018 const unsigned char *HeaderPtr = BufPtr; |
| 3028 const unsigned char *EndBufPtr = BufPtr + MemBuf->getBufferSize(); | 3019 const unsigned char *EndBufPtr = BufPtr + MemBuf->getBufferSize(); |
| 3029 | 3020 |
| 3030 // Read header and verify it is good. | 3021 // Read header and verify it is good. |
| 3031 NaClBitcodeHeader Header; | 3022 NaClBitcodeHeader Header; |
| 3032 if (Header.Read(BufPtr, EndBufPtr) || !Header.IsSupported()) { | 3023 if (Header.Read(HeaderPtr, EndBufPtr) || !Header.IsSupported()) { |
| 3033 errs() << "Invalid PNaCl bitcode header.\n"; | 3024 errs() << "Invalid PNaCl bitcode header.\n"; |
| 3034 ErrorStatus.assign(EC_Bitcode); | 3025 ErrorStatus.assign(EC_Bitcode); |
| 3035 return; | 3026 return; |
| 3036 } | 3027 } |
| 3037 | 3028 |
| 3038 // Create a bitstream reader to read the bitcode file. | 3029 // Create a bitstream reader to read the bitcode file. |
| 3039 NaClBitstreamReader InputStreamFile(BufPtr, EndBufPtr); | 3030 NaClBitstreamReader InputStreamFile(BufPtr, EndBufPtr, |
| 3031 Header.getHeaderSize()); |
| 3040 NaClBitstreamCursor InputStream(InputStreamFile); | 3032 NaClBitstreamCursor InputStream(InputStreamFile); |
| 3041 | 3033 |
| 3042 TopLevelParser Parser(*this, Header, InputStream, ErrorStatus); | 3034 TopLevelParser Parser(*this, InputStream, ErrorStatus); |
| 3043 int TopLevelBlocks = 0; | 3035 int TopLevelBlocks = 0; |
| 3044 while (!InputStream.AtEndOfStream()) { | 3036 while (!InputStream.AtEndOfStream()) { |
| 3045 if (Parser.Parse()) { | 3037 if (Parser.Parse()) { |
| 3046 ErrorStatus.assign(EC_Bitcode); | 3038 ErrorStatus.assign(EC_Bitcode); |
| 3047 return; | 3039 return; |
| 3048 } | 3040 } |
| 3049 ++TopLevelBlocks; | 3041 ++TopLevelBlocks; |
| 3050 } | 3042 } |
| 3051 | 3043 |
| 3052 if (TopLevelBlocks != 1) { | 3044 if (TopLevelBlocks != 1) { |
| 3053 errs() << IRFilename | 3045 errs() << IRFilename |
| 3054 << ": Contains more than one module. Found: " << TopLevelBlocks | 3046 << ": Contains more than one module. Found: " << TopLevelBlocks |
| 3055 << "\n"; | 3047 << "\n"; |
| 3056 ErrorStatus.assign(EC_Bitcode); | 3048 ErrorStatus.assign(EC_Bitcode); |
| 3057 } | 3049 } |
| 3058 } | 3050 } |
| 3059 | 3051 |
| 3060 } // end of namespace Ice | 3052 } // end of namespace Ice |
| OLD | NEW |