Chromium Code Reviews

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