2020-01-17 06:34:18 +00:00
|
|
|
/*
|
|
|
|
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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <tools/yulPhaser/Program.h>
|
|
|
|
|
|
|
|
#include <liblangutil/CharStream.h>
|
|
|
|
#include <liblangutil/ErrorReporter.h>
|
2020-02-28 02:25:00 +00:00
|
|
|
#include <liblangutil/SourceReferenceFormatter.h>
|
2020-01-17 06:34:18 +00:00
|
|
|
|
|
|
|
#include <libyul/AsmAnalysis.h>
|
|
|
|
#include <libyul/AsmAnalysisInfo.h>
|
2020-01-23 09:27:50 +00:00
|
|
|
#include <libyul/AsmJsonConverter.h>
|
2020-01-17 06:34:18 +00:00
|
|
|
#include <libyul/AsmParser.h>
|
2020-01-23 09:27:50 +00:00
|
|
|
#include <libyul/AsmPrinter.h>
|
2020-01-17 06:34:18 +00:00
|
|
|
#include <libyul/YulString.h>
|
|
|
|
#include <libyul/backends/evm/EVMDialect.h>
|
|
|
|
#include <libyul/optimiser/Disambiguator.h>
|
|
|
|
#include <libyul/optimiser/ForLoopInitRewriter.h>
|
|
|
|
#include <libyul/optimiser/FunctionGrouper.h>
|
|
|
|
#include <libyul/optimiser/FunctionHoister.h>
|
|
|
|
#include <libyul/optimiser/Metrics.h>
|
2020-01-24 22:22:01 +00:00
|
|
|
#include <libyul/optimiser/OptimiserStep.h>
|
2020-01-17 06:34:18 +00:00
|
|
|
#include <libyul/optimiser/Suite.h>
|
|
|
|
|
2020-01-23 09:27:50 +00:00
|
|
|
#include <libsolutil/JSON.h>
|
2020-01-17 06:34:18 +00:00
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::langutil;
|
|
|
|
using namespace solidity::yul;
|
|
|
|
using namespace solidity::util;
|
|
|
|
using namespace solidity::phaser;
|
|
|
|
|
2020-01-23 09:27:50 +00:00
|
|
|
namespace solidity::phaser
|
|
|
|
{
|
|
|
|
|
|
|
|
ostream& operator<<(ostream& _stream, Program const& _program);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-02-28 02:25:00 +00:00
|
|
|
ostream& std::operator<<(ostream& _outputStream, ErrorList const& _errors)
|
|
|
|
{
|
|
|
|
SourceReferenceFormatter formatter(_outputStream);
|
|
|
|
|
|
|
|
for (auto const& error: _errors)
|
|
|
|
formatter.printErrorInformation(*error);
|
|
|
|
|
|
|
|
return _outputStream;
|
|
|
|
}
|
|
|
|
|
2020-01-31 12:23:57 +00:00
|
|
|
Program::Program(Program const& program):
|
|
|
|
m_ast(make_unique<Block>(get<Block>(ASTCopier{}(*program.m_ast)))),
|
|
|
|
m_dialect{program.m_dialect},
|
|
|
|
m_nameDispenser(program.m_nameDispenser)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-28 02:33:46 +00:00
|
|
|
variant<Program, ErrorList> Program::load(CharStream& _sourceCode)
|
2020-01-17 06:34:18 +00:00
|
|
|
{
|
2020-01-22 19:14:36 +00:00
|
|
|
// ASSUMPTION: parseSource() rewinds the stream on its own
|
2020-01-17 06:34:18 +00:00
|
|
|
Dialect const& dialect = EVMDialect::strictAssemblyForEVMObjects(EVMVersion{});
|
2020-02-28 02:33:46 +00:00
|
|
|
|
|
|
|
variant<unique_ptr<Block>, ErrorList> astOrErrors = parseSource(dialect, _sourceCode);
|
|
|
|
if (holds_alternative<ErrorList>(astOrErrors))
|
|
|
|
return get<ErrorList>(astOrErrors);
|
|
|
|
|
|
|
|
variant<unique_ptr<AsmAnalysisInfo>, ErrorList> analysisInfoOrErrors = analyzeAST(
|
|
|
|
dialect,
|
|
|
|
*get<unique_ptr<Block>>(astOrErrors)
|
|
|
|
);
|
|
|
|
if (holds_alternative<ErrorList>(analysisInfoOrErrors))
|
|
|
|
return get<ErrorList>(analysisInfoOrErrors);
|
2020-01-17 06:34:18 +00:00
|
|
|
|
|
|
|
Program program(
|
|
|
|
dialect,
|
2020-02-28 02:33:46 +00:00
|
|
|
disambiguateAST(
|
|
|
|
dialect,
|
|
|
|
*get<unique_ptr<Block>>(astOrErrors),
|
|
|
|
*get<unique_ptr<AsmAnalysisInfo>>(analysisInfoOrErrors)
|
|
|
|
)
|
2020-01-17 06:34:18 +00:00
|
|
|
);
|
|
|
|
program.optimise({
|
|
|
|
FunctionHoister::name,
|
|
|
|
FunctionGrouper::name,
|
|
|
|
ForLoopInitRewriter::name,
|
|
|
|
});
|
|
|
|
|
|
|
|
return program;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Program::optimise(vector<string> const& _optimisationSteps)
|
|
|
|
{
|
2020-01-31 10:32:44 +00:00
|
|
|
m_ast = applyOptimisationSteps(m_dialect, m_nameDispenser, move(m_ast), _optimisationSteps);
|
2020-01-17 06:34:18 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 09:27:50 +00:00
|
|
|
ostream& phaser::operator<<(ostream& _stream, Program const& _program)
|
|
|
|
{
|
|
|
|
return _stream << AsmPrinter()(*_program.m_ast);
|
|
|
|
}
|
|
|
|
|
|
|
|
string Program::toJson() const
|
|
|
|
{
|
|
|
|
Json::Value serializedAst = AsmJsonConverter(0)(*m_ast);
|
|
|
|
return jsonPrettyPrint(serializedAst);
|
|
|
|
}
|
|
|
|
|
2020-02-28 02:33:46 +00:00
|
|
|
variant<unique_ptr<Block>, ErrorList> Program::parseSource(Dialect const& _dialect, CharStream _source)
|
2020-01-17 06:34:18 +00:00
|
|
|
{
|
|
|
|
ErrorList errors;
|
|
|
|
ErrorReporter errorReporter(errors);
|
|
|
|
auto scanner = make_shared<Scanner>(move(_source));
|
|
|
|
Parser parser(errorReporter, _dialect);
|
|
|
|
|
2020-01-22 16:33:36 +00:00
|
|
|
unique_ptr<Block> ast = parser.parse(scanner, false);
|
2020-02-28 02:33:46 +00:00
|
|
|
if (ast == nullptr)
|
|
|
|
return errors;
|
2020-01-17 06:34:18 +00:00
|
|
|
|
2020-02-28 02:33:46 +00:00
|
|
|
assert(errorReporter.errors().empty());
|
2020-03-05 03:44:32 +00:00
|
|
|
return variant<unique_ptr<Block>, ErrorList>(move(ast));
|
2020-01-17 06:34:18 +00:00
|
|
|
}
|
|
|
|
|
2020-02-28 02:33:46 +00:00
|
|
|
variant<unique_ptr<AsmAnalysisInfo>, ErrorList> Program::analyzeAST(Dialect const& _dialect, Block const& _ast)
|
2020-01-17 06:34:18 +00:00
|
|
|
{
|
|
|
|
ErrorList errors;
|
|
|
|
ErrorReporter errorReporter(errors);
|
|
|
|
auto analysisInfo = make_unique<AsmAnalysisInfo>();
|
|
|
|
AsmAnalyzer analyzer(*analysisInfo, errorReporter, _dialect);
|
|
|
|
|
|
|
|
bool analysisSuccessful = analyzer.analyze(_ast);
|
2020-02-28 02:33:46 +00:00
|
|
|
if (!analysisSuccessful)
|
|
|
|
return errors;
|
2020-01-17 06:34:18 +00:00
|
|
|
|
2020-02-28 02:33:46 +00:00
|
|
|
assert(errorReporter.errors().empty());
|
2020-03-05 03:44:32 +00:00
|
|
|
return variant<unique_ptr<AsmAnalysisInfo>, ErrorList>(move(analysisInfo));
|
2020-01-17 06:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unique_ptr<Block> Program::disambiguateAST(
|
|
|
|
Dialect const& _dialect,
|
|
|
|
Block const& _ast,
|
|
|
|
AsmAnalysisInfo const& _analysisInfo
|
|
|
|
)
|
|
|
|
{
|
2020-01-24 22:22:01 +00:00
|
|
|
set<YulString> const externallyUsedIdentifiers = {};
|
|
|
|
Disambiguator disambiguator(_dialect, _analysisInfo, externallyUsedIdentifiers);
|
2020-01-17 06:34:18 +00:00
|
|
|
|
|
|
|
return make_unique<Block>(get<Block>(disambiguator(_ast)));
|
|
|
|
}
|
|
|
|
|
2020-01-31 10:32:44 +00:00
|
|
|
unique_ptr<Block> Program::applyOptimisationSteps(
|
2020-01-24 22:22:01 +00:00
|
|
|
Dialect const& _dialect,
|
|
|
|
NameDispenser& _nameDispenser,
|
2020-01-31 10:32:44 +00:00
|
|
|
unique_ptr<Block> _ast,
|
2020-01-17 06:34:18 +00:00
|
|
|
vector<string> const& _optimisationSteps
|
|
|
|
)
|
|
|
|
{
|
2020-01-24 22:22:01 +00:00
|
|
|
// An empty set of reserved identifiers. It could be a constructor parameter but I don't
|
|
|
|
// think it would be useful in this tool. Other tools (like yulopti) have it empty too.
|
|
|
|
set<YulString> const externallyUsedIdentifiers = {};
|
|
|
|
OptimiserStepContext context{_dialect, _nameDispenser, externallyUsedIdentifiers};
|
|
|
|
|
2020-01-17 06:34:18 +00:00
|
|
|
for (string const& step: _optimisationSteps)
|
2020-01-31 10:32:44 +00:00
|
|
|
OptimiserSuite::allSteps().at(step)->run(context, *_ast);
|
|
|
|
|
|
|
|
return _ast;
|
2020-01-17 06:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t Program::computeCodeSize(Block const& _ast)
|
|
|
|
{
|
|
|
|
return CodeSize::codeSizeIncludingFunctions(_ast);
|
|
|
|
}
|