Unified dialect selection in libyul/SyntaxTest and YulOptimizerTest.

This commit is contained in:
a3d4 2020-03-09 00:40:54 +01:00
parent bb38ce1759
commit a5ae51fa6e
5 changed files with 62 additions and 63 deletions

View File

@ -31,6 +31,7 @@
#include <libyul/AsmPrinter.h>
#include <libyul/AssemblyStack.h>
#include <libyul/backends/evm/EVMDialect.h>
#include <libyul/backends/wasm/WasmDialect.h>
#include <liblangutil/Scanner.h>
#include <liblangutil/ErrorReporter.h>
@ -106,3 +107,51 @@ string yul::test::format(string const& _source, bool _yul)
{
return yul::AsmPrinter()(*parse(_source, _yul).first);
}
namespace
{
std::map<string const, yul::Dialect const& (*)(langutil::EVMVersion)> const validDialects = {
{
"evm",
[](langutil::EVMVersion _evmVersion) -> yul::Dialect const&
{ return yul::EVMDialect::strictAssemblyForEVMObjects(_evmVersion); }
},
{
"evmTyped",
[](langutil::EVMVersion _evmVersion) -> yul::Dialect const&
{ return yul::EVMDialectTyped::instance(_evmVersion); }
},
{
"yul",
[](langutil::EVMVersion) -> yul::Dialect const&
{ return yul::Dialect::yulDeprecated(); }
},
{
"ewasm",
[](langutil::EVMVersion) -> yul::Dialect const&
{ return yul::WasmDialect::instance(); }
}
};
vector<string> validDialectNames()
{
vector<string> names{size(validDialects), ""};
transform(begin(validDialects), end(validDialects), names.begin(), [](auto const& dialect) { return dialect.first; });
return names;
}
}
yul::Dialect const& yul::test::dialect(std::string const& _name, langutil::EVMVersion _evmVersion)
{
if (!validDialects.count(_name))
BOOST_THROW_EXCEPTION(runtime_error{
"Invalid Dialect \"" +
_name +
"\". Valid dialects are " +
util::joinHumanReadable(validDialectNames(), ", ", " and ") +
"."
});
return validDialects.at(_name)(_evmVersion);
}

View File

@ -23,6 +23,8 @@
#include <libyul/AsmData.h>
#include <liblangutil/EVMVersion.h>
#include <string>
#include <vector>
#include <memory>
@ -53,4 +55,6 @@ parse(std::string const& _source, Dialect const& _dialect, langutil::ErrorList&
Block disambiguate(std::string const& _source, bool _yul = true);
std::string format(std::string const& _source, bool _yul = true);
solidity::yul::Dialect const& dialect(std::string const& _name, langutil::EVMVersion _evmVersion);
}

View File

@ -19,59 +19,22 @@
#include <libyul/AsmAnalysisInfo.h>
#include <libyul/AsmParser.h>
#include <libyul/backends/wasm/WasmDialect.h>
#include <libyul/backends/evm/EVMDialect.h>
#include <libyul/backends/wasm/WasmDialect.h>
#include <liblangutil/EVMVersion.h>
#include <liblangutil/Exceptions.h>
#include <test/libyul/Common.h>
#include <test/libyul/SyntaxTest.h>
#include <test/Common.h>
using namespace std;
using namespace solidity;
using namespace solidity::util;
using namespace solidity::langutil;
using namespace solidity::yul::test;
namespace
{
std::map<string const, yul::Dialect const& (*)(langutil::EVMVersion)> const validDialects = {
{
"evm",
[](langutil::EVMVersion _evmVersion) -> yul::Dialect const&
{ return yul::EVMDialect::strictAssemblyForEVM(_evmVersion); }
},
{
"evmTyped",
[](langutil::EVMVersion _evmVersion) -> yul::Dialect const&
{ return yul::EVMDialectTyped::instance(_evmVersion); }
},
{
"yul",
[](langutil::EVMVersion) -> yul::Dialect const&
{ return yul::Dialect::yulDeprecated(); }
},
{
"ewasm",
[](langutil::EVMVersion) -> yul::Dialect const&
{ return yul::WasmDialect::instance(); }
}
};
vector<string> validDialectNames()
{
vector<string> names{size(validDialects), ""};
transform(begin(validDialects), end(validDialects), names.begin(), [](auto const& dialect) { return dialect.first; });
return names;
}
}
void SyntaxTest::parseAndAnalyze()
{
yul::Dialect const& dialect = validDialects.at(m_dialectName)(m_evmVersion);
if (m_sources.size() != 1)
BOOST_THROW_EXCEPTION(runtime_error{"Expected only one source for yul test."});
@ -82,12 +45,12 @@ void SyntaxTest::parseAndAnalyze()
ErrorReporter errorReporter{errorList};
auto scanner = make_shared<Scanner>(CharStream(source, name));
auto parserResult = yul::Parser(errorReporter, dialect).parse(scanner, false);
auto parserResult = yul::Parser(errorReporter, *m_dialect).parse(scanner, false);
if (parserResult)
{
yul::AsmAnalysisInfo analysisInfo;
yul::AsmAnalyzer(analysisInfo, errorReporter, dialect).analyze(*parserResult);
yul::AsmAnalyzer(analysisInfo, errorReporter, *m_dialect).analyze(*parserResult);
}
for (auto const& error: errorList)
@ -115,14 +78,6 @@ void SyntaxTest::parseAndAnalyze()
SyntaxTest::SyntaxTest(string const& _filename, langutil::EVMVersion _evmVersion):
CommonSyntaxTest(_filename, _evmVersion)
{
m_dialectName = m_reader.stringSetting("dialect", "evmTyped");
if (!validDialects.count(m_dialectName))
BOOST_THROW_EXCEPTION(runtime_error{
"Invalid Dialect \"" +
m_dialectName +
"\". Valid dialects are " +
joinHumanReadable(validDialectNames(), ", ", " and ") +
"."
});
string dialectName = m_reader.stringSetting("dialect", "evmTyped");
m_dialect = &dialect(dialectName, solidity::test::CommonOptions::get().evmVersion());
}

View File

@ -42,7 +42,7 @@ protected:
void parseAndAnalyze() override;
private:
std::string m_dialectName;
Dialect const* m_dialect = nullptr;
};
}

View File

@ -101,16 +101,7 @@ YulOptimizerTest::YulOptimizerTest(string const& _filename):
m_source = m_reader.source();
auto dialectName = m_reader.stringSetting("dialect", "evm");
if (dialectName == "yul")
m_dialect = &Dialect::yulDeprecated();
else if (dialectName == "ewasm")
m_dialect = &WasmDialect::instance();
else if (dialectName == "evm")
m_dialect = &EVMDialect::strictAssemblyForEVMObjects(solidity::test::CommonOptions::get().evmVersion());
else if (dialectName == "evmTyped")
m_dialect = &EVMDialectTyped::instance(solidity::test::CommonOptions::get().evmVersion());
else
BOOST_THROW_EXCEPTION(runtime_error("Invalid dialect " + dialectName));
m_dialect = &dialect(dialectName, solidity::test::CommonOptions::get().evmVersion());
m_step = m_reader.stringSetting("step", "");