solidity/test/libyul/YulOptimizerTest.cpp

120 lines
4.2 KiB
C++
Raw Normal View History

2018-10-09 13:43:55 +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/>.
*/
// SPDX-License-Identifier: GPL-3.0
2018-10-09 13:43:55 +00:00
2018-10-15 09:52:35 +00:00
#include <test/libyul/YulOptimizerTest.h>
#include <test/libyul/YulOptimizerTestCommon.h>
2018-10-09 13:43:55 +00:00
#include <test/libsolidity/util/SoltestErrors.h>
2019-12-19 16:58:20 +00:00
#include <test/libyul/Common.h>
#include <test/Common.h>
2018-10-09 13:43:55 +00:00
2020-12-29 12:19:23 +00:00
#include <libyul/Object.h>
#include <libyul/AsmPrinter.h>
2020-12-29 12:19:23 +00:00
#include <liblangutil/CharStreamProvider.h>
2020-12-01 13:22:15 +00:00
#include <liblangutil/SourceReferenceFormatter.h>
2021-06-29 12:38:59 +00:00
#include <liblangutil/Scanner.h>
2018-10-09 13:43:55 +00:00
#include <libsolutil/AnsiColorized.h>
2018-10-09 13:43:55 +00:00
#include <fstream>
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;
2018-10-09 13:43:55 +00:00
using namespace std;
2020-03-06 00:22:51 +00:00
YulOptimizerTest::YulOptimizerTest(string const& _filename):
EVMVersionRestrictedTestCase(_filename)
2018-10-09 13:43:55 +00:00
{
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();
2020-03-06 00:22:51 +00:00
m_source = m_reader.source();
2018-10-09 13:43:55 +00:00
2020-03-06 00:22:51 +00:00
auto dialectName = m_reader.stringSetting("dialect", "evm");
m_dialect = &dialect(dialectName, solidity::test::CommonOptions::get().evmVersion());
2020-03-06 00:22:51 +00:00
m_expectation = m_reader.simpleExpectations();
2018-10-09 13:43:55 +00:00
}
TestCase::TestResult YulOptimizerTest::run(ostream& _stream, string const& _linePrefix, bool const _formatted)
2018-10-09 13:43:55 +00:00
{
std::tie(m_object, m_analysisInfo) = parse(_stream, _linePrefix, _formatted, m_source);
if (!m_object)
return TestResult::FatalError;
2018-10-09 13:43:55 +00:00
soltestAssert(m_dialect, "Dialect not set.");
2019-09-23 14:32:50 +00:00
m_object->analysisInfo = m_analysisInfo;
YulOptimizerTestCommon tester(m_object, *m_dialect);
tester.setStep(m_optimizerStep);
2019-05-21 13:52:15 +00:00
if (!tester.runStep())
2018-10-09 13:43:55 +00:00
{
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Invalid optimizer step: " << m_optimizerStep << endl;
return TestResult::FatalError;
2018-10-09 13:43:55 +00:00
}
auto const printed = (m_object->subObjects.empty() ? AsmPrinter{ *m_dialect }(*m_object->code) : m_object->toString(m_dialect));
// Re-parse new code for compilability
// TODO: support for wordSizeTransform which needs different input and output dialects
if (m_optimizerStep != "wordSizeTransform" && !std::get<0>(parse(_stream, _linePrefix, _formatted, printed)))
{
util::AnsiColorized(_stream, _formatted, {util::formatting::BOLD, util::formatting::CYAN})
<< _linePrefix << "Result after the optimiser:" << endl;
printIndented(_stream, printed, _linePrefix + " ");
return TestResult::FatalError;
}
m_obtainedResult = "step: " + m_optimizerStep + "\n\n" + printed + "\n";
2018-10-09 13:43:55 +00:00
return checkResult(_stream, _linePrefix, _formatted);
2018-10-09 13:43:55 +00:00
}
std::pair<std::shared_ptr<Object>, std::shared_ptr<AsmAnalysisInfo>> YulOptimizerTest::parse(
ostream& _stream,
string const& _linePrefix,
bool const _formatted,
string const& _source
)
2018-10-09 13:43:55 +00:00
{
2019-12-19 16:58:20 +00:00
ErrorList errors;
soltestAssert(m_dialect, "");
shared_ptr<Object> object;
shared_ptr<AsmAnalysisInfo> analysisInfo;
std::tie(object, analysisInfo) = yul::test::parse(_source, *m_dialect, errors);
2021-06-30 12:48:45 +00:00
if (!object || !analysisInfo || Error::containsErrors(errors))
2018-10-09 13:43:55 +00:00
{
2019-02-13 11:21:53 +00:00
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Error parsing source." << endl;
2021-06-29 12:38:59 +00:00
CharStream charStream(_source, "");
SourceReferenceFormatter{_stream, SingletonCharStreamProvider(charStream), true, false}
.printErrorInformation(errors);
return {};
2018-10-09 13:43:55 +00:00
}
return {std::move(object), std::move(analysisInfo)};
2018-10-09 13:43:55 +00:00
}