From e77ae3f06e6d12836a8fb1420bc2d57fc64c268c Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Wed, 5 Aug 2020 12:33:45 +0100 Subject: [PATCH 1/2] Parse optimiser output in yulOptimiserTests --- test/libyul/YulOptimizerTest.cpp | 37 +++++++++++++++++++++++--------- test/libyul/YulOptimizerTest.h | 4 +++- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/test/libyul/YulOptimizerTest.cpp b/test/libyul/YulOptimizerTest.cpp index a09725b1c..530f5efec 100644 --- a/test/libyul/YulOptimizerTest.cpp +++ b/test/libyul/YulOptimizerTest.cpp @@ -111,7 +111,8 @@ YulOptimizerTest::YulOptimizerTest(string const& _filename): TestCase::TestResult YulOptimizerTest::run(ostream& _stream, string const& _linePrefix, bool const _formatted) { - if (!parse(_stream, _linePrefix, _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."); @@ -354,26 +355,42 @@ TestCase::TestResult YulOptimizerTest::run(ostream& _stream, string const& _line return TestResult::FatalError; } - m_obtainedResult = - "step: " + m_optimizerStep + "\n\n" + - (m_object->subObjects.empty() ? AsmPrinter{ *m_dialect }(*m_object->code) : m_object->toString(m_dialect)) + - "\n"; + 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); } -bool YulOptimizerTest::parse(ostream& _stream, string const& _linePrefix, bool const _formatted) +std::pair, std::shared_ptr> YulOptimizerTest::parse( + ostream& _stream, + string const& _linePrefix, + bool const _formatted, + string const& _source +) { ErrorList errors; soltestAssert(m_dialect, ""); - std::tie(m_object, m_analysisInfo) = yul::test::parse(m_source, *m_dialect, errors); - if (!m_object || !m_analysisInfo || !Error::containsOnlyWarnings(errors)) + shared_ptr object; + shared_ptr analysisInfo; + std::tie(object, analysisInfo) = yul::test::parse(_source, *m_dialect, errors); + if (!object || !analysisInfo || !Error::containsOnlyWarnings(errors)) { AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Error parsing source." << endl; printErrors(_stream, errors); - return false; + return {}; } - return true; + return {std::move(object), std::move(analysisInfo)}; } void YulOptimizerTest::disambiguate() diff --git a/test/libyul/YulOptimizerTest.h b/test/libyul/YulOptimizerTest.h index 2cebf3057..6baeca728 100644 --- a/test/libyul/YulOptimizerTest.h +++ b/test/libyul/YulOptimizerTest.h @@ -58,7 +58,9 @@ public: TestResult run(std::ostream& _stream, std::string const& _linePrefix = "", bool const _formatted = false) override; private: - bool parse(std::ostream& _stream, std::string const& _linePrefix, bool const _formatted); + std::pair, std::shared_ptr> parse( + std::ostream& _stream, std::string const& _linePrefix, bool const _formatted, std::string const& _source + ); void disambiguate(); void updateContext(); From 2e8616e2e4e943bf2645c280271effc7b30b5034 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Wed, 5 Aug 2020 22:12:51 +0100 Subject: [PATCH 2/2] Run prequisite steps for VarNameCleaner in tests --- test/libyul/YulOptimizerTest.cpp | 4 ++++ .../yulOptimizerTests/varNameCleaner/builtins.yul | 2 +- .../varNameCleaner/function_names.yul | 8 +++++--- .../varNameCleaner/function_parameters.yul | 6 ++++-- .../varNameCleaner/function_scopes.yul | 1 + .../yulOptimizerTests/varNameCleaner/instructions.yul | 2 +- .../varNameCleaner/name_stripping.yul | 10 ++++++---- .../varNameCleaner/reshuffling-inverse.yul | 10 ++++++---- .../yulOptimizerTests/varNameCleaner/reshuffling.yul | 8 +++++--- 9 files changed, 33 insertions(+), 18 deletions(-) diff --git a/test/libyul/YulOptimizerTest.cpp b/test/libyul/YulOptimizerTest.cpp index 530f5efec..13c9ed5c7 100644 --- a/test/libyul/YulOptimizerTest.cpp +++ b/test/libyul/YulOptimizerTest.cpp @@ -142,7 +142,11 @@ TestCase::TestResult YulOptimizerTest::run(ostream& _stream, string const& _line else if (m_optimizerStep == "varDeclInitializer") VarDeclInitializer::run(*m_context, *m_object->code); else if (m_optimizerStep == "varNameCleaner") + { + disambiguate(); + FunctionGrouper::run(*m_context, *m_object->code); VarNameCleaner::run(*m_context, *m_object->code); + } else if (m_optimizerStep == "forLoopConditionIntoBody") { disambiguate(); diff --git a/test/libyul/yulOptimizerTests/varNameCleaner/builtins.yul b/test/libyul/yulOptimizerTests/varNameCleaner/builtins.yul index b6ee0377f..43c30137a 100644 --- a/test/libyul/yulOptimizerTests/varNameCleaner/builtins.yul +++ b/test/libyul/yulOptimizerTests/varNameCleaner/builtins.yul @@ -4,4 +4,4 @@ // ---- // step: varNameCleaner // -// { let datasize_1 := 1 } +// { { let datasize_1 := 1 } } diff --git a/test/libyul/yulOptimizerTests/varNameCleaner/function_names.yul b/test/libyul/yulOptimizerTests/varNameCleaner/function_names.yul index ea4ca9ea2..c8b2bef24 100644 --- a/test/libyul/yulOptimizerTests/varNameCleaner/function_names.yul +++ b/test/libyul/yulOptimizerTests/varNameCleaner/function_names.yul @@ -1,14 +1,16 @@ { - let f_2 function f() { let f_1 } + let f_2 let f_10 } // ---- // step: varNameCleaner // // { -// let f_1 +// { +// let f_1 +// let f_2 +// } // function f() // { let f_1 } -// let f_2 // } diff --git a/test/libyul/yulOptimizerTests/varNameCleaner/function_parameters.yul b/test/libyul/yulOptimizerTests/varNameCleaner/function_parameters.yul index 1608dd6fb..c0656056e 100644 --- a/test/libyul/yulOptimizerTests/varNameCleaner/function_parameters.yul +++ b/test/libyul/yulOptimizerTests/varNameCleaner/function_parameters.yul @@ -12,12 +12,14 @@ // step: varNameCleaner // // { -// let f_1 +// { +// let f_1 +// let f_2 +// } // function f(x) -> x_1, y // { // let y_1 := x // y := y_1 // x_1 := y // } -// let f_2 // } diff --git a/test/libyul/yulOptimizerTests/varNameCleaner/function_scopes.yul b/test/libyul/yulOptimizerTests/varNameCleaner/function_scopes.yul index c7d86ef98..2882459db 100644 --- a/test/libyul/yulOptimizerTests/varNameCleaner/function_scopes.yul +++ b/test/libyul/yulOptimizerTests/varNameCleaner/function_scopes.yul @@ -6,6 +6,7 @@ // step: varNameCleaner // // { +// { } // function f() // { let x := 0 } // function g() diff --git a/test/libyul/yulOptimizerTests/varNameCleaner/instructions.yul b/test/libyul/yulOptimizerTests/varNameCleaner/instructions.yul index 2d7dbeb4a..325ec7d84 100644 --- a/test/libyul/yulOptimizerTests/varNameCleaner/instructions.yul +++ b/test/libyul/yulOptimizerTests/varNameCleaner/instructions.yul @@ -4,4 +4,4 @@ // ---- // step: varNameCleaner // -// { let mul_1 := 1 } +// { { let mul_1 := 1 } } diff --git a/test/libyul/yulOptimizerTests/varNameCleaner/name_stripping.yul b/test/libyul/yulOptimizerTests/varNameCleaner/name_stripping.yul index cd32d8530..7d7d763c5 100644 --- a/test/libyul/yulOptimizerTests/varNameCleaner/name_stripping.yul +++ b/test/libyul/yulOptimizerTests/varNameCleaner/name_stripping.yul @@ -8,8 +8,10 @@ // step: varNameCleaner // // { -// let a := 1 -// let a_1 := 2 -// let a_2 := 0xdeadbeef -// let _1 := 21718 +// { +// let a := 1 +// let a_1 := 2 +// let a_2 := 0xdeadbeef +// let _1 := 21718 +// } // } diff --git a/test/libyul/yulOptimizerTests/varNameCleaner/reshuffling-inverse.yul b/test/libyul/yulOptimizerTests/varNameCleaner/reshuffling-inverse.yul index 100e5ccc6..237ab8612 100644 --- a/test/libyul/yulOptimizerTests/varNameCleaner/reshuffling-inverse.yul +++ b/test/libyul/yulOptimizerTests/varNameCleaner/reshuffling-inverse.yul @@ -8,8 +8,10 @@ // step: varNameCleaner // // { -// let x := 1 -// let x_1 := 2 -// let x_2 := 3 -// let x_3 := 4 +// { +// let x := 1 +// let x_1 := 2 +// let x_2 := 3 +// let x_3 := 4 +// } // } diff --git a/test/libyul/yulOptimizerTests/varNameCleaner/reshuffling.yul b/test/libyul/yulOptimizerTests/varNameCleaner/reshuffling.yul index d197d4ff3..0df1715d7 100644 --- a/test/libyul/yulOptimizerTests/varNameCleaner/reshuffling.yul +++ b/test/libyul/yulOptimizerTests/varNameCleaner/reshuffling.yul @@ -7,7 +7,9 @@ // step: varNameCleaner // // { -// let x := 1 -// let x_1 := 2 -// let x_2 := 3 +// { +// let x := 1 +// let x_1 := 2 +// let x_2 := 3 +// } // }