forked from argotorg/solidity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYulOptimizerTest.cpp
More file actions
126 lines (101 loc) · 4.41 KB
/
Copy pathYulOptimizerTest.cpp
File metadata and controls
126 lines (101 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
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
#include <test/libyul/YulOptimizerTest.h>
#include <test/libyul/YulOptimizerTestCommon.h>
#include <test/libsolidity/util/SoltestErrors.h>
#include <test/libyul/Common.h>
#include <test/Common.h>
#include <libyul/Object.h>
#include <libyul/optimiser/ReasoningBasedSimplifier.h>
#include <libyul/AsmPrinter.h>
#include <liblangutil/CharStreamProvider.h>
#include <liblangutil/SourceReferenceFormatter.h>
#include <liblangutil/Scanner.h>
#include <libsolutil/AnsiColorized.h>
#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;
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();
if (m_optimizerStep == "reasoningBasedSimplifier" && (
solidity::test::CommonOptions::get().disableSMT ||
ReasoningBasedSimplifier::invalidInCurrentEnvironment()
))
m_shouldRun = false;
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)
{
std::tie(m_object, m_analysisInfo) = parse(_stream, _linePrefix, _formatted, m_source);
if (!m_object)
return TestResult::FatalError;
soltestAssert(m_dialect, "Dialect not set.");
m_object->analysisInfo = m_analysisInfo;
YulOptimizerTestCommon tester(m_object, *m_dialect);
tester.setStep(m_optimizerStep);
if (!tester.runStep())
{
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Invalid optimizer step: " << m_optimizerStep << endl;
return TestResult::FatalError;
}
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";
return checkResult(_stream, _linePrefix, _formatted);
}
std::pair<std::shared_ptr<Object>, std::shared_ptr<AsmAnalysisInfo>> YulOptimizerTest::parse(
ostream& _stream,
string const& _linePrefix,
bool const _formatted,
string const& _source
)
{
ErrorList errors;
soltestAssert(m_dialect, "");
shared_ptr<Object> object;
shared_ptr<AsmAnalysisInfo> analysisInfo;
std::tie(object, analysisInfo) = yul::test::parse(_source, *m_dialect, errors);
if (!object || !analysisInfo || Error::containsErrors(errors))
{
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Error parsing source." << endl;
CharStream charStream(_source, "");
SourceReferenceFormatter{_stream, SingletonCharStreamProvider(charStream), true, false}
.printErrorInformation(errors);
return {};
}
return {std::move(object), std::move(analysisInfo)};
}