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/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// 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>
|
2020-12-28 15:39:14 +00:00
|
|
|
#include <test/libyul/YulOptimizerTestCommon.h>
|
2018-10-09 13:43:55 +00:00
|
|
|
|
2019-07-21 13:46:03 +00:00
|
|
|
#include <test/libsolidity/util/SoltestErrors.h>
|
2019-12-19 16:58:20 +00:00
|
|
|
#include <test/libyul/Common.h>
|
2020-01-14 16:48:17 +00:00
|
|
|
#include <test/Common.h>
|
2018-10-09 13:43:55 +00:00
|
|
|
|
2020-12-29 12:19:23 +00:00
|
|
|
#include <libyul/Object.h>
|
2018-11-23 10:18:57 +00:00
|
|
|
#include <libyul/AsmPrinter.h>
|
2020-12-29 12:19:23 +00:00
|
|
|
|
2021-08-03 13:20:00 +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
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/AnsiColorized.h>
|
2019-02-11 15:00:24 +00:00
|
|
|
|
2018-10-09 13:43:55 +00:00
|
|
|
#include <fstream>
|
|
|
|
|
2019-12-23 15:50:30 +00:00
|
|
|
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");
|
2020-03-08 23:40:54 +00:00
|
|
|
m_dialect = &dialect(dialectName, solidity::test::CommonOptions::get().evmVersion());
|
2019-12-18 20:54:30 +00:00
|
|
|
|
2020-03-06 00:22:51 +00:00
|
|
|
m_expectation = m_reader.simpleExpectations();
|
2018-10-09 13:43:55 +00:00
|
|
|
}
|
|
|
|
|
2019-05-07 13:33:22 +00:00
|
|
|
TestCase::TestResult YulOptimizerTest::run(ostream& _stream, string const& _linePrefix, bool const _formatted)
|
2018-10-09 13:43:55 +00:00
|
|
|
{
|
2020-08-05 11:33:45 +00:00
|
|
|
std::tie(m_object, m_analysisInfo) = parse(_stream, _linePrefix, _formatted, m_source);
|
|
|
|
if (!m_object)
|
2019-05-07 13:33:22 +00:00
|
|
|
return TestResult::FatalError;
|
2018-10-09 13:43:55 +00:00
|
|
|
|
2019-05-16 08:56:56 +00:00
|
|
|
soltestAssert(m_dialect, "Dialect not set.");
|
2019-09-23 14:32:50 +00:00
|
|
|
|
2020-12-28 15:39:14 +00:00
|
|
|
m_object->analysisInfo = m_analysisInfo;
|
2020-10-07 12:04:00 +00:00
|
|
|
YulOptimizerTestCommon tester(m_object, *m_dialect);
|
|
|
|
tester.setStep(m_optimizerStep);
|
2019-05-21 13:52:15 +00:00
|
|
|
|
2021-01-06 12:04:42 +00:00
|
|
|
if (!tester.runStep())
|
2018-10-09 13:43:55 +00:00
|
|
|
{
|
2019-02-11 15:00:24 +00:00
|
|
|
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Invalid optimizer step: " << m_optimizerStep << endl;
|
2019-05-07 13:33:22 +00:00
|
|
|
return TestResult::FatalError;
|
2018-10-09 13:43:55 +00:00
|
|
|
}
|
|
|
|
|
2020-08-05 11:33:45 +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
|
|
|
|
2020-05-25 13:31:24 +00:00
|
|
|
return checkResult(_stream, _linePrefix, _formatted);
|
2018-10-09 13:43:55 +00:00
|
|
|
}
|
|
|
|
|
2020-08-05 11:33:45 +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, "");
|
2020-08-05 11:33:45 +00:00
|
|
|
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);
|
2020-08-05 11:33:45 +00:00
|
|
|
return {};
|
2018-10-09 13:43:55 +00:00
|
|
|
}
|
2020-08-05 11:33:45 +00:00
|
|
|
return {std::move(object), std::move(analysisInfo)};
|
2018-10-09 13:43:55 +00:00
|
|
|
}
|