/* This file is part of solidity. solidity is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. solidity is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with solidity. If not, see . */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace dev; using namespace langutil; using namespace yul; using namespace yul::test; using namespace dev::solidity; using namespace dev::solidity::test; using namespace std; YulOptimizerTest::YulOptimizerTest(string const& _filename) { boost::filesystem::path path(_filename); if (path.empty() || std::next(path.begin()) == path.end() || std::next(std::next(path.begin())) == path.end()) BOOST_THROW_EXCEPTION(runtime_error("Filename path has to contain a directory: \"" + _filename + "\".")); m_optimizerStep = std::prev(std::prev(path.end()))->string(); ifstream file(_filename); if (!file) BOOST_THROW_EXCEPTION(runtime_error("Cannot open test case: \"" + _filename + "\".")); file.exceptions(ios::badbit); string line; while (getline(file, line)) { if (boost::algorithm::starts_with(line, "// ----")) break; if (m_source.empty() && boost::algorithm::starts_with(line, "// yul")) m_yul = true; m_source += line + "\n"; } while (getline(file, line)) if (boost::algorithm::starts_with(line, "// ")) m_expectation += line.substr(3) + "\n"; else m_expectation += line + "\n"; } bool YulOptimizerTest::run(ostream& _stream, string const& _linePrefix, bool const _formatted) { yul::AsmPrinter printer{m_yul}; shared_ptr ast; shared_ptr analysisInfo; if (!parse(_stream, _linePrefix, _formatted)) return false; if (m_optimizerStep == "disambiguator") disambiguate(); else if (m_optimizerStep == "blockFlattener") { disambiguate(); BlockFlattener{}(*m_ast); } else if (m_optimizerStep == "varDeclPropagator") { disambiguate(); VarDeclPropagator{}(*m_ast); } else if (m_optimizerStep == "forLoopInitRewriter") { disambiguate(); ForLoopInitRewriter{}(*m_ast); } else if (m_optimizerStep == "commonSubexpressionEliminator") { disambiguate(); (CommonSubexpressionEliminator{})(*m_ast); } else if (m_optimizerStep == "expressionSplitter") { NameDispenser nameDispenser(*m_ast); ExpressionSplitter{nameDispenser}(*m_ast); } else if (m_optimizerStep == "expressionJoiner") { disambiguate(); ExpressionJoiner::run(*m_ast); } else if (m_optimizerStep == "splitJoin") { disambiguate(); NameDispenser nameDispenser(*m_ast); ExpressionSplitter{nameDispenser}(*m_ast); ExpressionJoiner::run(*m_ast); ExpressionJoiner::run(*m_ast); } else if (m_optimizerStep == "functionGrouper") { disambiguate(); (FunctionGrouper{})(*m_ast); } else if (m_optimizerStep == "functionHoister") { disambiguate(); (FunctionHoister{})(*m_ast); } else if (m_optimizerStep == "expressionInliner") { disambiguate(); ExpressionInliner(*m_ast).run(); } else if (m_optimizerStep == "fullInliner") { disambiguate(); (FunctionHoister{})(*m_ast); (FunctionGrouper{})(*m_ast); NameDispenser nameDispenser(*m_ast); ExpressionSplitter{nameDispenser}(*m_ast); FullInliner(*m_ast, nameDispenser).run(); ExpressionJoiner::run(*m_ast); } else if (m_optimizerStep == "mainFunction") { disambiguate(); (FunctionGrouper{})(*m_ast); (MainFunction{})(*m_ast); } else if (m_optimizerStep == "rematerialiser") { disambiguate(); (Rematerialiser{})(*m_ast); } else if (m_optimizerStep == "expressionSimplifier") { disambiguate(); ExpressionSimplifier::run(*m_ast); } else if (m_optimizerStep == "fullSimplify") { disambiguate(); NameDispenser nameDispenser(*m_ast); ExpressionSplitter{nameDispenser}(*m_ast); CommonSubexpressionEliminator{}(*m_ast); ExpressionSimplifier::run(*m_ast); UnusedPruner::runUntilStabilised(*m_ast); ExpressionJoiner::run(*m_ast); ExpressionJoiner::run(*m_ast); } else if (m_optimizerStep == "unusedPruner") { disambiguate(); UnusedPruner::runUntilStabilised(*m_ast); } else if (m_optimizerStep == "ssaTransform") { disambiguate(); NameDispenser nameDispenser(*m_ast); SSATransform::run(*m_ast, nameDispenser); } else if (m_optimizerStep == "redundantAssignEliminator") { disambiguate(); RedundantAssignEliminator::run(*m_ast); } else if (m_optimizerStep == "ssaPlusCleanup") { disambiguate(); NameDispenser nameDispenser(*m_ast); SSATransform::run(*m_ast, nameDispenser); RedundantAssignEliminator::run(*m_ast); } else if (m_optimizerStep == "fullSuite") OptimiserSuite::run(*m_ast, *m_analysisInfo); else { FormattedScope(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Invalid optimizer step: " << m_optimizerStep << endl; return false; } m_obtainedResult = m_optimizerStep + "\n" + printer(*m_ast) + "\n"; if (m_expectation != m_obtainedResult) { string nextIndentLevel = _linePrefix + " "; FormattedScope(_stream, _formatted, {formatting::BOLD, formatting::CYAN}) << _linePrefix << "Expected result:" << endl; // TODO could compute a simple diff with highlighted lines printIndented(_stream, m_expectation, nextIndentLevel); FormattedScope(_stream, _formatted, {formatting::BOLD, formatting::CYAN}) << _linePrefix << "Obtained result:" << endl; printIndented(_stream, m_obtainedResult, nextIndentLevel); return false; } return true; } void YulOptimizerTest::printSource(ostream& _stream, string const& _linePrefix, bool const) const { printIndented(_stream, m_source, _linePrefix); } void YulOptimizerTest::printUpdatedExpectations(ostream& _stream, string const& _linePrefix) const { printIndented(_stream, m_obtainedResult, _linePrefix); } void YulOptimizerTest::printIndented(ostream& _stream, string const& _output, string const& _linePrefix) const { stringstream output(_output); string line; while (getline(output, line)) _stream << _linePrefix << line << endl; } bool YulOptimizerTest::parse(ostream& _stream, string const& _linePrefix, bool const _formatted) { yul::AsmFlavour flavour = m_yul ? yul::AsmFlavour::Yul : yul::AsmFlavour::Strict; ErrorList errors; ErrorReporter errorReporter(errors); shared_ptr scanner = make_shared(CharStream(m_source, "")); m_ast = yul::Parser(errorReporter, flavour).parse(scanner, false); if (!m_ast || !errorReporter.errors().empty()) { FormattedScope(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Error parsing source." << endl; printErrors(_stream, errorReporter.errors(), *scanner); return false; } m_analysisInfo = make_shared(); yul::AsmAnalyzer analyzer( *m_analysisInfo, errorReporter, dev::test::Options::get().evmVersion(), boost::none, flavour ); if (!analyzer.analyze(*m_ast) || !errorReporter.errors().empty()) { FormattedScope(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Error analyzing source." << endl; printErrors(_stream, errorReporter.errors(), *scanner); return false; } return true; } void YulOptimizerTest::disambiguate() { *m_ast = boost::get(Disambiguator(*m_analysisInfo)(*m_ast)); m_analysisInfo.reset(); } void YulOptimizerTest::printErrors(ostream& _stream, ErrorList const& _errors, Scanner const& _scanner) { SourceReferenceFormatter formatter(_stream, [&](string const&) -> Scanner const& { return _scanner; }); for (auto const& error: _errors) formatter.printExceptionInformation( *error, (error->type() == Error::Type::Warning) ? "Warning" : "Error" ); } ons'>+0 * - Update to 2.02amdmi32016-07-082-4/+5 * Remove ${PORTSDIR}/ from dependencies, categories v, w, x, y, and z.mat2016-04-011-8/+8 * Change the way Perl modules are installed, update the default Perl to 5.18.mat2014-11-262-10/+1 * - Update to 2.00culot2014-05-263-7/+7 * - Update to 1.02 [1]culot2014-02-202-6/+6 * - stage supportaz2014-01-053-5/+3 * Add NO_STAGE all over the place in preparation for the staging support (cat: ...bapt2013-09-211-0/+1 * - convert to the new perl5 frameworkaz2013-09-021-1/+2 * - Update to 0.09010 [1]culot2012-10-262-9/+8 * - Add TEST_DEPENDSswills2012-04-121-0/+2 * - Update to 0.09002sunpoet2011-11-192-5/+5 * - Update to 0.09000culot2011-04-042-4/+4 * - Get Rid MD5 supportmiwi2011-03-191-1/+0 * - Also remove them from pkg-plist lists not from me that crept inpgollucci2010-11-241-1/+0 * - Update to 0.08002wen2010-10-093-17/+17 * - only 13% of the p5- ports embed @comment $FreeBSD$:pgollucci2010-09-241-1/+0 * - Update to 0.06000wen2010-01-182-4/+5 * - Update to 0.05000pgollucci2009-06-202-7/+10