Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 //===- subzero/src/PNaClTranslator.cpp - ICE from bitcode -----------------===// | 1 //===- subzero/src/PNaClTranslator.cpp - ICE from bitcode -----------------===// |
| 2 // | 2 // |
| 3 // The Subzero Code Generator | 3 // The Subzero Code Generator |
| 4 // | 4 // |
| 5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
| 7 // | 7 // |
| 8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
| 9 // | 9 // |
| 10 // This file implements the PNaCl bitcode file to Ice, to machine code | 10 // This file implements the PNaCl bitcode file to Ice, to machine code |
| (...skipping 803 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 814 CurrentNode = InstallNextBasicBlock(); | 814 CurrentNode = InstallNextBasicBlock(); |
| 815 for (Function::const_arg_iterator ArgI = LLVMFunc->arg_begin(), | 815 for (Function::const_arg_iterator ArgI = LLVMFunc->arg_begin(), |
| 816 ArgE = LLVMFunc->arg_end(); | 816 ArgE = LLVMFunc->arg_end(); |
| 817 ArgI != ArgE; ++ArgI) { | 817 ArgI != ArgE; ++ArgI) { |
| 818 Func->addArg(NextInstVar(Context->convertToIceType(ArgI->getType()))); | 818 Func->addArg(NextInstVar(Context->convertToIceType(ArgI->getType()))); |
| 819 } | 819 } |
| 820 } | 820 } |
| 821 | 821 |
| 822 ~FunctionParser() LLVM_OVERRIDE; | 822 ~FunctionParser() LLVM_OVERRIDE; |
| 823 | 823 |
| 824 // Set the next constant ID to the given constant C. | |
| 825 void setNextConstantID(Ice::Constant *C) { | |
| 826 LocalOperands.push_back(C); | |
| 827 } | |
| 828 | |
| 824 private: | 829 private: |
| 825 // Timer for reading function bitcode and converting to ICE. | 830 // Timer for reading function bitcode and converting to ICE. |
| 826 Ice::Timer TConvert; | 831 Ice::Timer TConvert; |
| 827 // The corresponding ICE function defined by the function block. | 832 // The corresponding ICE function defined by the function block. |
| 828 Ice::Cfg *Func; | 833 Ice::Cfg *Func; |
| 829 // The index to the current basic block being built. | 834 // The index to the current basic block being built. |
| 830 uint32_t CurrentBbIndex; | 835 uint32_t CurrentBbIndex; |
| 831 // The basic block being built. | 836 // The basic block being built. |
| 832 Ice::CfgNode *CurrentNode; | 837 Ice::CfgNode *CurrentNode; |
| 833 // The ID for the function. | 838 // The ID for the function. |
| 834 unsigned FcnId; | 839 unsigned FcnId; |
| 835 // The corresponding LLVM function. | 840 // The corresponding LLVM function. |
| 836 Function *LLVMFunc; | 841 Function *LLVMFunc; |
| 837 // Holds operands local to the function block, based on indices | 842 // Holds operands local to the function block, based on indices |
| 838 // defined in the bitcode file. | 843 // defined in the bitcode file. |
| 839 std::vector<Ice::Operand *> LocalOperands; | 844 std::vector<Ice::Operand *> LocalOperands; |
| 840 // Holds the dividing point between local and global absolute value indices. | 845 // Holds the dividing point between local and global absolute value indices. |
| 841 uint32_t CachedNumGlobalValueIDs; | 846 uint32_t CachedNumGlobalValueIDs; |
| 842 // True if the last processed instruction was a terminating | 847 // True if the last processed instruction was a terminating |
| 843 // instruction. | 848 // instruction. |
| 844 bool InstIsTerminating; | 849 bool InstIsTerminating; |
| 845 | 850 |
| 851 virtual bool ParseBlock(unsigned BlockID) LLVM_OVERRIDE; | |
| 852 | |
| 846 virtual void ProcessRecord() LLVM_OVERRIDE; | 853 virtual void ProcessRecord() LLVM_OVERRIDE; |
| 847 | 854 |
| 848 virtual void ExitBlock() LLVM_OVERRIDE; | 855 virtual void ExitBlock() LLVM_OVERRIDE; |
| 849 | 856 |
| 850 // Creates and appends a new basic block to the list of basic blocks. | 857 // Creates and appends a new basic block to the list of basic blocks. |
| 851 Ice::CfgNode *InstallNextBasicBlock() { return Func->makeNode(); } | 858 Ice::CfgNode *InstallNextBasicBlock() { return Func->makeNode(); } |
| 852 | 859 |
| 853 // Returns the Index-th basic block in the list of basic blocks. | 860 // Returns the Index-th basic block in the list of basic blocks. |
| 854 Ice::CfgNode *GetBasicBlock(uint32_t Index) { | 861 Ice::CfgNode *GetBasicBlock(uint32_t Index) { |
| 855 const Ice::NodeList &Nodes = Func->getNodes(); | 862 const Ice::NodeList &Nodes = Func->getNodes(); |
| (...skipping 656 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1512 } | 1519 } |
| 1513 default: | 1520 default: |
| 1514 // Generate error message! | 1521 // Generate error message! |
| 1515 BlockParserBaseClass::ProcessRecord(); | 1522 BlockParserBaseClass::ProcessRecord(); |
| 1516 break; | 1523 break; |
| 1517 } | 1524 } |
| 1518 if (Inst) | 1525 if (Inst) |
| 1519 CurrentNode->appendInst(Inst); | 1526 CurrentNode->appendInst(Inst); |
| 1520 } | 1527 } |
| 1521 | 1528 |
| 1529 /// Parses constants within a function block. | |
| 1530 class ConstantsParser : public BlockParserBaseClass { | |
| 1531 ConstantsParser(const ConstantsParser &) LLVM_DELETED_FUNCTION; | |
| 1532 ConstantsParser &operator=(const ConstantsParser &) LLVM_DELETED_FUNCTION; | |
| 1533 | |
| 1534 public: | |
| 1535 ConstantsParser(unsigned BlockID, FunctionParser *FuncParser) | |
| 1536 : BlockParserBaseClass(BlockID, FuncParser), | |
| 1537 FuncParser(FuncParser), | |
| 1538 NextConstantType(Ice::IceType_void) {} | |
| 1539 | |
| 1540 ~ConstantsParser() LLVM_OVERRIDE {} | |
| 1541 | |
| 1542 private: | |
| 1543 // The parser of the function block this constants block appears in. | |
| 1544 FunctionParser *FuncParser; | |
| 1545 // The type to use for succeeding constants. | |
| 1546 Ice::Type NextConstantType; | |
| 1547 | |
| 1548 virtual void ProcessRecord() LLVM_OVERRIDE; | |
| 1549 | |
| 1550 Ice::GlobalContext *getContext() { | |
| 1551 return getTranslator().getContext(); | |
| 1552 } | |
| 1553 | |
| 1554 // Returns true if the type to use for succeeding constants is defined. | |
| 1555 // If false, also generates an error message. | |
| 1556 bool isValidNextConstantType() { | |
| 1557 if (NextConstantType != Ice::IceType_void) return true; | |
|
Jim Stichnoth
2014/09/05 23:34:29
return on separate line, i.e. run "make format-dif
Karl
2014/09/08 20:41:39
Done.
| |
| 1558 Error("Constant record not preceded by set type record"); | |
| 1559 return false; | |
| 1560 } | |
| 1561 }; | |
| 1562 | |
| 1563 void ConstantsParser::ProcessRecord() { | |
| 1564 const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); | |
| 1565 switch (Record.GetCode()) { | |
| 1566 case naclbitc::CST_CODE_SETTYPE: { | |
| 1567 // SETTYPE: [typeid] | |
| 1568 if (!isValidRecordSize(1, "constants block set type")) return; | |
| 1569 NextConstantType | |
| 1570 = Context->convertToIceType(Context->getTypeByID(Values[0])); | |
| 1571 if (NextConstantType == Ice::IceType_void) | |
| 1572 Error("constants block set type not allowed for void type"); | |
| 1573 return; | |
| 1574 } | |
| 1575 case naclbitc::CST_CODE_UNDEF: { | |
| 1576 // UNDEF | |
| 1577 if (!isValidRecordSize(0, "constants block undef")) return; | |
| 1578 if (!isValidNextConstantType()) return; | |
| 1579 FuncParser->setNextConstantID( | |
| 1580 getContext()->getConstantUndef(NextConstantType)); | |
| 1581 return; | |
| 1582 } | |
| 1583 case naclbitc::CST_CODE_INTEGER: { | |
| 1584 // INTEGER: [intval] | |
| 1585 if (!isValidRecordSize(1, "constants block integer")) return; | |
| 1586 if (!isValidNextConstantType()) return; | |
| 1587 if (IntegerType *IType = dyn_cast<IntegerType>( | |
| 1588 Context->convertToLLVMType(NextConstantType))) { | |
|
jvoung (off chromium)
2014/09/06 17:53:47
Might just be able to check the IceType's typeWidt
Karl
2014/09/08 20:41:39
Since we already depend of LLVM typesfor the types
| |
| 1589 APInt Value(IType->getBitWidth(), | |
| 1590 NaClDecodeSignRotatedValue(Values[0])); | |
| 1591 Ice::Constant *C = getContext()->getConstantInt( | |
| 1592 NextConstantType, Value.getSExtValue()); | |
| 1593 FuncParser->setNextConstantID(C); | |
| 1594 return; | |
| 1595 } | |
| 1596 std::string Buffer; | |
| 1597 raw_string_ostream StrBuf(Buffer); | |
| 1598 StrBuf << "constant block integer record for non-integer type " | |
| 1599 << NextConstantType; | |
| 1600 Error(StrBuf.str()); | |
| 1601 return; | |
| 1602 } | |
| 1603 case naclbitc::CST_CODE_FLOAT: { | |
| 1604 // FLOAT: [fpval] | |
| 1605 if (!isValidRecordSize(1, "constants block float")) return; | |
| 1606 if (!isValidNextConstantType()) return; | |
| 1607 switch (NextConstantType) { | |
| 1608 case Ice::IceType_f32: { | |
| 1609 APFloat Value(APFloat::IEEEsingle, | |
| 1610 APInt(32, static_cast<uint32_t>(Values[0]))); | |
| 1611 FuncParser->setNextConstantID( | |
| 1612 getContext()->getConstantFloat(Value.convertToFloat())); | |
| 1613 return; | |
| 1614 } | |
| 1615 case Ice::IceType_f64: { | |
| 1616 APFloat Value(APFloat::IEEEdouble, APInt(64, Values[0])); | |
| 1617 FuncParser->setNextConstantID( | |
| 1618 getContext()->getConstantDouble(Value.convertToDouble())); | |
| 1619 return; | |
| 1620 } | |
| 1621 default: { | |
| 1622 std::string Buffer; | |
| 1623 raw_string_ostream StrBuf(Buffer); | |
| 1624 StrBuf << "constant block float record for non-floating type " | |
| 1625 << NextConstantType; | |
| 1626 Error(StrBuf.str()); | |
| 1627 return; | |
| 1628 } | |
| 1629 } | |
| 1630 } | |
| 1631 default: | |
| 1632 // Generate error message! | |
| 1633 BlockParserBaseClass::ProcessRecord(); | |
| 1634 return; | |
| 1635 } | |
| 1636 } | |
| 1637 | |
| 1638 bool FunctionParser::ParseBlock(unsigned BlockID) { | |
| 1639 switch (BlockID) { | |
| 1640 case naclbitc::CONSTANTS_BLOCK_ID: { | |
| 1641 ConstantsParser Parser(BlockID, this); | |
| 1642 return Parser.ParseThisBlock(); | |
| 1643 } | |
| 1644 default: | |
| 1645 return BlockParserBaseClass::ParseBlock(BlockID); | |
| 1646 } | |
| 1647 } | |
| 1648 | |
| 1522 /// Parses the module block in the bitcode file. | 1649 /// Parses the module block in the bitcode file. |
| 1523 class ModuleParser : public BlockParserBaseClass { | 1650 class ModuleParser : public BlockParserBaseClass { |
| 1524 public: | 1651 public: |
| 1525 ModuleParser(unsigned BlockID, TopLevelParser *Context) | 1652 ModuleParser(unsigned BlockID, TopLevelParser *Context) |
| 1526 : BlockParserBaseClass(BlockID, Context) {} | 1653 : BlockParserBaseClass(BlockID, Context) {} |
| 1527 | 1654 |
| 1528 virtual ~ModuleParser() LLVM_OVERRIDE {} | 1655 virtual ~ModuleParser() LLVM_OVERRIDE {} |
| 1529 | 1656 |
| 1530 protected: | 1657 protected: |
| 1531 virtual bool ParseBlock(unsigned BlockID) LLVM_OVERRIDE; | 1658 virtual bool ParseBlock(unsigned BlockID) LLVM_OVERRIDE; |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1677 if (TopLevelBlocks != 1) { | 1804 if (TopLevelBlocks != 1) { |
| 1678 errs() << IRFilename | 1805 errs() << IRFilename |
| 1679 << ": Contains more than one module. Found: " << TopLevelBlocks | 1806 << ": Contains more than one module. Found: " << TopLevelBlocks |
| 1680 << "\n"; | 1807 << "\n"; |
| 1681 ErrorStatus = true; | 1808 ErrorStatus = true; |
| 1682 } | 1809 } |
| 1683 return; | 1810 return; |
| 1684 } | 1811 } |
| 1685 | 1812 |
| 1686 } // end of namespace Ice | 1813 } // end of namespace Ice |
| OLD | NEW |