/*
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 .
*/
// SPDX-License-Identifier: GPL-3.0
/**
* Interactive yul optimizer
*/
#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 std;
using namespace solidity;
using namespace solidity::util;
using namespace solidity::langutil;
using namespace solidity::frontend;
using namespace solidity::yul;
namespace po = boost::program_options;
class YulOpti
{
public:
static void printErrors(CharStream const& _charStream, ErrorList const& _errors)
{
SourceReferenceFormatter{
cerr,
SingletonCharStreamProvider(_charStream),
true,
false
}.printErrorInformation(_errors);
}
void parse(string const& _input)
{
ErrorList errors;
ErrorReporter errorReporter(errors);
CharStream charStream(_input, "");
try
{
ObjectParser parser(errorReporter, m_dialect);
auto scanner = make_shared(charStream);
m_inputIsCodeBlock = (scanner->currentToken() == Token::LBrace);
m_object = parser.parse(scanner, false);
if (!m_object || !errorReporter.errors().empty())
{
cerr << "Error parsing source." << endl;
printErrors(charStream, errors);
solThrow(Exception, "Could not parse source.");
}
runCodeAnalyzer(errorReporter);
}
catch(...)
{
cerr << "Fatal error during parsing: " << endl;
printErrors(charStream, errors);
throw;
}
}
void printUsageBanner(
map const& _extraOptions,
size_t _columns
)
{
yulAssert(_columns > 0);
auto const& optimiserSteps = OptimiserSuite::stepAbbreviationToNameMap();
auto hasShorterString = [](auto const& a, auto const& b) { return a.second.size() < b.second.size(); };
size_t longestDescriptionLength = max(
max_element(optimiserSteps.begin(), optimiserSteps.end(), hasShorterString)->second.size(),
max_element(_extraOptions.begin(), _extraOptions.end(), hasShorterString)->second.size()
);
vector overlappingAbbreviations =
ranges::views::set_intersection(_extraOptions | ranges::views::keys, optimiserSteps | ranges::views::keys) |
ranges::views::transform([](char _abbreviation){ return string(1, _abbreviation); }) |
ranges::to();
yulAssert(
overlappingAbbreviations.empty(),
"ERROR: Conflict between yulopti controls and the following Yul optimizer step abbreviations: " +
boost::join(overlappingAbbreviations, ", ") + ".\n"
"This is most likely caused by someone adding a new step abbreviation to "
"OptimiserSuite::stepNameToAbbreviationMap() and not realizing that it's used by yulopti.\n"
"Please update the code to use a different character and recompile yulopti."
);
vector> sortedOptions =
ranges::views::concat(optimiserSteps, _extraOptions) |
ranges::to>>() |
ranges::actions::sort([](tuple const& _a, tuple const& _b) {
return (
!boost::algorithm::iequals(get<1>(_a), get<1>(_b)) ?
boost::algorithm::lexicographical_compare(get<1>(_a), get<1>(_b), boost::algorithm::is_iless()) :
toLower(get<0>(_a)) < toLower(get<0>(_b))
);
});
yulAssert(sortedOptions.size() > 0);
size_t rows = (sortedOptions.size() - 1) / _columns + 1;
for (size_t row = 0; row < rows; ++row)
{
for (auto const& [key, name]: sortedOptions | ranges::views::drop(row) | ranges::views::stride(rows))
cout << key << ": " << setw(static_cast(longestDescriptionLength)) << setiosflags(ios::left) << name << " ";
cout << endl;
}
}
void applyFunctionToObjectAndSubobjects(Object& _object, function _function)
{
for (auto const& subObjectNode: _object.subObjects)
{
auto subObject = dynamic_pointer_cast