/*
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
#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 solidity;
using namespace solidity::util;
using namespace solidity::langutil;
using namespace solidity::yul;
using namespace solidity::yul::test;
using namespace solidity::frontend;
using namespace solidity::frontend::test;
using namespace std;
YulOptimizerTest::YulOptimizerTest(string const& _filename):
EVMVersionRestrictedTestCase(_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();
m_source = m_reader.source();
auto dialectName = m_reader.stringSetting("dialect", "evm");
m_dialect = &dialect(dialectName, solidity::test::CommonOptions::get().evmVersion());
m_expectation = m_reader.simpleExpectations();
}
TestCase::TestResult YulOptimizerTest::run(ostream& _stream, string const& _linePrefix, bool const _formatted)
{
if (!parse(_stream, _linePrefix, _formatted))
return TestResult::FatalError;
soltestAssert(m_dialect, "Dialect not set.");
updateContext();
if (m_optimizerStep == "disambiguator")
disambiguate();
else if (m_optimizerStep == "nameDisplacer")
{
disambiguate();
NameDisplacer{
*m_nameDispenser,
{"illegal1"_yulstring, "illegal2"_yulstring, "illegal3"_yulstring, "illegal4"_yulstring, "illegal5"_yulstring}
}(*m_ast);
}
else if (m_optimizerStep == "blockFlattener")
{
disambiguate();
BlockFlattener::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "constantOptimiser")
{
GasMeter meter(dynamic_cast(*m_dialect), false, 200);
ConstantOptimiser{dynamic_cast(*m_dialect), meter}(*m_ast);
}
else if (m_optimizerStep == "varDeclInitializer")
VarDeclInitializer::run(*m_context, *m_ast);
else if (m_optimizerStep == "varNameCleaner")
VarNameCleaner::run(*m_context, *m_ast);
else if (m_optimizerStep == "forLoopConditionIntoBody")
{
disambiguate();
ForLoopConditionIntoBody::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "forLoopInitRewriter")
{
disambiguate();
ForLoopInitRewriter::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "commonSubexpressionEliminator")
{
disambiguate();
CommonSubexpressionEliminator::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "conditionalUnsimplifier")
{
disambiguate();
ConditionalUnsimplifier::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "conditionalSimplifier")
{
disambiguate();
ConditionalSimplifier::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "expressionSplitter")
ExpressionSplitter::run(*m_context, *m_ast);
else if (m_optimizerStep == "expressionJoiner")
{
disambiguate();
ExpressionJoiner::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "splitJoin")
{
disambiguate();
ExpressionSplitter::run(*m_context, *m_ast);
ExpressionJoiner::run(*m_context, *m_ast);
ExpressionJoiner::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "functionGrouper")
{
disambiguate();
FunctionGrouper::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "functionHoister")
{
disambiguate();
FunctionHoister::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "expressionInliner")
{
disambiguate();
ExpressionInliner::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "fullInliner")
{
disambiguate();
FunctionHoister::run(*m_context, *m_ast);
FunctionGrouper::run(*m_context, *m_ast);
ExpressionSplitter::run(*m_context, *m_ast);
FullInliner::run(*m_context, *m_ast);
ExpressionJoiner::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "mainFunction")
{
disambiguate();
FunctionGrouper::run(*m_context, *m_ast);
MainFunction::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "rematerialiser")
{
disambiguate();
Rematerialiser::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "expressionSimplifier")
{
disambiguate();
ExpressionSimplifier::run(*m_context, *m_ast);
ExpressionSimplifier::run(*m_context, *m_ast);
ExpressionSimplifier::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "fullSimplify")
{
disambiguate();
ExpressionSplitter::run(*m_context, *m_ast);
ForLoopInitRewriter::run(*m_context, *m_ast);
CommonSubexpressionEliminator::run(*m_context, *m_ast);
ExpressionSimplifier::run(*m_context, *m_ast);
UnusedPruner::run(*m_context, *m_ast);
CircularReferencesPruner::run(*m_context, *m_ast);
DeadCodeEliminator::run(*m_context, *m_ast);
ExpressionJoiner::run(*m_context, *m_ast);
ExpressionJoiner::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "unusedPruner")
{
disambiguate();
UnusedPruner::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "circularReferencesPruner")
{
disambiguate();
FunctionHoister::run(*m_context, *m_ast);
CircularReferencesPruner::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "deadCodeEliminator")
{
disambiguate();
ForLoopInitRewriter::run(*m_context, *m_ast);
DeadCodeEliminator::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "ssaTransform")
{
disambiguate();
ForLoopInitRewriter::run(*m_context, *m_ast);
SSATransform::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "redundantAssignEliminator")
{
disambiguate();
RedundantAssignEliminator::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "ssaPlusCleanup")
{
disambiguate();
SSATransform::run(*m_context, *m_ast);
RedundantAssignEliminator::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "loadResolver")
{
disambiguate();
ForLoopInitRewriter::run(*m_context, *m_ast);
ExpressionSplitter::run(*m_context, *m_ast);
CommonSubexpressionEliminator::run(*m_context, *m_ast);
ExpressionSimplifier::run(*m_context, *m_ast);
LoadResolver::run(*m_context, *m_ast);
UnusedPruner::run(*m_context, *m_ast);
ExpressionJoiner::run(*m_context, *m_ast);
ExpressionJoiner::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "loopInvariantCodeMotion")
{
disambiguate();
ForLoopInitRewriter::run(*m_context, *m_ast);
LoopInvariantCodeMotion::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "controlFlowSimplifier")
{
disambiguate();
ControlFlowSimplifier::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "structuralSimplifier")
{
disambiguate();
ForLoopInitRewriter::run(*m_context, *m_ast);
LiteralRematerialiser::run(*m_context, *m_ast);
StructuralSimplifier::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "equivalentFunctionCombiner")
{
disambiguate();
EquivalentFunctionCombiner::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "ssaReverser")
{
disambiguate();
SSAReverser::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "ssaAndBack")
{
disambiguate();
// apply SSA
SSATransform::run(*m_context, *m_ast);
RedundantAssignEliminator::run(*m_context, *m_ast);
// reverse SSA
SSAReverser::run(*m_context, *m_ast);
CommonSubexpressionEliminator::run(*m_context, *m_ast);
UnusedPruner::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "stackCompressor")
{
disambiguate();
FunctionGrouper::run(*m_context, *m_ast);
size_t maxIterations = 16;
Object obj;
obj.code = m_ast;
StackCompressor::run(*m_dialect, obj, true, maxIterations);
m_ast = obj.code;
BlockFlattener::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "wordSizeTransform")
{
disambiguate();
ExpressionSplitter::run(*m_context, *m_ast);
WordSizeTransform::run(*m_dialect, *m_dialect, *m_ast, *m_nameDispenser);
}
else if (m_optimizerStep == "fullSuite")
{
GasMeter meter(dynamic_cast(*m_dialect), false, 200);
yul::Object obj;
obj.code = m_ast;
obj.analysisInfo = m_analysisInfo;
OptimiserSuite::run(*m_dialect, &meter, obj, true, solidity::frontend::OptimiserSettings::DefaultYulOptimiserSteps);
}
else
{
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Invalid optimizer step: " << m_optimizerStep << endl;
return TestResult::FatalError;
}
m_obtainedResult = "step: " + m_optimizerStep + "\n\n" + AsmPrinter{ *m_dialect }(*m_ast) + "\n";
return checkResult(_stream, _linePrefix, _formatted);
}
bool YulOptimizerTest::parse(ostream& _stream, string const& _linePrefix, bool const _formatted)
{
ErrorList errors;
soltestAssert(m_dialect, "");
std::tie(m_ast, m_analysisInfo) = yul::test::parse(m_source, *m_dialect, errors);
if (!m_ast || !m_analysisInfo || !Error::containsOnlyWarnings(errors))
{
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Error parsing source." << endl;
printErrors(_stream, errors);
return false;
}
return true;
}
void YulOptimizerTest::disambiguate()
{
*m_ast = std::get(Disambiguator(*m_dialect, *m_analysisInfo)(*m_ast));
m_analysisInfo.reset();
updateContext();
}
void YulOptimizerTest::updateContext()
{
m_nameDispenser = make_unique(*m_dialect, *m_ast, m_reservedIdentifiers);
m_context = make_unique(OptimiserStepContext{
*m_dialect,
*m_nameDispenser,
m_reservedIdentifiers
});
}
void YulOptimizerTest::printErrors(ostream& _stream, ErrorList const& _errors)
{
SourceReferenceFormatter formatter(_stream);
for (auto const& error: _errors)
formatter.printErrorInformation(*error);
}