Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(325)

Side by Side Diff: src/PNaClTranslator.cpp

Issue 735513002: Create local config file for subzero reader tests. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-subzero.git@master
Patch Set: Fix nit. Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | tests_lit/parse_errs/insertextract-err.ll » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 break; 154 break;
155 } 155 }
156 case FuncSig: { 156 case FuncSig: {
157 Stream << " " << Signature; 157 Stream << " " << Signature;
158 } 158 }
159 default: 159 default:
160 break; 160 break;
161 } 161 }
162 } 162 }
163 163
164 class BlockParserBaseClass;
165
164 // Top-level class to read PNaCl bitcode files, and translate to ICE. 166 // Top-level class to read PNaCl bitcode files, and translate to ICE.
165 class TopLevelParser : public NaClBitcodeParser { 167 class TopLevelParser : public NaClBitcodeParser {
166 TopLevelParser(const TopLevelParser &) = delete; 168 TopLevelParser(const TopLevelParser &) = delete;
167 TopLevelParser &operator=(const TopLevelParser &) = delete; 169 TopLevelParser &operator=(const TopLevelParser &) = delete;
168 170
169 public: 171 public:
170 typedef std::vector<Ice::FunctionDeclaration *> FunctionDeclarationListType; 172 typedef std::vector<Ice::FunctionDeclaration *> FunctionDeclarationListType;
171 173
172 TopLevelParser(Ice::Translator &Translator, const std::string &InputName, 174 TopLevelParser(Ice::Translator &Translator, const std::string &InputName,
173 NaClBitcodeHeader &Header, NaClBitstreamCursor &Cursor, 175 NaClBitcodeHeader &Header, NaClBitstreamCursor &Cursor,
174 bool &ErrorStatus) 176 bool &ErrorStatus)
175 : NaClBitcodeParser(Cursor), Translator(Translator), 177 : NaClBitcodeParser(Cursor), Translator(Translator),
176 Mod(new Module(InputName, getGlobalContext())), DL(PNaClDataLayout), 178 Mod(new Module(InputName, getGlobalContext())), DL(PNaClDataLayout),
177 Header(Header), TypeConverter(Mod->getContext()), 179 Header(Header), TypeConverter(Mod->getContext()),
178 ErrorStatus(ErrorStatus), NumErrors(0), NumFunctionIds(0), 180 ErrorStatus(ErrorStatus), NumErrors(0), NumFunctionIds(0),
179 NumFunctionBlocks(0) { 181 NumFunctionBlocks(0), BlockParser(nullptr) {
180 Mod->setDataLayout(PNaClDataLayout); 182 Mod->setDataLayout(PNaClDataLayout);
181 setErrStream(Translator.getContext()->getStrDump()); 183 setErrStream(Translator.getContext()->getStrDump());
182 } 184 }
183 185
184 ~TopLevelParser() override {} 186 ~TopLevelParser() override {}
185 187
186 Ice::Translator &getTranslator() { return Translator; } 188 Ice::Translator &getTranslator() { return Translator; }
187 189
190 void setBlockParser(BlockParserBaseClass *NewBlockParser) {
191 BlockParser = NewBlockParser;
192 }
193
188 // Generates error with given Message. Always returns true. 194 // Generates error with given Message. Always returns true.
189 bool Error(const std::string &Message) override { 195 bool Error(const std::string &Message) override;
190 ErrorStatus = true; 196
191 ++NumErrors; 197 // Generates error message with respect to the current block parser.
192 NaClBitcodeParser::Error(Message); 198 bool BlockError(const std::string &Message);
193 if (!AllowErrorRecovery)
194 report_fatal_error("Unable to continue");
195 return true;
196 }
197 199
198 /// Returns the number of errors found while parsing the bitcode 200 /// Returns the number of errors found while parsing the bitcode
199 /// file. 201 /// file.
200 unsigned getNumErrors() const { return NumErrors; } 202 unsigned getNumErrors() const { return NumErrors; }
201 203
202 /// Returns the LLVM module associated with the translation. 204 /// Returns the LLVM module associated with the translation.
203 Module *getModule() const { return Mod.get(); } 205 Module *getModule() const { return Mod.get(); }
204 206
205 const DataLayout &getDataLayout() const { return DL; } 207 const DataLayout &getDataLayout() const { return DL; }
206 208
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
315 } 317 }
316 std::string Name; 318 std::string Name;
317 bool SuppressMangling; 319 bool SuppressMangling;
318 if (Decl) { 320 if (Decl) {
319 Name = Decl->getName(); 321 Name = Decl->getName();
320 SuppressMangling = Decl->getSuppressMangling(); 322 SuppressMangling = Decl->getSuppressMangling();
321 } else { 323 } else {
322 std::string Buffer; 324 std::string Buffer;
323 raw_string_ostream StrBuf(Buffer); 325 raw_string_ostream StrBuf(Buffer);
324 StrBuf << "Reference to global not defined: " << ID; 326 StrBuf << "Reference to global not defined: " << ID;
325 Error(StrBuf.str()); 327 BlockError(StrBuf.str());
326 // TODO(kschimpf) Remove error recovery once implementation complete. 328 // TODO(kschimpf) Remove error recovery once implementation complete.
327 Name = "??"; 329 Name = "??";
328 SuppressMangling = false; 330 SuppressMangling = false;
329 } 331 }
330 const Ice::RelocOffsetT Offset = 0; 332 const Ice::RelocOffsetT Offset = 0;
331 C = getTranslator().getContext()->getConstantSym(Offset, Name, 333 C = getTranslator().getContext()->getConstantSym(Offset, Name,
332 SuppressMangling); 334 SuppressMangling);
333 ValueIDConstants[ID] = C; 335 ValueIDConstants[ID] = C;
334 return C; 336 return C;
335 } 337 }
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
425 unsigned NumFunctionIds; 427 unsigned NumFunctionIds;
426 // The number of function blocks (processed so far). 428 // The number of function blocks (processed so far).
427 unsigned NumFunctionBlocks; 429 unsigned NumFunctionBlocks;
428 // The list of function declaration IDs (in the order found) that 430 // The list of function declaration IDs (in the order found) that
429 // aren't just proto declarations. 431 // aren't just proto declarations.
430 // TODO(kschimpf): Instead of using this list, just use 432 // TODO(kschimpf): Instead of using this list, just use
431 // FunctionDeclarationList, and the isProto member function. 433 // FunctionDeclarationList, and the isProto member function.
432 std::vector<unsigned> DefiningFunctionDeclarationsList; 434 std::vector<unsigned> DefiningFunctionDeclarationsList;
433 // Error recovery value to use when getFuncSigTypeByID fails. 435 // Error recovery value to use when getFuncSigTypeByID fails.
434 Ice::FuncSigType UndefinedFuncSigType; 436 Ice::FuncSigType UndefinedFuncSigType;
437 // The block parser currently being applied. Used for error
438 // reporting.
439 BlockParserBaseClass *BlockParser;
435 440
436 bool ParseBlock(unsigned BlockID) override; 441 bool ParseBlock(unsigned BlockID) override;
437 442
438 // Gets extended type associated with the given index, assuming the 443 // Gets extended type associated with the given index, assuming the
439 // extended type is of the WantedKind. Generates error message if 444 // extended type is of the WantedKind. Generates error message if
440 // corresponding extended type of WantedKind can't be found, and 445 // corresponding extended type of WantedKind can't be found, and
441 // returns nullptr. 446 // returns nullptr.
442 ExtendedType *getTypeByIDAsKind(unsigned ID, 447 ExtendedType *getTypeByIDAsKind(unsigned ID,
443 ExtendedType::TypeKind WantedKind) { 448 ExtendedType::TypeKind WantedKind) {
444 ExtendedType *Ty = nullptr; 449 ExtendedType *Ty = nullptr;
(...skipping 17 matching lines...) Expand all
462 467
463 // Reports that there is not global variable declaration for 468 // Reports that there is not global variable declaration for
464 // ID. Returns an error recovery value to use. 469 // ID. Returns an error recovery value to use.
465 Ice::VariableDeclaration *reportGetGlobalVariableByIDError(unsigned Index); 470 Ice::VariableDeclaration *reportGetGlobalVariableByIDError(unsigned Index);
466 471
467 // Reports that there is no corresponding ICE type for LLVMTy, and 472 // Reports that there is no corresponding ICE type for LLVMTy, and
468 // returns ICE::IceType_void. 473 // returns ICE::IceType_void.
469 Ice::Type convertToIceTypeError(Type *LLVMTy); 474 Ice::Type convertToIceTypeError(Type *LLVMTy);
470 }; 475 };
471 476
477 bool TopLevelParser::Error(const std::string &Message) {
478 ErrorStatus = true;
479 ++NumErrors;
480 NaClBitcodeParser::Error(Message);
481 if (!AllowErrorRecovery)
482 report_fatal_error("Unable to continue");
483 return true;
484 }
485
472 void TopLevelParser::reportBadTypeIDAs(unsigned ID, const ExtendedType *Ty, 486 void TopLevelParser::reportBadTypeIDAs(unsigned ID, const ExtendedType *Ty,
473 ExtendedType::TypeKind WantedType) { 487 ExtendedType::TypeKind WantedType) {
474 std::string Buffer; 488 std::string Buffer;
475 raw_string_ostream StrBuf(Buffer); 489 raw_string_ostream StrBuf(Buffer);
476 if (Ty == nullptr) { 490 if (Ty == nullptr) {
477 StrBuf << "Can't find extended type for type id: " << ID; 491 StrBuf << "Can't find extended type for type id: " << ID;
478 } else { 492 } else {
479 StrBuf << "Type id " << ID << " not " << WantedType << ". Found: " << *Ty; 493 StrBuf << "Type id " << ID << " not " << WantedType << ". Found: " << *Ty;
480 } 494 }
481 Error(StrBuf.str()); 495 BlockError(StrBuf.str());
482 } 496 }
483 497
484 Ice::FunctionDeclaration * 498 Ice::FunctionDeclaration *
485 TopLevelParser::reportGetFunctionByIDError(unsigned ID) { 499 TopLevelParser::reportGetFunctionByIDError(unsigned ID) {
486 std::string Buffer; 500 std::string Buffer;
487 raw_string_ostream StrBuf(Buffer); 501 raw_string_ostream StrBuf(Buffer);
488 StrBuf << "Function index " << ID 502 StrBuf << "Function index " << ID
489 << " not allowed. Out of range. Must be less than " 503 << " not allowed. Out of range. Must be less than "
490 << FunctionDeclarationList.size(); 504 << FunctionDeclarationList.size();
491 Error(StrBuf.str()); 505 BlockError(StrBuf.str());
492 // TODO(kschimpf) Remove error recovery once implementation complete. 506 // TODO(kschimpf) Remove error recovery once implementation complete.
493 if (!FunctionDeclarationList.empty()) 507 if (!FunctionDeclarationList.empty())
494 return FunctionDeclarationList[0]; 508 return FunctionDeclarationList[0];
495 report_fatal_error("Unable to continue"); 509 report_fatal_error("Unable to continue");
496 } 510 }
497 511
498 Ice::VariableDeclaration * 512 Ice::VariableDeclaration *
499 TopLevelParser::reportGetGlobalVariableByIDError(unsigned Index) { 513 TopLevelParser::reportGetGlobalVariableByIDError(unsigned Index) {
500 std::string Buffer; 514 std::string Buffer;
501 raw_string_ostream StrBuf(Buffer); 515 raw_string_ostream StrBuf(Buffer);
502 StrBuf << "Global index " << Index 516 StrBuf << "Global index " << Index
503 << " not allowed. Out of range. Must be less than " 517 << " not allowed. Out of range. Must be less than "
504 << VariableDeclarations.size(); 518 << VariableDeclarations.size();
505 Error(StrBuf.str()); 519 BlockError(StrBuf.str());
506 // TODO(kschimpf) Remove error recovery once implementation complete. 520 // TODO(kschimpf) Remove error recovery once implementation complete.
507 if (!VariableDeclarations.empty()) 521 if (!VariableDeclarations.empty())
508 return VariableDeclarations[0]; 522 return VariableDeclarations[0];
509 report_fatal_error("Unable to continue"); 523 report_fatal_error("Unable to continue");
510 } 524 }
511 525
512 Ice::Type TopLevelParser::convertToIceTypeError(Type *LLVMTy) { 526 Ice::Type TopLevelParser::convertToIceTypeError(Type *LLVMTy) {
513 std::string Buffer; 527 std::string Buffer;
514 raw_string_ostream StrBuf(Buffer); 528 raw_string_ostream StrBuf(Buffer);
515 StrBuf << "Invalid LLVM type: " << *LLVMTy; 529 StrBuf << "Invalid LLVM type: " << *LLVMTy;
516 Error(StrBuf.str()); 530 Error(StrBuf.str());
517 return Ice::IceType_void; 531 return Ice::IceType_void;
518 } 532 }
519 533
520 // Base class for parsing blocks within the bitcode file. Note: 534 // Base class for parsing blocks within the bitcode file. Note:
521 // Because this is the base class of block parsers, we generate error 535 // Because this is the base class of block parsers, we generate error
522 // messages if ParseBlock or ParseRecord is not overridden in derived 536 // messages if ParseBlock or ParseRecord is not overridden in derived
523 // classes. 537 // classes.
524 class BlockParserBaseClass : public NaClBitcodeParser { 538 class BlockParserBaseClass : public NaClBitcodeParser {
539 BlockParserBaseClass(const BlockParserBaseClass &) = delete;
540 BlockParserBaseClass &operator=(const BlockParserBaseClass &) = delete;
541
525 public: 542 public:
526 // Constructor for the top-level module block parser. 543 // Constructor for the top-level module block parser.
527 BlockParserBaseClass(unsigned BlockID, TopLevelParser *Context) 544 BlockParserBaseClass(unsigned BlockID, TopLevelParser *Context)
528 : NaClBitcodeParser(BlockID, Context), Context(Context) {} 545 : NaClBitcodeParser(BlockID, Context), Context(Context) {
546 Context->setBlockParser(this);
547 }
529 548
530 ~BlockParserBaseClass() override {} 549 ~BlockParserBaseClass() override { Context->setBlockParser(nullptr); }
550
551 // Returns the printable name of the type of block being parsed.
552 virtual const char *getBlockName() const {
553 // If this class is used, it is parsing an unknown block.
554 return "unknown";
555 }
556
557 // Generates an error Message with the bit address prefixed to it.
558 bool Error(const std::string &Message) override;
531 559
532 protected: 560 protected:
533 // The context parser that contains the decoded state. 561 // The context parser that contains the decoded state.
534 TopLevelParser *Context; 562 TopLevelParser *Context;
535 563
536 // Constructor for nested block parsers. 564 // Constructor for nested block parsers.
537 BlockParserBaseClass(unsigned BlockID, BlockParserBaseClass *EnclosingParser) 565 BlockParserBaseClass(unsigned BlockID, BlockParserBaseClass *EnclosingParser)
538 : NaClBitcodeParser(BlockID, EnclosingParser), 566 : NaClBitcodeParser(BlockID, EnclosingParser),
539 Context(EnclosingParser->Context) {} 567 Context(EnclosingParser->Context) {}
540 568
541 // Gets the translator associated with the bitcode parser. 569 // Gets the translator associated with the bitcode parser.
542 Ice::Translator &getTranslator() const { return Context->getTranslator(); } 570 Ice::Translator &getTranslator() const { return Context->getTranslator(); }
543 571
544 const Ice::ClFlags &getFlags() const { return getTranslator().getFlags(); } 572 const Ice::ClFlags &getFlags() const { return getTranslator().getFlags(); }
545 573
546 bool isIRGenerationDisabled() const { 574 bool isIRGenerationDisabled() const {
547 return ALLOW_DISABLE_IR_GEN ? getTranslator().getFlags().DisableIRGeneration 575 return ALLOW_DISABLE_IR_GEN ? getTranslator().getFlags().DisableIRGeneration
548 : false; 576 : false;
549 } 577 }
550 578
551 // Generates an error Message with the bit address prefixed to it.
552 bool Error(const std::string &Message) override {
553 uint64_t Bit = Record.GetStartBit() + Context->getHeaderSize() * 8;
554 std::string Buffer;
555 raw_string_ostream StrBuf(Buffer);
556 StrBuf << "(" << format("%" PRIu64 ":%u", (Bit / 8),
557 static_cast<unsigned>(Bit % 8)) << ") ";
558 // Note: If dump routines have been turned off, the error messages
559 // will not be readable. Hence, replace with simple error.
560 if (ALLOW_DUMP)
561 StrBuf << Message;
562 else
563 StrBuf << "Invalid input record";
564 return Context->Error(StrBuf.str());
565 }
566
567 // Default implementation. Reports that block is unknown and skips 579 // Default implementation. Reports that block is unknown and skips
568 // its contents. 580 // its contents.
569 bool ParseBlock(unsigned BlockID) override; 581 bool ParseBlock(unsigned BlockID) override;
570 582
571 // Default implementation. Reports that the record is not 583 // Default implementation. Reports that the record is not
572 // understood. 584 // understood.
573 void ProcessRecord() override; 585 void ProcessRecord() override;
574 586
575 // Checks if the size of the record is Size. Return true if valid. 587 // Checks if the size of the record is Size. Return true if valid.
576 // Otherwise generates an error and returns false. 588 // Otherwise generates an error and returns false.
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
616 private: 628 private:
617 /// Generates a record size error. ExpectedSize is the number 629 /// Generates a record size error. ExpectedSize is the number
618 /// of elements expected. RecordName is the name of the kind of 630 /// of elements expected. RecordName is the name of the kind of
619 /// record that has incorrect size. ContextMessage (if not nullptr) 631 /// record that has incorrect size. ContextMessage (if not nullptr)
620 /// is appended to "record expects" to describe how ExpectedSize 632 /// is appended to "record expects" to describe how ExpectedSize
621 /// should be interpreted. 633 /// should be interpreted.
622 void ReportRecordSizeError(unsigned ExpectedSize, const char *RecordName, 634 void ReportRecordSizeError(unsigned ExpectedSize, const char *RecordName,
623 const char *ContextMessage); 635 const char *ContextMessage);
624 }; 636 };
625 637
638 bool TopLevelParser::BlockError(const std::string &Message) {
639 if (BlockParser)
640 return BlockParser->Error(Message);
641 else
642 return Error(Message);
643 }
644
645 // Generates an error Message with the bit address prefixed to it.
646 bool BlockParserBaseClass::Error(const std::string &Message) {
647 uint64_t Bit = Record.GetStartBit() + Context->getHeaderSize() * 8;
648 std::string Buffer;
649 raw_string_ostream StrBuf(Buffer);
650 StrBuf << "(" << format("%" PRIu64 ":%u", (Bit / 8),
651 static_cast<unsigned>(Bit % 8)) << ") ";
652 // Note: If dump routines have been turned off, the error messages
653 // will not be readable. Hence, replace with simple error.
654 if (ALLOW_DUMP)
655 StrBuf << Message;
656 else {
657 StrBuf << "Invalid " << getBlockName() << " record: <" << Record.GetCode();
658 for (const uint64_t Val : Record.GetValues()) {
659 StrBuf << " " << Val;
660 }
661 StrBuf << ">";
662 }
663 return Context->Error(StrBuf.str());
664 }
665
626 void BlockParserBaseClass::ReportRecordSizeError(unsigned ExpectedSize, 666 void BlockParserBaseClass::ReportRecordSizeError(unsigned ExpectedSize,
627 const char *RecordName, 667 const char *RecordName,
628 const char *ContextMessage) { 668 const char *ContextMessage) {
629 std::string Buffer; 669 std::string Buffer;
630 raw_string_ostream StrBuf(Buffer); 670 raw_string_ostream StrBuf(Buffer);
631 StrBuf << RecordName << " record expects"; 671 const char *BlockName = getBlockName();
672 const char FirstChar = toupper(*BlockName);
673 StrBuf << FirstChar << (BlockName + 1) << " " << RecordName
674 << " record expects";
632 if (ContextMessage) 675 if (ContextMessage)
633 StrBuf << " " << ContextMessage; 676 StrBuf << " " << ContextMessage;
634 StrBuf << " " << ExpectedSize << " argument"; 677 StrBuf << " " << ExpectedSize << " argument";
635 if (ExpectedSize > 1) 678 if (ExpectedSize > 1)
636 StrBuf << "s"; 679 StrBuf << "s";
637 StrBuf << ". Found: " << Record.GetValues().size(); 680 StrBuf << ". Found: " << Record.GetValues().size();
638 Error(StrBuf.str()); 681 Error(StrBuf.str());
639 } 682 }
640 683
641 bool BlockParserBaseClass::ParseBlock(unsigned BlockID) { 684 bool BlockParserBaseClass::ParseBlock(unsigned BlockID) {
642 // If called, derived class doesn't know how to handle block. 685 // If called, derived class doesn't know how to handle block.
643 // Report error and skip. 686 // Report error and skip.
644 std::string Buffer; 687 std::string Buffer;
645 raw_string_ostream StrBuf(Buffer); 688 raw_string_ostream StrBuf(Buffer);
646 StrBuf << "Don't know how to parse block id: " << BlockID; 689 StrBuf << "Don't know how to parse block id: " << BlockID;
647 Error(StrBuf.str()); 690 Error(StrBuf.str());
648 // TODO(kschimpf) Remove error recovery once implementation complete. 691 // TODO(kschimpf) Remove error recovery once implementation complete.
649 SkipBlock(); 692 SkipBlock();
650 return false; 693 return false;
651 } 694 }
652 695
653 void BlockParserBaseClass::ProcessRecord() { 696 void BlockParserBaseClass::ProcessRecord() {
654 // If called, derived class doesn't know how to handle. 697 // If called, derived class doesn't know how to handle.
655 std::string Buffer; 698 std::string Buffer;
656 raw_string_ostream StrBuf(Buffer); 699 raw_string_ostream StrBuf(Buffer);
657 StrBuf << "Don't know how to process record: " << Record; 700 StrBuf << "Don't know how to process " << getBlockName()
701 << " record:" << Record;
658 Error(StrBuf.str()); 702 Error(StrBuf.str());
659 } 703 }
660 704
661 // Class to parse a types block. 705 // Class to parse a types block.
662 class TypesParser : public BlockParserBaseClass { 706 class TypesParser : public BlockParserBaseClass {
663 public: 707 public:
664 TypesParser(unsigned BlockID, BlockParserBaseClass *EnclosingParser) 708 TypesParser(unsigned BlockID, BlockParserBaseClass *EnclosingParser)
665 : BlockParserBaseClass(BlockID, EnclosingParser), 709 : BlockParserBaseClass(BlockID, EnclosingParser),
666 Timer(Ice::TimerStack::TT_parseTypes, getTranslator().getContext()), 710 Timer(Ice::TimerStack::TT_parseTypes, getTranslator().getContext()),
667 NextTypeId(0) {} 711 NextTypeId(0) {}
668 712
669 ~TypesParser() override {} 713 ~TypesParser() override {}
670 714
671 private: 715 private:
672 Ice::TimerMarker Timer; 716 Ice::TimerMarker Timer;
673 // The type ID that will be associated with the next type defining 717 // The type ID that will be associated with the next type defining
674 // record in the types block. 718 // record in the types block.
675 unsigned NextTypeId; 719 unsigned NextTypeId;
676 720
677 void ProcessRecord() override; 721 void ProcessRecord() override;
678 722
723 const char *getBlockName() const override { return "type"; }
724
679 void setNextTypeIDAsSimpleType(Ice::Type Ty) { 725 void setNextTypeIDAsSimpleType(Ice::Type Ty) {
680 Context->getTypeByIDForDefining(NextTypeId++)->setAsSimpleType(Ty); 726 Context->getTypeByIDForDefining(NextTypeId++)->setAsSimpleType(Ty);
681 } 727 }
682 }; 728 };
683 729
684 void TypesParser::ProcessRecord() { 730 void TypesParser::ProcessRecord() {
685 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); 731 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues();
686 switch (Record.GetCode()) { 732 switch (Record.GetCode()) {
687 case naclbitc::TYPE_CODE_NUMENTRY: 733 case naclbitc::TYPE_CODE_NUMENTRY:
688 // NUMENTRY: [numentries] 734 // NUMENTRY: [numentries]
689 if (!isValidRecordSize(1, "Type count")) 735 if (!isValidRecordSize(1, "count"))
690 return; 736 return;
691 Context->resizeTypeIDValues(Values[0]); 737 Context->resizeTypeIDValues(Values[0]);
692 return; 738 return;
693 case naclbitc::TYPE_CODE_VOID: 739 case naclbitc::TYPE_CODE_VOID:
694 // VOID 740 // VOID
695 if (!isValidRecordSize(0, "Type void")) 741 if (!isValidRecordSize(0, "void"))
696 return; 742 return;
697 setNextTypeIDAsSimpleType(Ice::IceType_void); 743 setNextTypeIDAsSimpleType(Ice::IceType_void);
698 return; 744 return;
699 case naclbitc::TYPE_CODE_FLOAT: 745 case naclbitc::TYPE_CODE_FLOAT:
700 // FLOAT 746 // FLOAT
701 if (!isValidRecordSize(0, "Type float")) 747 if (!isValidRecordSize(0, "float"))
702 return; 748 return;
703 setNextTypeIDAsSimpleType(Ice::IceType_f32); 749 setNextTypeIDAsSimpleType(Ice::IceType_f32);
704 return; 750 return;
705 case naclbitc::TYPE_CODE_DOUBLE: 751 case naclbitc::TYPE_CODE_DOUBLE:
706 // DOUBLE 752 // DOUBLE
707 if (!isValidRecordSize(0, "Type double")) 753 if (!isValidRecordSize(0, "double"))
708 return; 754 return;
709 setNextTypeIDAsSimpleType(Ice::IceType_f64); 755 setNextTypeIDAsSimpleType(Ice::IceType_f64);
710 return; 756 return;
711 case naclbitc::TYPE_CODE_INTEGER: 757 case naclbitc::TYPE_CODE_INTEGER:
712 // INTEGER: [width] 758 // INTEGER: [width]
713 if (!isValidRecordSize(1, "Type integer")) 759 if (!isValidRecordSize(1, "integer"))
714 return; 760 return;
715 switch (Values[0]) { 761 switch (Values[0]) {
716 case 1: 762 case 1:
717 setNextTypeIDAsSimpleType(Ice::IceType_i1); 763 setNextTypeIDAsSimpleType(Ice::IceType_i1);
718 return; 764 return;
719 case 8: 765 case 8:
720 setNextTypeIDAsSimpleType(Ice::IceType_i8); 766 setNextTypeIDAsSimpleType(Ice::IceType_i8);
721 return; 767 return;
722 case 16: 768 case 16:
723 setNextTypeIDAsSimpleType(Ice::IceType_i16); 769 setNextTypeIDAsSimpleType(Ice::IceType_i16);
724 return; 770 return;
725 case 32: 771 case 32:
726 setNextTypeIDAsSimpleType(Ice::IceType_i32); 772 setNextTypeIDAsSimpleType(Ice::IceType_i32);
727 return; 773 return;
728 case 64: 774 case 64:
729 setNextTypeIDAsSimpleType(Ice::IceType_i64); 775 setNextTypeIDAsSimpleType(Ice::IceType_i64);
730 return; 776 return;
731 default: 777 default:
732 break; 778 break;
733 } 779 }
734 { 780 {
735 std::string Buffer; 781 std::string Buffer;
736 raw_string_ostream StrBuf(Buffer); 782 raw_string_ostream StrBuf(Buffer);
737 StrBuf << "Type integer record with invalid bitsize: " << Values[0]; 783 StrBuf << "Type integer record with invalid bitsize: " << Values[0];
738 Error(StrBuf.str()); 784 Error(StrBuf.str());
739 } 785 }
740 return; 786 return;
741 case naclbitc::TYPE_CODE_VECTOR: { 787 case naclbitc::TYPE_CODE_VECTOR: {
742 // VECTOR: [numelts, eltty] 788 // VECTOR: [numelts, eltty]
743 if (!isValidRecordSize(2, "Type vector")) 789 if (!isValidRecordSize(2, "vector"))
744 return; 790 return;
745 Ice::Type BaseTy = Context->getSimpleTypeByID(Values[1]); 791 Ice::Type BaseTy = Context->getSimpleTypeByID(Values[1]);
746 Ice::SizeT Size = Values[0]; 792 Ice::SizeT Size = Values[0];
747 switch (BaseTy) { 793 switch (BaseTy) {
748 case Ice::IceType_i1: 794 case Ice::IceType_i1:
749 switch (Size) { 795 switch (Size) {
750 case 4: 796 case 4:
751 setNextTypeIDAsSimpleType(Ice::IceType_v4i1); 797 setNextTypeIDAsSimpleType(Ice::IceType_v4i1);
752 return; 798 return;
753 case 8: 799 case 8:
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
791 std::string Buffer; 837 std::string Buffer;
792 raw_string_ostream StrBuf(Buffer); 838 raw_string_ostream StrBuf(Buffer);
793 StrBuf << "Invalid type vector record: <" << Values[0] << " x " << BaseTy 839 StrBuf << "Invalid type vector record: <" << Values[0] << " x " << BaseTy
794 << ">"; 840 << ">";
795 Error(StrBuf.str()); 841 Error(StrBuf.str());
796 } 842 }
797 return; 843 return;
798 } 844 }
799 case naclbitc::TYPE_CODE_FUNCTION: { 845 case naclbitc::TYPE_CODE_FUNCTION: {
800 // FUNCTION: [vararg, retty, paramty x N] 846 // FUNCTION: [vararg, retty, paramty x N]
801 if (!isValidRecordSizeAtLeast(2, "Type signature")) 847 if (!isValidRecordSizeAtLeast(2, "signature"))
802 return; 848 return;
803 if (Values[0]) 849 if (Values[0])
804 Error("Function type can't define varargs"); 850 Error("Function type can't define varargs");
805 ExtendedType *Ty = Context->getTypeByIDForDefining(NextTypeId++); 851 ExtendedType *Ty = Context->getTypeByIDForDefining(NextTypeId++);
806 Ty->setAsFunctionType(); 852 Ty->setAsFunctionType();
807 FuncSigExtendedType *FuncTy = cast<FuncSigExtendedType>(Ty); 853 FuncSigExtendedType *FuncTy = cast<FuncSigExtendedType>(Ty);
808 FuncTy->setReturnType(Context->getSimpleTypeByID(Values[1])); 854 FuncTy->setReturnType(Context->getSimpleTypeByID(Values[1]));
809 for (unsigned i = 2, e = Values.size(); i != e; ++i) { 855 for (unsigned i = 2, e = Values.size(); i != e; ++i) {
810 // Check that type void not used as argument type. 856 // Check that type void not used as argument type.
811 // Note: PNaCl restrictions can't be checked until we 857 // Note: PNaCl restrictions can't be checked until we
(...skipping 25 matching lines...) Expand all
837 GlobalsParser(unsigned BlockID, BlockParserBaseClass *EnclosingParser) 883 GlobalsParser(unsigned BlockID, BlockParserBaseClass *EnclosingParser)
838 : BlockParserBaseClass(BlockID, EnclosingParser), 884 : BlockParserBaseClass(BlockID, EnclosingParser),
839 Timer(Ice::TimerStack::TT_parseGlobals, getTranslator().getContext()), 885 Timer(Ice::TimerStack::TT_parseGlobals, getTranslator().getContext()),
840 InitializersNeeded(0), NextGlobalID(0), 886 InitializersNeeded(0), NextGlobalID(0),
841 DummyGlobalVar( 887 DummyGlobalVar(
842 Ice::VariableDeclaration::create(getTranslator().getContext())), 888 Ice::VariableDeclaration::create(getTranslator().getContext())),
843 CurGlobalVar(DummyGlobalVar) {} 889 CurGlobalVar(DummyGlobalVar) {}
844 890
845 ~GlobalsParser() final {} 891 ~GlobalsParser() final {}
846 892
893 const char *getBlockName() const override { return "globals"; }
894
847 private: 895 private:
848 Ice::TimerMarker Timer; 896 Ice::TimerMarker Timer;
849 // Keeps track of how many initializers are expected for the global variable 897 // Keeps track of how many initializers are expected for the global variable
850 // declaration being built. 898 // declaration being built.
851 unsigned InitializersNeeded; 899 unsigned InitializersNeeded;
852 900
853 // The index of the next global variable declaration. 901 // The index of the next global variable declaration.
854 unsigned NextGlobalID; 902 unsigned NextGlobalID;
855 903
856 // Dummy global variable declaration to guarantee CurGlobalVar is 904 // Dummy global variable declaration to guarantee CurGlobalVar is
857 // always defined (allowing code to not need to check if 905 // always defined (allowing code to not need to check if
858 // CurGlobalVar is nullptr). 906 // CurGlobalVar is nullptr).
859 Ice::VariableDeclaration *DummyGlobalVar; 907 Ice::VariableDeclaration *DummyGlobalVar;
860 908
861 // Holds the current global variable declaration being built. 909 // Holds the current global variable declaration being built.
862 Ice::VariableDeclaration *CurGlobalVar; 910 Ice::VariableDeclaration *CurGlobalVar;
863 911
864 void ExitBlock() override { 912 void ExitBlock() override {
865 verifyNoMissingInitializers(); 913 verifyNoMissingInitializers();
866 unsigned NumIDs = Context->getNumGlobalVariables(); 914 unsigned NumIDs = Context->getNumGlobalVariables();
867 if (NextGlobalID < NumIDs) { 915 if (NextGlobalID < NumIDs) {
868 std::string Buffer; 916 std::string Buffer;
869 raw_string_ostream StrBuf(Buffer); 917 raw_string_ostream StrBuf(Buffer);
870 StrBuf << "Globals block expects " << NumIDs 918 StrBuf << getBlockName() << " block expects " << NumIDs
871 << " global variable declarations. Found: " << NextGlobalID; 919 << " global variable declarations. Found: " << NextGlobalID;
872 Error(StrBuf.str()); 920 Error(StrBuf.str());
873 } 921 }
874 BlockParserBaseClass::ExitBlock(); 922 BlockParserBaseClass::ExitBlock();
875 } 923 }
876 924
877 void ProcessRecord() override; 925 void ProcessRecord() override;
878 926
879 // Checks if the number of initializers for the CurGlobalVar is the same as 927 // Checks if the number of initializers for the CurGlobalVar is the same as
880 // the number found in the bitcode file. If different, and error message is 928 // the number found in the bitcode file. If different, and error message is
(...skipping 13 matching lines...) Expand all
894 InitializersNeeded = NumInits; 942 InitializersNeeded = NumInits;
895 } 943 }
896 } 944 }
897 }; 945 };
898 946
899 void GlobalsParser::ProcessRecord() { 947 void GlobalsParser::ProcessRecord() {
900 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); 948 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues();
901 switch (Record.GetCode()) { 949 switch (Record.GetCode()) {
902 case naclbitc::GLOBALVAR_COUNT: 950 case naclbitc::GLOBALVAR_COUNT:
903 // COUNT: [n] 951 // COUNT: [n]
904 if (!isValidRecordSize(1, "Globals count")) 952 if (!isValidRecordSize(1, "count"))
905 return; 953 return;
906 if (NextGlobalID != Context->getNumGlobalVariables()) { 954 if (NextGlobalID != Context->getNumGlobalVariables()) {
907 Error("Globals count record not first in block."); 955 Error("Globals count record not first in block.");
908 return; 956 return;
909 } 957 }
910 Context->CreateGlobalVariables(Values[0]); 958 Context->CreateGlobalVariables(Values[0]);
911 return; 959 return;
912 case naclbitc::GLOBALVAR_VAR: { 960 case naclbitc::GLOBALVAR_VAR: {
913 // VAR: [align, isconst] 961 // VAR: [align, isconst]
914 if (!isValidRecordSize(2, "Globals variable")) 962 if (!isValidRecordSize(2, "variable"))
915 return; 963 return;
916 verifyNoMissingInitializers(); 964 verifyNoMissingInitializers();
917 if (!isIRGenerationDisabled()) { 965 if (!isIRGenerationDisabled()) {
918 InitializersNeeded = 1; 966 InitializersNeeded = 1;
919 CurGlobalVar = Context->getGlobalVariableByID(NextGlobalID); 967 CurGlobalVar = Context->getGlobalVariableByID(NextGlobalID);
920 CurGlobalVar->setAlignment((1 << Values[0]) >> 1); 968 CurGlobalVar->setAlignment((1 << Values[0]) >> 1);
921 CurGlobalVar->setIsConstant(Values[1] != 0); 969 CurGlobalVar->setIsConstant(Values[1] != 0);
922 } 970 }
923 ++NextGlobalID; 971 ++NextGlobalID;
924 return; 972 return;
925 } 973 }
926 case naclbitc::GLOBALVAR_COMPOUND: 974 case naclbitc::GLOBALVAR_COMPOUND:
927 // COMPOUND: [size] 975 // COMPOUND: [size]
928 if (!isValidRecordSize(1, "globals compound")) 976 if (!isValidRecordSize(1, "compound"))
929 return; 977 return;
930 if (!CurGlobalVar->getInitializers().empty()) { 978 if (!CurGlobalVar->getInitializers().empty()) {
931 Error("Globals compound record not first initializer"); 979 Error("Globals compound record not first initializer");
932 return; 980 return;
933 } 981 }
934 if (Values[0] < 2) { 982 if (Values[0] < 2) {
935 std::string Buffer; 983 std::string Buffer;
936 raw_string_ostream StrBuf(Buffer); 984 raw_string_ostream StrBuf(Buffer);
937 StrBuf << "Globals compound record size invalid. Found: " << Values[0]; 985 StrBuf << getBlockName()
986 << " compound record size invalid. Found: " << Values[0];
938 Error(StrBuf.str()); 987 Error(StrBuf.str());
939 return; 988 return;
940 } 989 }
941 if (isIRGenerationDisabled()) 990 if (isIRGenerationDisabled())
942 return; 991 return;
943 InitializersNeeded = Values[0]; 992 InitializersNeeded = Values[0];
944 return; 993 return;
945 case naclbitc::GLOBALVAR_ZEROFILL: { 994 case naclbitc::GLOBALVAR_ZEROFILL: {
946 // ZEROFILL: [size] 995 // ZEROFILL: [size]
947 if (!isValidRecordSize(1, "Globals zerofill")) 996 if (!isValidRecordSize(1, "zerofill"))
948 return; 997 return;
949 if (isIRGenerationDisabled()) 998 if (isIRGenerationDisabled())
950 return; 999 return;
951 CurGlobalVar->addInitializer( 1000 CurGlobalVar->addInitializer(
952 new Ice::VariableDeclaration::ZeroInitializer(Values[0])); 1001 new Ice::VariableDeclaration::ZeroInitializer(Values[0]));
953 return; 1002 return;
954 } 1003 }
955 case naclbitc::GLOBALVAR_DATA: { 1004 case naclbitc::GLOBALVAR_DATA: {
956 // DATA: [b0, b1, ...] 1005 // DATA: [b0, b1, ...]
957 if (!isValidRecordSizeAtLeast(1, "Globals data")) 1006 if (!isValidRecordSizeAtLeast(1, "data"))
958 return; 1007 return;
959 if (isIRGenerationDisabled()) 1008 if (isIRGenerationDisabled())
960 return; 1009 return;
961 CurGlobalVar->addInitializer( 1010 CurGlobalVar->addInitializer(
962 new Ice::VariableDeclaration::DataInitializer(Values)); 1011 new Ice::VariableDeclaration::DataInitializer(Values));
963 return; 1012 return;
964 } 1013 }
965 case naclbitc::GLOBALVAR_RELOC: { 1014 case naclbitc::GLOBALVAR_RELOC: {
966 // RELOC: [val, [addend]] 1015 // RELOC: [val, [addend]]
967 if (!isValidRecordSizeInRange(1, 2, "Globals reloc")) 1016 if (!isValidRecordSizeInRange(1, 2, "reloc"))
968 return; 1017 return;
969 if (isIRGenerationDisabled()) 1018 if (isIRGenerationDisabled())
970 return; 1019 return;
971 unsigned Index = Values[0]; 1020 unsigned Index = Values[0];
972 Ice::SizeT Offset = 0; 1021 Ice::SizeT Offset = 0;
973 if (Values.size() == 2) 1022 if (Values.size() == 2)
974 Offset = Values[1]; 1023 Offset = Values[1];
975 CurGlobalVar->addInitializer(new Ice::VariableDeclaration::RelocInitializer( 1024 CurGlobalVar->addInitializer(new Ice::VariableDeclaration::RelocInitializer(
976 Context->getGlobalDeclarationByID(Index), Offset)); 1025 Context->getGlobalDeclarationByID(Index), Offset));
977 return; 1026 return;
978 } 1027 }
979 default: 1028 default:
980 BlockParserBaseClass::ProcessRecord(); 1029 BlockParserBaseClass::ProcessRecord();
981 return; 1030 return;
982 } 1031 }
983 } 1032 }
984 1033
985 /// Base class for parsing a valuesymtab block in the bitcode file. 1034 /// Base class for parsing a valuesymtab block in the bitcode file.
986 class ValuesymtabParser : public BlockParserBaseClass { 1035 class ValuesymtabParser : public BlockParserBaseClass {
987 ValuesymtabParser(const ValuesymtabParser &) = delete; 1036 ValuesymtabParser(const ValuesymtabParser &) = delete;
988 void operator=(const ValuesymtabParser &) = delete; 1037 void operator=(const ValuesymtabParser &) = delete;
989 1038
990 public: 1039 public:
991 ValuesymtabParser(unsigned BlockID, BlockParserBaseClass *EnclosingParser) 1040 ValuesymtabParser(unsigned BlockID, BlockParserBaseClass *EnclosingParser)
992 : BlockParserBaseClass(BlockID, EnclosingParser) {} 1041 : BlockParserBaseClass(BlockID, EnclosingParser) {}
993 1042
994 ~ValuesymtabParser() override {} 1043 ~ValuesymtabParser() override {}
995 1044
1045 const char *getBlockName() const override { return "valuesymtab"; }
1046
996 protected: 1047 protected:
997 typedef SmallString<128> StringType; 1048 typedef SmallString<128> StringType;
998 1049
999 // Associates Name with the value defined by the given Index. 1050 // Associates Name with the value defined by the given Index.
1000 virtual void setValueName(uint64_t Index, StringType &Name) = 0; 1051 virtual void setValueName(uint64_t Index, StringType &Name) = 0;
1001 1052
1002 // Associates Name with the value defined by the given Index; 1053 // Associates Name with the value defined by the given Index;
1003 virtual void setBbName(uint64_t Index, StringType &Name) = 0; 1054 virtual void setBbName(uint64_t Index, StringType &Name) = 0;
1004 1055
1005 private: 1056 private:
1006 1057
1007 void ProcessRecord() override; 1058 void ProcessRecord() override;
1008 1059
1009 void ConvertToString(StringType &ConvertedName) { 1060 void ConvertToString(StringType &ConvertedName) {
1010 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); 1061 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues();
1011 for (size_t i = 1, e = Values.size(); i != e; ++i) { 1062 for (size_t i = 1, e = Values.size(); i != e; ++i) {
1012 ConvertedName += static_cast<char>(Values[i]); 1063 ConvertedName += static_cast<char>(Values[i]);
1013 } 1064 }
1014 } 1065 }
1015 }; 1066 };
1016 1067
1017 void ValuesymtabParser::ProcessRecord() { 1068 void ValuesymtabParser::ProcessRecord() {
1018 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); 1069 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues();
1019 StringType ConvertedName; 1070 StringType ConvertedName;
1020 switch (Record.GetCode()) { 1071 switch (Record.GetCode()) {
1021 case naclbitc::VST_CODE_ENTRY: { 1072 case naclbitc::VST_CODE_ENTRY: {
1022 // VST_ENTRY: [ValueId, namechar x N] 1073 // VST_ENTRY: [ValueId, namechar x N]
1023 if (!isValidRecordSizeAtLeast(2, "Valuesymtab value entry")) 1074 if (!isValidRecordSizeAtLeast(2, "value entry"))
1024 return; 1075 return;
1025 ConvertToString(ConvertedName); 1076 ConvertToString(ConvertedName);
1026 setValueName(Values[0], ConvertedName); 1077 setValueName(Values[0], ConvertedName);
1027 return; 1078 return;
1028 } 1079 }
1029 case naclbitc::VST_CODE_BBENTRY: { 1080 case naclbitc::VST_CODE_BBENTRY: {
1030 // VST_BBENTRY: [BbId, namechar x N] 1081 // VST_BBENTRY: [BbId, namechar x N]
1031 if (!isValidRecordSizeAtLeast(2, "Valuesymtab basic block entry")) 1082 if (!isValidRecordSizeAtLeast(2, "basic block entry"))
1032 return; 1083 return;
1033 ConvertToString(ConvertedName); 1084 ConvertToString(ConvertedName);
1034 setBbName(Values[0], ConvertedName); 1085 setBbName(Values[0], ConvertedName);
1035 return; 1086 return;
1036 } 1087 }
1037 default: 1088 default:
1038 break; 1089 break;
1039 } 1090 }
1040 // If reached, don't know how to handle record. 1091 // If reached, don't know how to handle record.
1041 BlockParserBaseClass::ProcessRecord(); 1092 BlockParserBaseClass::ProcessRecord();
1042 return; 1093 return;
1043 } 1094 }
1044 1095
1045 class FunctionValuesymtabParser; 1096 class FunctionValuesymtabParser;
1046 1097
1047 /// Parses function blocks in the bitcode file. 1098 /// Parses function blocks in the bitcode file.
1048 class FunctionParser : public BlockParserBaseClass { 1099 class FunctionParser : public BlockParserBaseClass {
1049 FunctionParser(const FunctionParser &) = delete; 1100 FunctionParser(const FunctionParser &) = delete;
1050 FunctionParser &operator=(const FunctionParser &) = delete; 1101 FunctionParser &operator=(const FunctionParser &) = delete;
1051 friend class FunctionValuesymtabParser;
1052 1102
1053 public: 1103 public:
1054 FunctionParser(unsigned BlockID, BlockParserBaseClass *EnclosingParser) 1104 FunctionParser(unsigned BlockID, BlockParserBaseClass *EnclosingParser)
1055 : BlockParserBaseClass(BlockID, EnclosingParser), 1105 : BlockParserBaseClass(BlockID, EnclosingParser),
1056 Timer(Ice::TimerStack::TT_parseFunctions, getTranslator().getContext()), 1106 Timer(Ice::TimerStack::TT_parseFunctions, getTranslator().getContext()),
1057 Func(isIRGenerationDisabled() 1107 Func(isIRGenerationDisabled()
1058 ? nullptr 1108 ? nullptr
1059 : new Ice::Cfg(getTranslator().getContext())), 1109 : new Ice::Cfg(getTranslator().getContext())),
1060 CurrentBbIndex(0), FcnId(Context->getNextFunctionBlockValueID()), 1110 CurrentBbIndex(0), FcnId(Context->getNextFunctionBlockValueID()),
1061 FuncDecl(Context->getFunctionByID(FcnId)), 1111 FuncDecl(Context->getFunctionByID(FcnId)),
(...skipping 21 matching lines...) Expand all
1083 CurrentNode = InstallNextBasicBlock(); 1133 CurrentNode = InstallNextBasicBlock();
1084 Func->setEntryNode(CurrentNode); 1134 Func->setEntryNode(CurrentNode);
1085 for (Ice::Type ArgType : Signature.getArgList()) { 1135 for (Ice::Type ArgType : Signature.getArgList()) {
1086 Func->addArg(getNextInstVar(ArgType)); 1136 Func->addArg(getNextInstVar(ArgType));
1087 } 1137 }
1088 } 1138 }
1089 } 1139 }
1090 1140
1091 ~FunctionParser() final {} 1141 ~FunctionParser() final {}
1092 1142
1143 const char *getBlockName() const override { return "function"; }
1144
1145 Ice::Cfg *getFunc() const {
1146 return Func;
1147 }
1148
1149 uint32_t getNumGlobalIDs() const {
1150 return CachedNumGlobalValueIDs;
1151 }
1152
1093 void setNextLocalInstIndex(Ice::Operand *Op) { 1153 void setNextLocalInstIndex(Ice::Operand *Op) {
1094 setOperand(NextLocalInstIndex++, Op); 1154 setOperand(NextLocalInstIndex++, Op);
1095 } 1155 }
1096 1156
1097 // Set the next constant ID to the given constant C. 1157 // Set the next constant ID to the given constant C.
1098 void setNextConstantID(Ice::Constant *C) { setNextLocalInstIndex(C); } 1158 void setNextConstantID(Ice::Constant *C) { setNextLocalInstIndex(C); }
1099 1159
1160 // Returns the value referenced by the given value Index.
1161 Ice::Operand *getOperand(uint32_t Index) {
1162 if (Index < CachedNumGlobalValueIDs) {
1163 return Context->getOrCreateGlobalConstantByID(Index);
1164 }
1165 uint32_t LocalIndex = Index - CachedNumGlobalValueIDs;
1166 if (LocalIndex >= LocalOperands.size()) {
1167 std::string Buffer;
1168 raw_string_ostream StrBuf(Buffer);
1169 StrBuf << "Value index " << Index << " not defined!";
1170 Error(StrBuf.str());
1171 report_fatal_error("Unable to continue");
1172 }
1173 Ice::Operand *Op = LocalOperands[LocalIndex];
1174 if (Op == nullptr) {
1175 if (isIRGenerationDisabled())
1176 return nullptr;
1177 std::string Buffer;
1178 raw_string_ostream StrBuf(Buffer);
1179 StrBuf << "Value index " << Index << " not defined!";
1180 Error(StrBuf.str());
1181 report_fatal_error("Unable to continue");
1182 }
1183 return Op;
1184 }
1185
1100 private: 1186 private:
1101 Ice::TimerMarker Timer; 1187 Ice::TimerMarker Timer;
1102 // The corresponding ICE function defined by the function block. 1188 // The corresponding ICE function defined by the function block.
1103 Ice::Cfg *Func; 1189 Ice::Cfg *Func;
1104 // The index to the current basic block being built. 1190 // The index to the current basic block being built.
1105 uint32_t CurrentBbIndex; 1191 uint32_t CurrentBbIndex;
1106 // The basic block being built. 1192 // The basic block being built.
1107 Ice::CfgNode *CurrentNode; 1193 Ice::CfgNode *CurrentNode;
1108 // The ID for the function. 1194 // The ID for the function.
1109 unsigned FcnId; 1195 unsigned FcnId;
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
1240 raw_string_ostream StrBuf(Buffer); 1326 raw_string_ostream StrBuf(Buffer);
1241 StrBuf << "Invalid relative value id: " << Id 1327 StrBuf << "Invalid relative value id: " << Id
1242 << " (must be <= " << BaseIndex << ")"; 1328 << " (must be <= " << BaseIndex << ")";
1243 Error(StrBuf.str()); 1329 Error(StrBuf.str());
1244 // TODO(kschimpf) Remove error recovery once implementation complete. 1330 // TODO(kschimpf) Remove error recovery once implementation complete.
1245 return 0; 1331 return 0;
1246 } 1332 }
1247 return BaseIndex - Id; 1333 return BaseIndex - Id;
1248 } 1334 }
1249 1335
1250 // Returns the value referenced by the given value Index.
1251 Ice::Operand *getOperand(uint32_t Index) {
1252 if (Index < CachedNumGlobalValueIDs) {
1253 return Context->getOrCreateGlobalConstantByID(Index);
1254 }
1255 uint32_t LocalIndex = Index - CachedNumGlobalValueIDs;
1256 if (LocalIndex >= LocalOperands.size()) {
1257 std::string Buffer;
1258 raw_string_ostream StrBuf(Buffer);
1259 StrBuf << "Value index " << Index << " not defined!";
1260 Error(StrBuf.str());
1261 report_fatal_error("Unable to continue");
1262 }
1263 Ice::Operand *Op = LocalOperands[LocalIndex];
1264 if (Op == nullptr) {
1265 if (isIRGenerationDisabled())
1266 return nullptr;
1267 std::string Buffer;
1268 raw_string_ostream StrBuf(Buffer);
1269 StrBuf << "Value index " << Index << " not defined!";
1270 Error(StrBuf.str());
1271 report_fatal_error("Unable to continue");
1272 }
1273 return Op;
1274 }
1275
1276 // Sets element Index (in the local operands list) to Op. 1336 // Sets element Index (in the local operands list) to Op.
1277 void setOperand(uint32_t Index, Ice::Operand *Op) { 1337 void setOperand(uint32_t Index, Ice::Operand *Op) {
1278 assert(Op || isIRGenerationDisabled()); 1338 assert(Op || isIRGenerationDisabled());
1279 // Check if simple push works. 1339 // Check if simple push works.
1280 uint32_t LocalIndex = Index - CachedNumGlobalValueIDs; 1340 uint32_t LocalIndex = Index - CachedNumGlobalValueIDs;
1281 if (LocalIndex == LocalOperands.size()) { 1341 if (LocalIndex == LocalOperands.size()) {
1282 LocalOperands.push_back(Op); 1342 LocalOperands.push_back(Op);
1283 return; 1343 return;
1284 } 1344 }
1285 1345
(...skipping 468 matching lines...) Expand 10 before | Expand all | Expand 10 after
1754 if (InstIsTerminating) { 1814 if (InstIsTerminating) {
1755 InstIsTerminating = false; 1815 InstIsTerminating = false;
1756 if (!isIRGenerationDisabled()) 1816 if (!isIRGenerationDisabled())
1757 CurrentNode = getBasicBlock(++CurrentBbIndex); 1817 CurrentNode = getBasicBlock(++CurrentBbIndex);
1758 } 1818 }
1759 // The base index for relative indexing. 1819 // The base index for relative indexing.
1760 int32_t BaseIndex = getNextInstIndex(); 1820 int32_t BaseIndex = getNextInstIndex();
1761 switch (Record.GetCode()) { 1821 switch (Record.GetCode()) {
1762 case naclbitc::FUNC_CODE_DECLAREBLOCKS: { 1822 case naclbitc::FUNC_CODE_DECLAREBLOCKS: {
1763 // DECLAREBLOCKS: [n] 1823 // DECLAREBLOCKS: [n]
1764 if (!isValidRecordSize(1, "function block count")) 1824 if (!isValidRecordSize(1, "count"))
1765 return; 1825 return;
1766 uint32_t NumBbs = Values[0]; 1826 uint32_t NumBbs = Values[0];
1767 if (NumBbs == 0) { 1827 if (NumBbs == 0) {
1768 Error("Functions must contain at least one basic block."); 1828 Error("Functions must contain at least one basic block.");
1769 // TODO(kschimpf) Remove error recovery once implementation complete. 1829 // TODO(kschimpf) Remove error recovery once implementation complete.
1770 NumBbs = 1; 1830 NumBbs = 1;
1771 } 1831 }
1772 if (isIRGenerationDisabled()) 1832 if (isIRGenerationDisabled())
1773 return; 1833 return;
1774 if (Func->getNodes().size() != 1) { 1834 if (Func->getNodes().size() != 1) {
1775 Error("Duplicate function block count record"); 1835 Error("Duplicate function block count record");
1776 return; 1836 return;
1777 } 1837 }
1778 // Install the basic blocks, skipping bb0 which was created in the 1838 // Install the basic blocks, skipping bb0 which was created in the
1779 // constructor. 1839 // constructor.
1780 for (size_t i = 1; i < NumBbs; ++i) 1840 for (size_t i = 1; i < NumBbs; ++i)
1781 InstallNextBasicBlock(); 1841 InstallNextBasicBlock();
1782 return; 1842 return;
1783 } 1843 }
1784 case naclbitc::FUNC_CODE_INST_BINOP: { 1844 case naclbitc::FUNC_CODE_INST_BINOP: {
1785 // BINOP: [opval, opval, opcode] 1845 // BINOP: [opval, opval, opcode]
1786 if (!isValidRecordSize(3, "function block binop")) 1846 if (!isValidRecordSize(3, "binop"))
1787 return; 1847 return;
1788 Ice::Operand *Op1 = getRelativeOperand(Values[0], BaseIndex); 1848 Ice::Operand *Op1 = getRelativeOperand(Values[0], BaseIndex);
1789 Ice::Operand *Op2 = getRelativeOperand(Values[1], BaseIndex); 1849 Ice::Operand *Op2 = getRelativeOperand(Values[1], BaseIndex);
1790 if (isIRGenerationDisabled()) { 1850 if (isIRGenerationDisabled()) {
1791 assert(Op1 == nullptr && Op2 == nullptr); 1851 assert(Op1 == nullptr && Op2 == nullptr);
1792 setNextLocalInstIndex(nullptr); 1852 setNextLocalInstIndex(nullptr);
1793 return; 1853 return;
1794 } 1854 }
1795 Ice::Type Type1 = Op1->getType(); 1855 Ice::Type Type1 = Op1->getType();
1796 Ice::Type Type2 = Op2->getType(); 1856 Ice::Type Type2 = Op2->getType();
1797 if (Type1 != Type2) { 1857 if (Type1 != Type2) {
1798 std::string Buffer; 1858 std::string Buffer;
1799 raw_string_ostream StrBuf(Buffer); 1859 raw_string_ostream StrBuf(Buffer);
1800 StrBuf << "Binop argument types differ: " << Type1 << " and " << Type2; 1860 StrBuf << "Binop argument types differ: " << Type1 << " and " << Type2;
1801 Error(StrBuf.str()); 1861 Error(StrBuf.str());
1802 appendErrorInstruction(Type1); 1862 appendErrorInstruction(Type1);
1803 return; 1863 return;
1804 } 1864 }
1805 1865
1806 Ice::InstArithmetic::OpKind Opcode; 1866 Ice::InstArithmetic::OpKind Opcode;
1807 if (!convertBinopOpcode(Values[2], Type1, Opcode)) 1867 if (!convertBinopOpcode(Values[2], Type1, Opcode))
1808 return; 1868 return;
1809 CurrentNode->appendInst(Ice::InstArithmetic::create( 1869 CurrentNode->appendInst(Ice::InstArithmetic::create(
1810 Func, Opcode, getNextInstVar(Type1), Op1, Op2)); 1870 Func, Opcode, getNextInstVar(Type1), Op1, Op2));
1811 return; 1871 return;
1812 } 1872 }
1813 case naclbitc::FUNC_CODE_INST_CAST: { 1873 case naclbitc::FUNC_CODE_INST_CAST: {
1814 // CAST: [opval, destty, castopc] 1874 // CAST: [opval, destty, castopc]
1815 if (!isValidRecordSize(3, "function block cast")) 1875 if (!isValidRecordSize(3, "cast"))
1816 return; 1876 return;
1817 Ice::Operand *Src = getRelativeOperand(Values[0], BaseIndex); 1877 Ice::Operand *Src = getRelativeOperand(Values[0], BaseIndex);
1818 Ice::Type CastType = Context->getSimpleTypeByID(Values[1]); 1878 Ice::Type CastType = Context->getSimpleTypeByID(Values[1]);
1819 Instruction::CastOps LLVMCastOp; 1879 Instruction::CastOps LLVMCastOp;
1820 Ice::InstCast::OpKind CastKind; 1880 Ice::InstCast::OpKind CastKind;
1821 if (!naclbitc::DecodeCastOpcode(Values[2], LLVMCastOp) || 1881 if (!naclbitc::DecodeCastOpcode(Values[2], LLVMCastOp) ||
1822 !convertLLVMCastOpToIceOp(LLVMCastOp, CastKind)) { 1882 !convertLLVMCastOpToIceOp(LLVMCastOp, CastKind)) {
1823 std::string Buffer; 1883 std::string Buffer;
1824 raw_string_ostream StrBuf(Buffer); 1884 raw_string_ostream StrBuf(Buffer);
1825 StrBuf << "Cast opcode not understood: " << Values[2]; 1885 StrBuf << "Cast opcode not understood: " << Values[2];
(...skipping 16 matching lines...) Expand all
1842 Error(StrBuf.str()); 1902 Error(StrBuf.str());
1843 appendErrorInstruction(CastType); 1903 appendErrorInstruction(CastType);
1844 return; 1904 return;
1845 } 1905 }
1846 CurrentNode->appendInst( 1906 CurrentNode->appendInst(
1847 Ice::InstCast::create(Func, CastKind, getNextInstVar(CastType), Src)); 1907 Ice::InstCast::create(Func, CastKind, getNextInstVar(CastType), Src));
1848 return; 1908 return;
1849 } 1909 }
1850 case naclbitc::FUNC_CODE_INST_VSELECT: { 1910 case naclbitc::FUNC_CODE_INST_VSELECT: {
1851 // VSELECT: [opval, opval, pred] 1911 // VSELECT: [opval, opval, pred]
1852 if (!isValidRecordSize(3, "function block select")) 1912 if (!isValidRecordSize(3, "select"))
1853 return; 1913 return;
1854 Ice::Operand *ThenVal = getRelativeOperand(Values[0], BaseIndex); 1914 Ice::Operand *ThenVal = getRelativeOperand(Values[0], BaseIndex);
1855 Ice::Operand *ElseVal = getRelativeOperand(Values[1], BaseIndex); 1915 Ice::Operand *ElseVal = getRelativeOperand(Values[1], BaseIndex);
1856 Ice::Operand *CondVal = getRelativeOperand(Values[2], BaseIndex); 1916 Ice::Operand *CondVal = getRelativeOperand(Values[2], BaseIndex);
1857 if (isIRGenerationDisabled()) { 1917 if (isIRGenerationDisabled()) {
1858 assert(ThenVal == nullptr && ElseVal == nullptr && CondVal == nullptr); 1918 assert(ThenVal == nullptr && ElseVal == nullptr && CondVal == nullptr);
1859 setNextLocalInstIndex(nullptr); 1919 setNextLocalInstIndex(nullptr);
1860 return; 1920 return;
1861 } 1921 }
1862 Ice::Type ThenType = ThenVal->getType(); 1922 Ice::Type ThenType = ThenVal->getType();
(...skipping 28 matching lines...) Expand all
1891 Error(StrBuf.str()); 1951 Error(StrBuf.str());
1892 appendErrorInstruction(ThenType); 1952 appendErrorInstruction(ThenType);
1893 return; 1953 return;
1894 } 1954 }
1895 CurrentNode->appendInst(Ice::InstSelect::create( 1955 CurrentNode->appendInst(Ice::InstSelect::create(
1896 Func, getNextInstVar(ThenType), CondVal, ThenVal, ElseVal)); 1956 Func, getNextInstVar(ThenType), CondVal, ThenVal, ElseVal));
1897 return; 1957 return;
1898 } 1958 }
1899 case naclbitc::FUNC_CODE_INST_EXTRACTELT: { 1959 case naclbitc::FUNC_CODE_INST_EXTRACTELT: {
1900 // EXTRACTELT: [opval, opval] 1960 // EXTRACTELT: [opval, opval]
1901 if (!isValidRecordSize(2, "function block extract element")) 1961 if (!isValidRecordSize(2, "extract element"))
1902 return; 1962 return;
1903 Ice::Operand *Vec = getRelativeOperand(Values[0], BaseIndex); 1963 Ice::Operand *Vec = getRelativeOperand(Values[0], BaseIndex);
1904 Ice::Operand *Index = getRelativeOperand(Values[1], BaseIndex); 1964 Ice::Operand *Index = getRelativeOperand(Values[1], BaseIndex);
1905 if (isIRGenerationDisabled()) { 1965 if (isIRGenerationDisabled()) {
1906 assert(Vec == nullptr && Index == nullptr); 1966 assert(Vec == nullptr && Index == nullptr);
1907 setNextLocalInstIndex(nullptr); 1967 setNextLocalInstIndex(nullptr);
1908 return; 1968 return;
1909 } 1969 }
1910 Ice::Type VecType = Vec->getType(); 1970 Ice::Type VecType = Vec->getType();
1911 VectorIndexCheckValue IndexCheckValue = validateVectorIndex(Vec, Index); 1971 VectorIndexCheckValue IndexCheckValue = validateVectorIndex(Vec, Index);
1912 if (IndexCheckValue != VectorIndexValid) { 1972 if (IndexCheckValue != VectorIndexValid) {
1913 std::string Buffer; 1973 std::string Buffer;
1914 raw_string_ostream StrBuf(Buffer); 1974 raw_string_ostream StrBuf(Buffer);
1915 dumpVectorIndexCheckValue(StrBuf, IndexCheckValue); 1975 dumpVectorIndexCheckValue(StrBuf, IndexCheckValue);
1916 StrBuf << ": extractelement " << VecType << " " << *Vec << ", " 1976 StrBuf << ": extractelement " << VecType << " " << *Vec << ", "
1917 << Index->getType() << " " << *Index; 1977 << Index->getType() << " " << *Index;
1918 Error(StrBuf.str()); 1978 Error(StrBuf.str());
1919 appendErrorInstruction(VecType); 1979 appendErrorInstruction(VecType);
1920 return; 1980 return;
1921 } 1981 }
1922 CurrentNode->appendInst(Ice::InstExtractElement::create( 1982 CurrentNode->appendInst(Ice::InstExtractElement::create(
1923 Func, getNextInstVar(typeElementType(VecType)), Vec, Index)); 1983 Func, getNextInstVar(typeElementType(VecType)), Vec, Index));
1924 return; 1984 return;
1925 } 1985 }
1926 case naclbitc::FUNC_CODE_INST_INSERTELT: { 1986 case naclbitc::FUNC_CODE_INST_INSERTELT: {
1927 // INSERTELT: [opval, opval, opval] 1987 // INSERTELT: [opval, opval, opval]
1928 if (!isValidRecordSize(3, "function block insert element")) 1988 if (!isValidRecordSize(3, "insert element"))
1929 return; 1989 return;
1930 Ice::Operand *Vec = getRelativeOperand(Values[0], BaseIndex); 1990 Ice::Operand *Vec = getRelativeOperand(Values[0], BaseIndex);
1931 Ice::Operand *Elt = getRelativeOperand(Values[1], BaseIndex); 1991 Ice::Operand *Elt = getRelativeOperand(Values[1], BaseIndex);
1932 Ice::Operand *Index = getRelativeOperand(Values[2], BaseIndex); 1992 Ice::Operand *Index = getRelativeOperand(Values[2], BaseIndex);
1933 if (isIRGenerationDisabled()) { 1993 if (isIRGenerationDisabled()) {
1934 assert(Vec == nullptr && Elt == nullptr && Index == nullptr); 1994 assert(Vec == nullptr && Elt == nullptr && Index == nullptr);
1935 setNextLocalInstIndex(nullptr); 1995 setNextLocalInstIndex(nullptr);
1936 return; 1996 return;
1937 } 1997 }
1938 Ice::Type VecType = Vec->getType(); 1998 Ice::Type VecType = Vec->getType();
1939 VectorIndexCheckValue IndexCheckValue = validateVectorIndex(Vec, Index); 1999 VectorIndexCheckValue IndexCheckValue = validateVectorIndex(Vec, Index);
1940 if (IndexCheckValue != VectorIndexValid) { 2000 if (IndexCheckValue != VectorIndexValid) {
1941 std::string Buffer; 2001 std::string Buffer;
1942 raw_string_ostream StrBuf(Buffer); 2002 raw_string_ostream StrBuf(Buffer);
1943 dumpVectorIndexCheckValue(StrBuf, IndexCheckValue); 2003 dumpVectorIndexCheckValue(StrBuf, IndexCheckValue);
1944 StrBuf << ": insertelement " << VecType << " " << *Vec << ", " 2004 StrBuf << ": insertelement " << VecType << " " << *Vec << ", "
1945 << Elt->getType() << " " << *Elt << ", " << Index->getType() << " " 2005 << Elt->getType() << " " << *Elt << ", " << Index->getType() << " "
1946 << *Index; 2006 << *Index;
1947 Error(StrBuf.str()); 2007 Error(StrBuf.str());
1948 appendErrorInstruction(Elt->getType()); 2008 appendErrorInstruction(Elt->getType());
1949 return; 2009 return;
1950 } 2010 }
1951 CurrentNode->appendInst(Ice::InstInsertElement::create( 2011 CurrentNode->appendInst(Ice::InstInsertElement::create(
1952 Func, getNextInstVar(VecType), Vec, Elt, Index)); 2012 Func, getNextInstVar(VecType), Vec, Elt, Index));
1953 return; 2013 return;
1954 } 2014 }
1955 case naclbitc::FUNC_CODE_INST_CMP2: { 2015 case naclbitc::FUNC_CODE_INST_CMP2: {
1956 // CMP2: [opval, opval, pred] 2016 // CMP2: [opval, opval, pred]
1957 if (!isValidRecordSize(3, "function block compare")) 2017 if (!isValidRecordSize(3, "compare"))
1958 return; 2018 return;
1959 Ice::Operand *Op1 = getRelativeOperand(Values[0], BaseIndex); 2019 Ice::Operand *Op1 = getRelativeOperand(Values[0], BaseIndex);
1960 Ice::Operand *Op2 = getRelativeOperand(Values[1], BaseIndex); 2020 Ice::Operand *Op2 = getRelativeOperand(Values[1], BaseIndex);
1961 if (isIRGenerationDisabled()) { 2021 if (isIRGenerationDisabled()) {
1962 assert(Op1 == nullptr && Op2 == nullptr); 2022 assert(Op1 == nullptr && Op2 == nullptr);
1963 setNextLocalInstIndex(nullptr); 2023 setNextLocalInstIndex(nullptr);
1964 return; 2024 return;
1965 } 2025 }
1966 Ice::Type Op1Type = Op1->getType(); 2026 Ice::Type Op1Type = Op1->getType();
1967 Ice::Type Op2Type = Op2->getType(); 2027 Ice::Type Op2Type = Op2->getType();
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
2013 raw_string_ostream StrBuf(Buffer); 2073 raw_string_ostream StrBuf(Buffer);
2014 StrBuf << "Compare on type not understood: " << Op1Type; 2074 StrBuf << "Compare on type not understood: " << Op1Type;
2015 Error(StrBuf.str()); 2075 Error(StrBuf.str());
2016 appendErrorInstruction(DestType); 2076 appendErrorInstruction(DestType);
2017 return; 2077 return;
2018 } 2078 }
2019 return; 2079 return;
2020 } 2080 }
2021 case naclbitc::FUNC_CODE_INST_RET: { 2081 case naclbitc::FUNC_CODE_INST_RET: {
2022 // RET: [opval?] 2082 // RET: [opval?]
2023 if (!isValidRecordSizeInRange(0, 1, "function block ret")) 2083 if (!isValidRecordSizeInRange(0, 1, "return"))
2024 return; 2084 return;
2025 if (Values.empty()) { 2085 if (Values.empty()) {
2026 if (isIRGenerationDisabled()) 2086 if (isIRGenerationDisabled())
2027 return; 2087 return;
2028 CurrentNode->appendInst(Ice::InstRet::create(Func)); 2088 CurrentNode->appendInst(Ice::InstRet::create(Func));
2029 } else { 2089 } else {
2030 Ice::Operand *RetVal = getRelativeOperand(Values[0], BaseIndex); 2090 Ice::Operand *RetVal = getRelativeOperand(Values[0], BaseIndex);
2031 if (isIRGenerationDisabled()) { 2091 if (isIRGenerationDisabled()) {
2032 assert(RetVal == nullptr); 2092 assert(RetVal == nullptr);
2033 return; 2093 return;
2034 } 2094 }
2035 CurrentNode->appendInst(Ice::InstRet::create(Func, RetVal)); 2095 CurrentNode->appendInst(Ice::InstRet::create(Func, RetVal));
2036 } 2096 }
2037 InstIsTerminating = true; 2097 InstIsTerminating = true;
2038 return; 2098 return;
2039 } 2099 }
2040 case naclbitc::FUNC_CODE_INST_BR: { 2100 case naclbitc::FUNC_CODE_INST_BR: {
2041 if (Values.size() == 1) { 2101 if (Values.size() == 1) {
2042 // BR: [bb#] 2102 // BR: [bb#]
2043 if (isIRGenerationDisabled()) 2103 if (isIRGenerationDisabled())
2044 return; 2104 return;
2045 Ice::CfgNode *Block = getBranchBasicBlock(Values[0]); 2105 Ice::CfgNode *Block = getBranchBasicBlock(Values[0]);
2046 if (Block == nullptr) 2106 if (Block == nullptr)
2047 return; 2107 return;
2048 CurrentNode->appendInst(Ice::InstBr::create(Func, Block)); 2108 CurrentNode->appendInst(Ice::InstBr::create(Func, Block));
2049 } else { 2109 } else {
2050 // BR: [bb#, bb#, opval] 2110 // BR: [bb#, bb#, opval]
2051 if (!isValidRecordSize(3, "function block branch")) 2111 if (!isValidRecordSize(3, "branch"))
2052 return; 2112 return;
2053 Ice::Operand *Cond = getRelativeOperand(Values[2], BaseIndex); 2113 Ice::Operand *Cond = getRelativeOperand(Values[2], BaseIndex);
2054 if (isIRGenerationDisabled()) { 2114 if (isIRGenerationDisabled()) {
2055 assert(Cond == nullptr); 2115 assert(Cond == nullptr);
2056 return; 2116 return;
2057 } 2117 }
2058 if (Cond->getType() != Ice::IceType_i1) { 2118 if (Cond->getType() != Ice::IceType_i1) {
2059 std::string Buffer; 2119 std::string Buffer;
2060 raw_string_ostream StrBuf(Buffer); 2120 raw_string_ostream StrBuf(Buffer);
2061 StrBuf << "Branch condition " << *Cond << " not i1. Found: " 2121 StrBuf << "Branch condition " << *Cond << " not i1. Found: "
(...skipping 13 matching lines...) Expand all
2075 } 2135 }
2076 case naclbitc::FUNC_CODE_INST_SWITCH: { 2136 case naclbitc::FUNC_CODE_INST_SWITCH: {
2077 // SWITCH: [Condty, Cond, BbIndex, NumCases Case ...] 2137 // SWITCH: [Condty, Cond, BbIndex, NumCases Case ...]
2078 // where Case = [1, 1, Value, BbIndex]. 2138 // where Case = [1, 1, Value, BbIndex].
2079 // 2139 //
2080 // Note: Unlike most instructions, we don't infer the type of 2140 // Note: Unlike most instructions, we don't infer the type of
2081 // Cond, but provide it as a separate field. There are also 2141 // Cond, but provide it as a separate field. There are also
2082 // unnecesary data fields (i.e. constants 1). These were not 2142 // unnecesary data fields (i.e. constants 1). These were not
2083 // cleaned up in PNaCl bitcode because the bitcode format was 2143 // cleaned up in PNaCl bitcode because the bitcode format was
2084 // already frozen when the problem was noticed. 2144 // already frozen when the problem was noticed.
2085 if (!isValidRecordSizeAtLeast(4, "function block switch")) 2145 if (!isValidRecordSizeAtLeast(4, "switch"))
2086 return; 2146 return;
2087 2147
2088 Ice::Type CondTy = Context->getSimpleTypeByID(Values[0]); 2148 Ice::Type CondTy = Context->getSimpleTypeByID(Values[0]);
2089 if (!Ice::isScalarIntegerType(CondTy)) { 2149 if (!Ice::isScalarIntegerType(CondTy)) {
2090 std::string Buffer; 2150 std::string Buffer;
2091 raw_string_ostream StrBuf(Buffer); 2151 raw_string_ostream StrBuf(Buffer);
2092 StrBuf << "Case condition must be non-wide integer. Found: " << CondTy; 2152 StrBuf << "Case condition must be non-wide integer. Found: " << CondTy;
2093 Error(StrBuf.str()); 2153 Error(StrBuf.str());
2094 return; 2154 return;
2095 } 2155 }
2096 Ice::SizeT BitWidth = Ice::getScalarIntBitWidth(CondTy); 2156 Ice::SizeT BitWidth = Ice::getScalarIntBitWidth(CondTy);
2097 Ice::Operand *Cond = getRelativeOperand(Values[1], BaseIndex); 2157 Ice::Operand *Cond = getRelativeOperand(Values[1], BaseIndex);
2098 2158
2099 const bool isIRGenDisabled = isIRGenerationDisabled(); 2159 const bool isIRGenDisabled = isIRGenerationDisabled();
2100 if (isIRGenDisabled) { 2160 if (isIRGenDisabled) {
2101 assert(Cond == nullptr); 2161 assert(Cond == nullptr);
2102 } else if (CondTy != Cond->getType()) { 2162 } else if (CondTy != Cond->getType()) {
2103 std::string Buffer; 2163 std::string Buffer;
2104 raw_string_ostream StrBuf(Buffer); 2164 raw_string_ostream StrBuf(Buffer);
2105 StrBuf << "Case condition expects type " << CondTy 2165 StrBuf << "Case condition expects type " << CondTy
2106 << ". Found: " << Cond->getType(); 2166 << ". Found: " << Cond->getType();
2107 Error(StrBuf.str()); 2167 Error(StrBuf.str());
2108 return; 2168 return;
2109 } 2169 }
2110 Ice::CfgNode *DefaultLabel = 2170 Ice::CfgNode *DefaultLabel =
2111 isIRGenDisabled ? nullptr : getBranchBasicBlock(Values[2]); 2171 isIRGenDisabled ? nullptr : getBranchBasicBlock(Values[2]);
2112 unsigned NumCases = Values[3]; 2172 unsigned NumCases = Values[3];
2113 2173
2114 // Now recognize each of the cases. 2174 // Now recognize each of the cases.
2115 if (!isValidRecordSize(4 + NumCases * 4, "Function block switch")) 2175 if (!isValidRecordSize(4 + NumCases * 4, "switch"))
2116 return; 2176 return;
2117 Ice::InstSwitch *Switch = 2177 Ice::InstSwitch *Switch =
2118 isIRGenDisabled ? nullptr : Ice::InstSwitch::create(Func, NumCases, 2178 isIRGenDisabled ? nullptr : Ice::InstSwitch::create(Func, NumCases,
2119 Cond, DefaultLabel); 2179 Cond, DefaultLabel);
2120 unsigned ValCaseIndex = 4; // index to beginning of case entry. 2180 unsigned ValCaseIndex = 4; // index to beginning of case entry.
2121 for (unsigned CaseIndex = 0; CaseIndex < NumCases; 2181 for (unsigned CaseIndex = 0; CaseIndex < NumCases;
2122 ++CaseIndex, ValCaseIndex += 4) { 2182 ++CaseIndex, ValCaseIndex += 4) {
2123 if (Values[ValCaseIndex] != 1 || Values[ValCaseIndex+1] != 1) { 2183 if (Values[ValCaseIndex] != 1 || Values[ValCaseIndex+1] != 1) {
2124 std::string Buffer; 2184 std::string Buffer;
2125 raw_string_ostream StrBuf(Buffer); 2185 raw_string_ostream StrBuf(Buffer);
(...skipping 11 matching lines...) Expand all
2137 Switch->addBranch(CaseIndex, Value.getSExtValue(), Label); 2197 Switch->addBranch(CaseIndex, Value.getSExtValue(), Label);
2138 } 2198 }
2139 if (isIRGenDisabled) 2199 if (isIRGenDisabled)
2140 return; 2200 return;
2141 CurrentNode->appendInst(Switch); 2201 CurrentNode->appendInst(Switch);
2142 InstIsTerminating = true; 2202 InstIsTerminating = true;
2143 return; 2203 return;
2144 } 2204 }
2145 case naclbitc::FUNC_CODE_INST_UNREACHABLE: { 2205 case naclbitc::FUNC_CODE_INST_UNREACHABLE: {
2146 // UNREACHABLE: [] 2206 // UNREACHABLE: []
2147 if (!isValidRecordSize(0, "function block unreachable")) 2207 if (!isValidRecordSize(0, "unreachable"))
2148 return; 2208 return;
2149 if (isIRGenerationDisabled()) 2209 if (isIRGenerationDisabled())
2150 return; 2210 return;
2151 CurrentNode->appendInst( 2211 CurrentNode->appendInst(
2152 Ice::InstUnreachable::create(Func)); 2212 Ice::InstUnreachable::create(Func));
2153 InstIsTerminating = true; 2213 InstIsTerminating = true;
2154 return; 2214 return;
2155 } 2215 }
2156 case naclbitc::FUNC_CODE_INST_PHI: { 2216 case naclbitc::FUNC_CODE_INST_PHI: {
2157 // PHI: [ty, val1, bb1, ..., valN, bbN] for n >= 2. 2217 // PHI: [ty, val1, bb1, ..., valN, bbN] for n >= 2.
2158 if (!isValidRecordSizeAtLeast(3, "function block phi")) 2218 if (!isValidRecordSizeAtLeast(3, "phi"))
2159 return; 2219 return;
2160 Ice::Type Ty = Context->getSimpleTypeByID(Values[0]); 2220 Ice::Type Ty = Context->getSimpleTypeByID(Values[0]);
2161 if ((Values.size() & 0x1) == 0) { 2221 if ((Values.size() & 0x1) == 0) {
2162 // Not an odd number of values. 2222 // Not an odd number of values.
2163 std::string Buffer; 2223 std::string Buffer;
2164 raw_string_ostream StrBuf(Buffer); 2224 raw_string_ostream StrBuf(Buffer);
2165 StrBuf << "function block phi record size not valid: " << Values.size(); 2225 StrBuf << "function block phi record size not valid: " << Values.size();
2166 Error(StrBuf.str()); 2226 Error(StrBuf.str());
2167 appendErrorInstruction(Ty); 2227 appendErrorInstruction(Ty);
2168 return; 2228 return;
(...skipping 25 matching lines...) Expand all
2194 appendErrorInstruction(Ty); 2254 appendErrorInstruction(Ty);
2195 return; 2255 return;
2196 } 2256 }
2197 Phi->addArgument(Op, getBasicBlock(Values[i + 1])); 2257 Phi->addArgument(Op, getBasicBlock(Values[i + 1]));
2198 } 2258 }
2199 CurrentNode->appendInst(Phi); 2259 CurrentNode->appendInst(Phi);
2200 return; 2260 return;
2201 } 2261 }
2202 case naclbitc::FUNC_CODE_INST_ALLOCA: { 2262 case naclbitc::FUNC_CODE_INST_ALLOCA: {
2203 // ALLOCA: [Size, align] 2263 // ALLOCA: [Size, align]
2204 if (!isValidRecordSize(2, "function block alloca")) 2264 if (!isValidRecordSize(2, "alloca"))
2205 return; 2265 return;
2206 Ice::Operand *ByteCount = getRelativeOperand(Values[0], BaseIndex); 2266 Ice::Operand *ByteCount = getRelativeOperand(Values[0], BaseIndex);
2207 unsigned Alignment; 2267 unsigned Alignment;
2208 extractAlignment("Alloca", Values[1], Alignment); 2268 extractAlignment("Alloca", Values[1], Alignment);
2209 if (isIRGenerationDisabled()) { 2269 if (isIRGenerationDisabled()) {
2210 assert(ByteCount == nullptr); 2270 assert(ByteCount == nullptr);
2211 setNextLocalInstIndex(nullptr); 2271 setNextLocalInstIndex(nullptr);
2212 return; 2272 return;
2213 } 2273 }
2214 Ice::Type PtrTy = Context->getIcePointerType(); 2274 Ice::Type PtrTy = Context->getIcePointerType();
2215 if (ByteCount->getType() != Ice::IceType_i32) { 2275 if (ByteCount->getType() != Ice::IceType_i32) {
2216 std::string Buffer; 2276 std::string Buffer;
2217 raw_string_ostream StrBuf(Buffer); 2277 raw_string_ostream StrBuf(Buffer);
2218 StrBuf << "Alloca on non-i32 value. Found: " << *ByteCount; 2278 StrBuf << "Alloca on non-i32 value. Found: " << *ByteCount;
2219 Error(StrBuf.str()); 2279 Error(StrBuf.str());
2220 appendErrorInstruction(PtrTy); 2280 appendErrorInstruction(PtrTy);
2221 return; 2281 return;
2222 } 2282 }
2223 CurrentNode->appendInst(Ice::InstAlloca::create(Func, ByteCount, Alignment, 2283 CurrentNode->appendInst(Ice::InstAlloca::create(Func, ByteCount, Alignment,
2224 getNextInstVar(PtrTy))); 2284 getNextInstVar(PtrTy)));
2225 return; 2285 return;
2226 } 2286 }
2227 case naclbitc::FUNC_CODE_INST_LOAD: { 2287 case naclbitc::FUNC_CODE_INST_LOAD: {
2228 // LOAD: [address, align, ty] 2288 // LOAD: [address, align, ty]
2229 if (!isValidRecordSize(3, "function block load")) 2289 if (!isValidRecordSize(3, "load"))
2230 return; 2290 return;
2231 Ice::Operand *Address = getRelativeOperand(Values[0], BaseIndex); 2291 Ice::Operand *Address = getRelativeOperand(Values[0], BaseIndex);
2232 Ice::Type Ty = Context->getSimpleTypeByID(Values[2]); 2292 Ice::Type Ty = Context->getSimpleTypeByID(Values[2]);
2233 unsigned Alignment; 2293 unsigned Alignment;
2234 extractAlignment("Load", Values[1], Alignment); 2294 extractAlignment("Load", Values[1], Alignment);
2235 if (isIRGenerationDisabled()) { 2295 if (isIRGenerationDisabled()) {
2236 assert(Address == nullptr); 2296 assert(Address == nullptr);
2237 setNextLocalInstIndex(nullptr); 2297 setNextLocalInstIndex(nullptr);
2238 return; 2298 return;
2239 } 2299 }
2240 if (!isValidPointerType(Address, "Load")) { 2300 if (!isValidPointerType(Address, "Load")) {
2241 appendErrorInstruction(Ty); 2301 appendErrorInstruction(Ty);
2242 return; 2302 return;
2243 } 2303 }
2244 if (!isValidLoadStoreAlignment(Alignment, Ty, "Load")) { 2304 if (!isValidLoadStoreAlignment(Alignment, Ty, "Load")) {
2245 appendErrorInstruction(Ty); 2305 appendErrorInstruction(Ty);
2246 return; 2306 return;
2247 } 2307 }
2248 CurrentNode->appendInst( 2308 CurrentNode->appendInst(
2249 Ice::InstLoad::create(Func, getNextInstVar(Ty), Address, Alignment)); 2309 Ice::InstLoad::create(Func, getNextInstVar(Ty), Address, Alignment));
2250 return; 2310 return;
2251 } 2311 }
2252 case naclbitc::FUNC_CODE_INST_STORE: { 2312 case naclbitc::FUNC_CODE_INST_STORE: {
2253 // STORE: [address, value, align] 2313 // STORE: [address, value, align]
2254 if (!isValidRecordSize(3, "function block store")) 2314 if (!isValidRecordSize(3, "store"))
2255 return; 2315 return;
2256 Ice::Operand *Address = getRelativeOperand(Values[0], BaseIndex); 2316 Ice::Operand *Address = getRelativeOperand(Values[0], BaseIndex);
2257 Ice::Operand *Value = getRelativeOperand(Values[1], BaseIndex); 2317 Ice::Operand *Value = getRelativeOperand(Values[1], BaseIndex);
2258 unsigned Alignment; 2318 unsigned Alignment;
2259 extractAlignment("Store", Values[2], Alignment); 2319 extractAlignment("Store", Values[2], Alignment);
2260 if (isIRGenerationDisabled()) { 2320 if (isIRGenerationDisabled()) {
2261 assert(Address == nullptr && Value == nullptr); 2321 assert(Address == nullptr && Value == nullptr);
2262 return; 2322 return;
2263 } 2323 }
2264 if (!isValidPointerType(Address, "Store")) 2324 if (!isValidPointerType(Address, "Store"))
(...skipping 11 matching lines...) Expand all
2276 // 2336 //
2277 // Note: The difference between CALL and CALL_INDIRECT is that 2337 // Note: The difference between CALL and CALL_INDIRECT is that
2278 // CALL has a reference to an explicit function declaration, while 2338 // CALL has a reference to an explicit function declaration, while
2279 // the CALL_INDIRECT is just an address. For CALL, we can infer 2339 // the CALL_INDIRECT is just an address. For CALL, we can infer
2280 // the return type by looking up the type signature associated 2340 // the return type by looking up the type signature associated
2281 // with the function declaration. For CALL_INDIRECT we can only 2341 // with the function declaration. For CALL_INDIRECT we can only
2282 // infer the type signature via argument types, and the 2342 // infer the type signature via argument types, and the
2283 // corresponding return type stored in CALL_INDIRECT record. 2343 // corresponding return type stored in CALL_INDIRECT record.
2284 Ice::SizeT ParamsStartIndex = 2; 2344 Ice::SizeT ParamsStartIndex = 2;
2285 if (Record.GetCode() == naclbitc::FUNC_CODE_INST_CALL) { 2345 if (Record.GetCode() == naclbitc::FUNC_CODE_INST_CALL) {
2286 if (!isValidRecordSizeAtLeast(2, "function block call")) 2346 if (!isValidRecordSizeAtLeast(2, "call"))
2287 return; 2347 return;
2288 } else { 2348 } else {
2289 if (!isValidRecordSizeAtLeast(3, "function block call indirect")) 2349 if (!isValidRecordSizeAtLeast(3, "call indirect"))
2290 return; 2350 return;
2291 ParamsStartIndex = 3; 2351 ParamsStartIndex = 3;
2292 } 2352 }
2293 2353
2294 // Extract out the called function and its return type. 2354 // Extract out the called function and its return type.
2295 uint32_t CalleeIndex = convertRelativeToAbsIndex(Values[1], BaseIndex); 2355 uint32_t CalleeIndex = convertRelativeToAbsIndex(Values[1], BaseIndex);
2296 Ice::Operand *Callee = getOperand(CalleeIndex); 2356 Ice::Operand *Callee = getOperand(CalleeIndex);
2297 Ice::Type ReturnType = Ice::IceType_void; 2357 Ice::Type ReturnType = Ice::IceType_void;
2298 const Ice::Intrinsics::FullIntrinsicInfo *IntrinsicInfo = nullptr; 2358 const Ice::Intrinsics::FullIntrinsicInfo *IntrinsicInfo = nullptr;
2299 if (Record.GetCode() == naclbitc::FUNC_CODE_INST_CALL) { 2359 if (Record.GetCode() == naclbitc::FUNC_CODE_INST_CALL) {
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
2408 break; 2468 break;
2409 } 2469 }
2410 } 2470 }
2411 } 2471 }
2412 2472
2413 CurrentNode->appendInst(Inst); 2473 CurrentNode->appendInst(Inst);
2414 return; 2474 return;
2415 } 2475 }
2416 case naclbitc::FUNC_CODE_INST_FORWARDTYPEREF: { 2476 case naclbitc::FUNC_CODE_INST_FORWARDTYPEREF: {
2417 // FORWARDTYPEREF: [opval, ty] 2477 // FORWARDTYPEREF: [opval, ty]
2418 if (!isValidRecordSize(2, "function block forward type ref")) 2478 if (!isValidRecordSize(2, "forward type ref"))
2419 return; 2479 return;
2420 Ice::Type OpType = Context->getSimpleTypeByID(Values[1]); 2480 Ice::Type OpType = Context->getSimpleTypeByID(Values[1]);
2421 setOperand(Values[0], 2481 setOperand(Values[0],
2422 isIRGenerationDisabled() ? nullptr : createInstVar(OpType)); 2482 isIRGenerationDisabled() ? nullptr : createInstVar(OpType));
2423 return; 2483 return;
2424 } 2484 }
2425 default: 2485 default:
2426 // Generate error message! 2486 // Generate error message!
2427 BlockParserBaseClass::ProcessRecord(); 2487 BlockParserBaseClass::ProcessRecord();
2428 return; 2488 return;
2429 } 2489 }
2430 } 2490 }
2431 2491
2432 /// Parses constants within a function block. 2492 /// Parses constants within a function block.
2433 class ConstantsParser : public BlockParserBaseClass { 2493 class ConstantsParser : public BlockParserBaseClass {
2434 ConstantsParser(const ConstantsParser &) = delete; 2494 ConstantsParser(const ConstantsParser &) = delete;
2435 ConstantsParser &operator=(const ConstantsParser &) = delete; 2495 ConstantsParser &operator=(const ConstantsParser &) = delete;
2436 2496
2437 public: 2497 public:
2438 ConstantsParser(unsigned BlockID, FunctionParser *FuncParser) 2498 ConstantsParser(unsigned BlockID, FunctionParser *FuncParser)
2439 : BlockParserBaseClass(BlockID, FuncParser), 2499 : BlockParserBaseClass(BlockID, FuncParser),
2440 Timer(Ice::TimerStack::TT_parseConstants, getTranslator().getContext()), 2500 Timer(Ice::TimerStack::TT_parseConstants, getTranslator().getContext()),
2441 FuncParser(FuncParser), NextConstantType(Ice::IceType_void) {} 2501 FuncParser(FuncParser), NextConstantType(Ice::IceType_void) {}
2442 2502
2443 ~ConstantsParser() override {} 2503 ~ConstantsParser() override {}
2444 2504
2505 const char *getBlockName() const override { return "constants"; }
2506
2445 private: 2507 private:
2446 Ice::TimerMarker Timer; 2508 Ice::TimerMarker Timer;
2447 // The parser of the function block this constants block appears in. 2509 // The parser of the function block this constants block appears in.
2448 FunctionParser *FuncParser; 2510 FunctionParser *FuncParser;
2449 // The type to use for succeeding constants. 2511 // The type to use for succeeding constants.
2450 Ice::Type NextConstantType; 2512 Ice::Type NextConstantType;
2451 2513
2452 void ProcessRecord() override; 2514 void ProcessRecord() override;
2453 2515
2454 Ice::GlobalContext *getContext() { return getTranslator().getContext(); } 2516 Ice::GlobalContext *getContext() { return getTranslator().getContext(); }
2455 2517
2456 // Returns true if the type to use for succeeding constants is defined. 2518 // Returns true if the type to use for succeeding constants is defined.
2457 // If false, also generates an error message. 2519 // If false, also generates an error message.
2458 bool isValidNextConstantType() { 2520 bool isValidNextConstantType() {
2459 if (NextConstantType != Ice::IceType_void) 2521 if (NextConstantType != Ice::IceType_void)
2460 return true; 2522 return true;
2461 Error("Constant record not preceded by set type record"); 2523 Error("Constant record not preceded by set type record");
2462 return false; 2524 return false;
2463 } 2525 }
2464 }; 2526 };
2465 2527
2466 void ConstantsParser::ProcessRecord() { 2528 void ConstantsParser::ProcessRecord() {
2467 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); 2529 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues();
2468 switch (Record.GetCode()) { 2530 switch (Record.GetCode()) {
2469 case naclbitc::CST_CODE_SETTYPE: { 2531 case naclbitc::CST_CODE_SETTYPE: {
2470 // SETTYPE: [typeid] 2532 // SETTYPE: [typeid]
2471 if (!isValidRecordSize(1, "constants block set type")) 2533 if (!isValidRecordSize(1, "set type"))
2472 return; 2534 return;
2473 NextConstantType = Context->getSimpleTypeByID(Values[0]); 2535 NextConstantType = Context->getSimpleTypeByID(Values[0]);
2474 if (NextConstantType == Ice::IceType_void) 2536 if (NextConstantType == Ice::IceType_void)
2475 Error("constants block set type not allowed for void type"); 2537 Error("constants block set type not allowed for void type");
2476 return; 2538 return;
2477 } 2539 }
2478 case naclbitc::CST_CODE_UNDEF: { 2540 case naclbitc::CST_CODE_UNDEF: {
2479 // UNDEF 2541 // UNDEF
2480 if (!isValidRecordSize(0, "constants block undef")) 2542 if (!isValidRecordSize(0, "undef"))
2481 return; 2543 return;
2482 if (!isValidNextConstantType()) 2544 if (!isValidNextConstantType())
2483 return; 2545 return;
2484 if (isIRGenerationDisabled()) { 2546 if (isIRGenerationDisabled()) {
2485 FuncParser->setNextConstantID(nullptr); 2547 FuncParser->setNextConstantID(nullptr);
2486 return; 2548 return;
2487 } 2549 }
2488 FuncParser->setNextConstantID( 2550 FuncParser->setNextConstantID(
2489 getContext()->getConstantUndef(NextConstantType)); 2551 getContext()->getConstantUndef(NextConstantType));
2490 return; 2552 return;
2491 } 2553 }
2492 case naclbitc::CST_CODE_INTEGER: { 2554 case naclbitc::CST_CODE_INTEGER: {
2493 // INTEGER: [intval] 2555 // INTEGER: [intval]
2494 if (!isValidRecordSize(1, "constants block integer")) 2556 if (!isValidRecordSize(1, "integer"))
2495 return; 2557 return;
2496 if (!isValidNextConstantType()) 2558 if (!isValidNextConstantType())
2497 return; 2559 return;
2498 if (isIRGenerationDisabled()) { 2560 if (isIRGenerationDisabled()) {
2499 FuncParser->setNextConstantID(nullptr); 2561 FuncParser->setNextConstantID(nullptr);
2500 return; 2562 return;
2501 } 2563 }
2502 if (auto IType = dyn_cast<IntegerType>( 2564 if (auto IType = dyn_cast<IntegerType>(
2503 Context->convertToLLVMType(NextConstantType))) { 2565 Context->convertToLLVMType(NextConstantType))) {
2504 APInt Value(IType->getBitWidth(), NaClDecodeSignRotatedValue(Values[0])); 2566 APInt Value(IType->getBitWidth(), NaClDecodeSignRotatedValue(Values[0]));
2505 if (Ice::Constant *C = getContext()->getConstantInt( 2567 if (Ice::Constant *C = getContext()->getConstantInt(
2506 NextConstantType, Value.getSExtValue())) { 2568 NextConstantType, Value.getSExtValue())) {
2507 FuncParser->setNextConstantID(C); 2569 FuncParser->setNextConstantID(C);
2508 return; 2570 return;
2509 } 2571 }
2510 } 2572 }
2511 std::string Buffer; 2573 std::string Buffer;
2512 raw_string_ostream StrBuf(Buffer); 2574 raw_string_ostream StrBuf(Buffer);
2513 StrBuf << "constant block integer record for non-integer type " 2575 StrBuf << "constant block integer record for non-integer type "
2514 << NextConstantType; 2576 << NextConstantType;
2515 Error(StrBuf.str()); 2577 Error(StrBuf.str());
2516 return; 2578 return;
2517 } 2579 }
2518 case naclbitc::CST_CODE_FLOAT: { 2580 case naclbitc::CST_CODE_FLOAT: {
2519 // FLOAT: [fpval] 2581 // FLOAT: [fpval]
2520 if (!isValidRecordSize(1, "constants block float")) 2582 if (!isValidRecordSize(1, "float"))
2521 return; 2583 return;
2522 if (!isValidNextConstantType()) 2584 if (!isValidNextConstantType())
2523 return; 2585 return;
2524 if (isIRGenerationDisabled()) { 2586 if (isIRGenerationDisabled()) {
2525 FuncParser->setNextConstantID(nullptr); 2587 FuncParser->setNextConstantID(nullptr);
2526 return; 2588 return;
2527 } 2589 }
2528 switch (NextConstantType) { 2590 switch (NextConstantType) {
2529 case Ice::IceType_f32: { 2591 case Ice::IceType_f32: {
2530 APFloat Value(APFloat::IEEEsingle, 2592 APFloat Value(APFloat::IEEEsingle,
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
2585 raw_string_ostream StrBuf(Buffer); 2647 raw_string_ostream StrBuf(Buffer);
2586 StrBuf << "Function-local " << Context << " name '" << Name 2648 StrBuf << "Function-local " << Context << " name '" << Name
2587 << "' can't be associated with index " << Index; 2649 << "' can't be associated with index " << Index;
2588 Error(StrBuf.str()); 2650 Error(StrBuf.str());
2589 } 2651 }
2590 }; 2652 };
2591 2653
2592 void FunctionValuesymtabParser::setValueName(uint64_t Index, StringType &Name) { 2654 void FunctionValuesymtabParser::setValueName(uint64_t Index, StringType &Name) {
2593 // Note: We check when Index is too small, so that we can error recover 2655 // Note: We check when Index is too small, so that we can error recover
2594 // (FP->getOperand will create fatal error). 2656 // (FP->getOperand will create fatal error).
2595 if (Index < getFunctionParser()->CachedNumGlobalValueIDs) { 2657 if (Index < getFunctionParser()->getNumGlobalIDs()) {
2596 reportUnableToAssign("instruction", Index, Name); 2658 reportUnableToAssign("instruction", Index, Name);
2597 // TODO(kschimpf) Remove error recovery once implementation complete. 2659 // TODO(kschimpf) Remove error recovery once implementation complete.
2598 return; 2660 return;
2599 } 2661 }
2600 if (isIRGenerationDisabled()) 2662 if (isIRGenerationDisabled())
2601 return; 2663 return;
2602 Ice::Operand *Op = getFunctionParser()->getOperand(Index); 2664 Ice::Operand *Op = getFunctionParser()->getOperand(Index);
2603 if (Ice::Variable *V = dyn_cast<Ice::Variable>(Op)) { 2665 if (Ice::Variable *V = dyn_cast<Ice::Variable>(Op)) {
2604 std::string Nm(Name.data(), Name.size()); 2666 std::string Nm(Name.data(), Name.size());
2605 V->setName(Nm); 2667 V->setName(Nm);
2606 } else { 2668 } else {
2607 reportUnableToAssign("variable", Index, Name); 2669 reportUnableToAssign("variable", Index, Name);
2608 } 2670 }
2609 } 2671 }
2610 2672
2611 void FunctionValuesymtabParser::setBbName(uint64_t Index, StringType &Name) { 2673 void FunctionValuesymtabParser::setBbName(uint64_t Index, StringType &Name) {
2612 if (isIRGenerationDisabled()) 2674 if (isIRGenerationDisabled())
2613 return; 2675 return;
2614 if (Index >= getFunctionParser()->Func->getNumNodes()) { 2676 if (Index >= getFunctionParser()->getFunc()->getNumNodes()) {
2615 reportUnableToAssign("block", Index, Name); 2677 reportUnableToAssign("block", Index, Name);
2616 return; 2678 return;
2617 } 2679 }
2618 std::string Nm(Name.data(), Name.size()); 2680 std::string Nm(Name.data(), Name.size());
2619 getFunctionParser()->Func->getNodes()[Index]->setName(Nm); 2681 getFunctionParser()->getFunc()->getNodes()[Index]->setName(Nm);
2620 } 2682 }
2621 2683
2622 bool FunctionParser::ParseBlock(unsigned BlockID) { 2684 bool FunctionParser::ParseBlock(unsigned BlockID) {
2623 switch (BlockID) { 2685 switch (BlockID) {
2624 case naclbitc::CONSTANTS_BLOCK_ID: { 2686 case naclbitc::CONSTANTS_BLOCK_ID: {
2625 ConstantsParser Parser(BlockID, this); 2687 ConstantsParser Parser(BlockID, this);
2626 return Parser.ParseThisBlock(); 2688 return Parser.ParseThisBlock();
2627 } 2689 }
2628 case naclbitc::VALUE_SYMTAB_BLOCK_ID: { 2690 case naclbitc::VALUE_SYMTAB_BLOCK_ID: {
2629 if (PNaClAllowLocalSymbolTables) { 2691 if (PNaClAllowLocalSymbolTables) {
(...skipping 12 matching lines...) Expand all
2642 class ModuleParser : public BlockParserBaseClass { 2704 class ModuleParser : public BlockParserBaseClass {
2643 public: 2705 public:
2644 ModuleParser(unsigned BlockID, TopLevelParser *Context) 2706 ModuleParser(unsigned BlockID, TopLevelParser *Context)
2645 : BlockParserBaseClass(BlockID, Context), 2707 : BlockParserBaseClass(BlockID, Context),
2646 Timer(Ice::TimerStack::TT_parseModule, 2708 Timer(Ice::TimerStack::TT_parseModule,
2647 Context->getTranslator().getContext()), 2709 Context->getTranslator().getContext()),
2648 GlobalDeclarationNamesAndInitializersInstalled(false) {} 2710 GlobalDeclarationNamesAndInitializersInstalled(false) {}
2649 2711
2650 ~ModuleParser() override {} 2712 ~ModuleParser() override {}
2651 2713
2714 const char *getBlockName() const override { return "module"; }
2715
2652 private: 2716 private:
2653 Ice::TimerMarker Timer; 2717 Ice::TimerMarker Timer;
2654 // True if we have already installed names for unnamed global declarations, 2718 // True if we have already installed names for unnamed global declarations,
2655 // and have generated global constant initializers. 2719 // and have generated global constant initializers.
2656 bool GlobalDeclarationNamesAndInitializersInstalled; 2720 bool GlobalDeclarationNamesAndInitializersInstalled;
2657 2721
2658 // Generates names for unnamed global addresses (i.e. functions and 2722 // Generates names for unnamed global addresses (i.e. functions and
2659 // global variables). Then lowers global variable declaration 2723 // global variables). Then lowers global variable declaration
2660 // initializers to the target. May be called multiple times. Only 2724 // initializers to the target. May be called multiple times. Only
2661 // the first call will do the installation. 2725 // the first call will do the installation.
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
2761 default: 2825 default:
2762 return BlockParserBaseClass::ParseBlock(BlockID); 2826 return BlockParserBaseClass::ParseBlock(BlockID);
2763 } 2827 }
2764 } 2828 }
2765 2829
2766 void ModuleParser::ProcessRecord() { 2830 void ModuleParser::ProcessRecord() {
2767 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); 2831 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues();
2768 switch (Record.GetCode()) { 2832 switch (Record.GetCode()) {
2769 case naclbitc::MODULE_CODE_VERSION: { 2833 case naclbitc::MODULE_CODE_VERSION: {
2770 // VERSION: [version#] 2834 // VERSION: [version#]
2771 if (!isValidRecordSize(1, "Module version")) 2835 if (!isValidRecordSize(1, "version"))
2772 return; 2836 return;
2773 unsigned Version = Values[0]; 2837 unsigned Version = Values[0];
2774 if (Version != 1) { 2838 if (Version != 1) {
2775 std::string Buffer; 2839 std::string Buffer;
2776 raw_string_ostream StrBuf(Buffer); 2840 raw_string_ostream StrBuf(Buffer);
2777 StrBuf << "Unknown bitstream version: " << Version; 2841 StrBuf << "Unknown bitstream version: " << Version;
2778 Error(StrBuf.str()); 2842 Error(StrBuf.str());
2779 } 2843 }
2780 return; 2844 return;
2781 } 2845 }
2782 case naclbitc::MODULE_CODE_FUNCTION: { 2846 case naclbitc::MODULE_CODE_FUNCTION: {
2783 // FUNCTION: [type, callingconv, isproto, linkage] 2847 // FUNCTION: [type, callingconv, isproto, linkage]
2784 if (!isValidRecordSize(4, "Function heading")) 2848 if (!isValidRecordSize(4, "address"))
2785 return; 2849 return;
2786 const Ice::FuncSigType &Signature = Context->getFuncSigTypeByID(Values[0]); 2850 const Ice::FuncSigType &Signature = Context->getFuncSigTypeByID(Values[0]);
2787 CallingConv::ID CallingConv; 2851 CallingConv::ID CallingConv;
2788 if (!naclbitc::DecodeCallingConv(Values[1], CallingConv)) { 2852 if (!naclbitc::DecodeCallingConv(Values[1], CallingConv)) {
2789 std::string Buffer; 2853 std::string Buffer;
2790 raw_string_ostream StrBuf(Buffer); 2854 raw_string_ostream StrBuf(Buffer);
2791 StrBuf << "Function heading has unknown calling convention: " 2855 StrBuf << "Function address has unknown calling convention: "
2792 << Values[1]; 2856 << Values[1];
2793 Error(StrBuf.str()); 2857 Error(StrBuf.str());
2794 return; 2858 return;
2795 } 2859 }
2796 GlobalValue::LinkageTypes Linkage; 2860 GlobalValue::LinkageTypes Linkage;
2797 if (!naclbitc::DecodeLinkage(Values[3], Linkage)) { 2861 if (!naclbitc::DecodeLinkage(Values[3], Linkage)) {
2798 std::string Buffer; 2862 std::string Buffer;
2799 raw_string_ostream StrBuf(Buffer); 2863 raw_string_ostream StrBuf(Buffer);
2800 StrBuf << "Function heading has unknown linkage. Found " << Values[3]; 2864 StrBuf << "Function address has unknown linkage. Found " << Values[3];
2801 Error(StrBuf.str()); 2865 Error(StrBuf.str());
2802 return; 2866 return;
2803 } 2867 }
2804 Ice::FunctionDeclaration *Func = Ice::FunctionDeclaration::create( 2868 Ice::FunctionDeclaration *Func = Ice::FunctionDeclaration::create(
2805 getTranslator().getContext(), Signature, CallingConv, Linkage, 2869 getTranslator().getContext(), Signature, CallingConv, Linkage,
2806 Values[2] == 0); 2870 Values[2] == 0);
2807 if (Values[2] == 0) 2871 if (Values[2] == 0)
2808 Context->setNextValueIDAsImplementedFunction(); 2872 Context->setNextValueIDAsImplementedFunction();
2809 Context->setNextFunctionID(Func); 2873 Context->setNextFunctionID(Func);
2810 return; 2874 return;
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
2874 2938
2875 if (TopLevelBlocks != 1) { 2939 if (TopLevelBlocks != 1) {
2876 errs() << IRFilename 2940 errs() << IRFilename
2877 << ": Contains more than one module. Found: " << TopLevelBlocks 2941 << ": Contains more than one module. Found: " << TopLevelBlocks
2878 << "\n"; 2942 << "\n";
2879 ErrorStatus = true; 2943 ErrorStatus = true;
2880 } 2944 }
2881 } 2945 }
2882 2946
2883 } // end of namespace Ice 2947 } // end of namespace Ice
OLDNEW
« no previous file with comments | « no previous file | tests_lit/parse_errs/insertextract-err.ll » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698