solidity/libyul/backends/wasm/EVMToEwasmTranslator.cpp

160 lines
5.6 KiB
C++
Raw Normal View History

2019-05-09 15:56:25 +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
2019-05-09 15:56:25 +00:00
/**
* Translates Yul code from EVM dialect to Ewasm dialect.
2019-05-09 15:56:25 +00:00
*/
#include <libyul/backends/wasm/EVMToEwasmTranslator.h>
2019-05-09 15:56:25 +00:00
#include <libyul/backends/wasm/WordSizeTransform.h>
#include <libyul/backends/wasm/WasmDialect.h>
#include <libyul/optimiser/ExpressionSplitter.h>
#include <libyul/optimiser/FunctionGrouper.h>
#include <libyul/optimiser/MainFunction.h>
#include <libyul/optimiser/FunctionHoister.h>
#include <libyul/optimiser/Disambiguator.h>
#include <libyul/optimiser/NameDisplacer.h>
2019-09-23 14:32:50 +00:00
#include <libyul/optimiser/OptimiserStep.h>
2019-11-19 23:00:34 +00:00
#include <libyul/optimiser/ForLoopConditionIntoBody.h>
2019-05-09 15:56:25 +00:00
2021-04-27 14:53:04 +00:00
#include <libyul/AST.h>
2019-05-09 15:56:25 +00:00
#include <libyul/AsmParser.h>
#include <libyul/AsmAnalysis.h>
#include <libyul/AsmAnalysisInfo.h>
#include <libyul/Object.h>
#include <liblangutil/ErrorReporter.h>
#include <liblangutil/Scanner.h>
#include <liblangutil/SourceReferenceFormatter.h>
2021-06-29 12:38:59 +00:00
#include <liblangutil/CharStreamProvider.h>
2019-05-09 15:56:25 +00:00
#include <libsolidity/interface/OptimiserSettings.h>
// The following headers are generated from the
// yul files placed in libyul/backends/wasm/polyfill.
#include <ewasmPolyfills/Arithmetic.h>
#include <ewasmPolyfills/Bitwise.h>
#include <ewasmPolyfills/Comparison.h>
#include <ewasmPolyfills/Conversion.h>
#include <ewasmPolyfills/Interface.h>
#include <ewasmPolyfills/Keccak.h>
#include <ewasmPolyfills/Logical.h>
#include <ewasmPolyfills/Memory.h>
2019-05-09 15:56:25 +00:00
using namespace std;
2019-12-11 16:31:36 +00:00
using namespace solidity;
using namespace solidity::yul;
using namespace solidity::util;
using namespace solidity::langutil;
2019-05-09 15:56:25 +00:00
Object EVMToEwasmTranslator::run(Object const& _object)
2019-05-09 15:56:25 +00:00
{
if (!m_polyfill)
parsePolyfill();
Block ast = std::get<Block>(Disambiguator(m_dialect, *_object.analysisInfo)(*_object.code));
2019-09-23 14:32:50 +00:00
set<YulString> reservedIdentifiers;
NameDispenser nameDispenser{m_dialect, ast, reservedIdentifiers};
// expectedExecutionsPerDeployment is currently unused.
OptimiserStepContext context{
m_dialect,
nameDispenser,
reservedIdentifiers,
frontend::OptimiserSettings::standard().expectedExecutionsPerDeployment
};
2019-09-23 14:32:50 +00:00
FunctionHoister::run(context, ast);
FunctionGrouper::run(context, ast);
MainFunction::run(context, ast);
2019-11-19 23:00:34 +00:00
ForLoopConditionIntoBody::run(context, ast);
2019-09-23 14:32:50 +00:00
ExpressionSplitter::run(context, ast);
2020-02-20 15:17:09 +00:00
WordSizeTransform::run(m_dialect, WasmDialect::instance(), ast, nameDispenser);
2019-05-09 15:56:25 +00:00
NameDisplacer{nameDispenser, m_polyfillFunctions}(ast);
for (auto const& st: m_polyfill->statements)
ast.statements.emplace_back(ASTCopier{}.translate(st));
Object ret;
2019-10-31 15:45:33 +00:00
ret.name = _object.name;
2019-05-09 15:56:25 +00:00
ret.code = make_shared<Block>(move(ast));
ret.debugData = _object.debugData;
2019-05-09 15:56:25 +00:00
ret.analysisInfo = make_shared<AsmAnalysisInfo>();
ErrorList errors;
ErrorReporter errorReporter(errors);
AsmAnalyzer analyzer(*ret.analysisInfo, errorReporter, WasmDialect::instance(), {}, _object.qualifiedDataNames());
2019-05-09 15:56:25 +00:00
if (!analyzer.analyze(*ret.code))
{
2020-02-19 15:56:53 +00:00
string message = "Invalid code generated after EVM to wasm translation.\n";
message += "Note that the source locations in the errors below will reference the original, not the translated code.\n";
message += "Translated code:\n";
message += "----------------------------------\n";
message += ret.toString(&WasmDialect::instance());
message += "----------------------------------\n";
2019-05-09 15:56:25 +00:00
for (auto const& err: errors)
2021-06-29 12:38:59 +00:00
message += langutil::SourceReferenceFormatter::formatErrorInformation(*err, m_charStreamProvider);
2019-05-09 15:56:25 +00:00
yulAssert(false, message);
}
for (auto const& subObjectNode: _object.subObjects)
if (Object const* subObject = dynamic_cast<Object const*>(subObjectNode.get()))
ret.subObjects.push_back(make_shared<Object>(run(*subObject)));
else
ret.subObjects.push_back(make_shared<Data>(dynamic_cast<Data const&>(*subObjectNode)));
ret.subIndexByName = _object.subIndexByName;
return ret;
}
void EVMToEwasmTranslator::parsePolyfill()
2019-05-09 15:56:25 +00:00
{
ErrorList errors;
ErrorReporter errorReporter(errors);
2021-07-14 10:53:39 +00:00
CharStream charStream(
"{" +
string(solidity::yul::wasm::polyfill::Arithmetic) +
string(solidity::yul::wasm::polyfill::Bitwise) +
string(solidity::yul::wasm::polyfill::Comparison) +
string(solidity::yul::wasm::polyfill::Conversion) +
string(solidity::yul::wasm::polyfill::Interface) +
string(solidity::yul::wasm::polyfill::Keccak) +
string(solidity::yul::wasm::polyfill::Logical) +
string(solidity::yul::wasm::polyfill::Memory) +
2021-07-14 10:53:39 +00:00
"}", "");
// Passing an empty SourceLocation() here is a workaround to prevent a crash
// when compiling from yul->ewasm. We're stripping nativeLocation and
// originLocation from the AST (but we only really need to strip nativeLocation)
m_polyfill = Parser(errorReporter, WasmDialect::instance(), langutil::SourceLocation()).parse(charStream);
2019-05-09 15:56:25 +00:00
if (!errors.empty())
{
string message;
for (auto const& err: errors)
2021-06-29 12:38:59 +00:00
message += langutil::SourceReferenceFormatter::formatErrorInformation(
*err,
2021-07-14 10:53:39 +00:00
SingletonCharStreamProvider(charStream)
2021-06-29 12:38:59 +00:00
);
2019-05-09 15:56:25 +00:00
yulAssert(false, message);
}
m_polyfillFunctions.clear();
for (auto const& statement: m_polyfill->statements)
m_polyfillFunctions.insert(std::get<FunctionDefinition>(statement).name);
2019-05-09 15:56:25 +00:00
}