2019-04-18 12:39:48 +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
|
2019-04-18 12:39:48 +00:00
|
|
|
/**
|
2019-12-11 13:27:24 +00:00
|
|
|
* Common code generator for translating Yul / inline assembly to Wasm.
|
2019-04-18 12:39:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2019-12-10 23:43:52 +00:00
|
|
|
#include <libyul/backends/wasm/WasmAST.h>
|
2020-10-29 14:00:27 +00:00
|
|
|
#include <libyul/ASTForward.h>
|
2019-04-18 12:39:48 +00:00
|
|
|
#include <libyul/Dialect.h>
|
2019-04-23 08:45:16 +00:00
|
|
|
#include <libyul/optimiser/NameDispenser.h>
|
2020-05-28 19:44:52 +00:00
|
|
|
#include <libyul/optimiser/TypeInfo.h>
|
|
|
|
|
|
|
|
#include <libsolutil/Common.h>
|
2019-04-18 12:39:48 +00:00
|
|
|
|
|
|
|
#include <stack>
|
2019-07-09 13:19:50 +00:00
|
|
|
#include <map>
|
2019-04-18 12:39:48 +00:00
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity::yul
|
2019-04-18 12:39:48 +00:00
|
|
|
{
|
|
|
|
struct AsmAnalysisInfo;
|
|
|
|
|
2019-12-11 13:27:24 +00:00
|
|
|
class WasmCodeTransform
|
2019-04-18 12:39:48 +00:00
|
|
|
{
|
|
|
|
public:
|
2019-10-31 16:27:52 +00:00
|
|
|
static wasm::Module run(Dialect const& _dialect, yul::Block const& _ast);
|
2019-04-18 12:39:48 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
wasm::Expression operator()(yul::Literal const& _literal);
|
|
|
|
wasm::Expression operator()(yul::Identifier const& _identifier);
|
|
|
|
wasm::Expression operator()(yul::FunctionCall const&);
|
|
|
|
wasm::Expression operator()(yul::ExpressionStatement const& _statement);
|
|
|
|
wasm::Expression operator()(yul::Assignment const& _assignment);
|
|
|
|
wasm::Expression operator()(yul::VariableDeclaration const& _varDecl);
|
|
|
|
wasm::Expression operator()(yul::If const& _if);
|
|
|
|
wasm::Expression operator()(yul::Switch const& _switch);
|
|
|
|
wasm::Expression operator()(yul::FunctionDefinition const&);
|
|
|
|
wasm::Expression operator()(yul::ForLoop const&);
|
|
|
|
wasm::Expression operator()(yul::Break const&);
|
|
|
|
wasm::Expression operator()(yul::Continue const&);
|
2019-10-28 14:25:02 +00:00
|
|
|
wasm::Expression operator()(yul::Leave const&);
|
2019-04-18 12:39:48 +00:00
|
|
|
wasm::Expression operator()(yul::Block const& _block);
|
|
|
|
|
|
|
|
private:
|
2019-12-11 13:27:24 +00:00
|
|
|
WasmCodeTransform(
|
2019-04-23 08:45:16 +00:00
|
|
|
Dialect const& _dialect,
|
2020-05-28 19:44:52 +00:00
|
|
|
Block const& _ast,
|
|
|
|
TypeInfo& _typeInfo
|
2019-04-23 08:45:16 +00:00
|
|
|
):
|
|
|
|
m_dialect(_dialect),
|
2020-05-28 19:44:52 +00:00
|
|
|
m_nameDispenser(_dialect, _ast),
|
|
|
|
m_typeInfo(_typeInfo)
|
2019-04-23 08:45:16 +00:00
|
|
|
{}
|
|
|
|
|
2019-04-18 12:39:48 +00:00
|
|
|
std::unique_ptr<wasm::Expression> visit(yul::Expression const& _expression);
|
|
|
|
wasm::Expression visitReturnByValue(yul::Expression const& _expression);
|
|
|
|
std::vector<wasm::Expression> visit(std::vector<yul::Expression> const& _expressions);
|
|
|
|
wasm::Expression visit(yul::Statement const& _statement);
|
|
|
|
std::vector<wasm::Expression> visit(std::vector<yul::Statement> const& _statements);
|
|
|
|
|
2019-04-23 08:45:16 +00:00
|
|
|
/// Returns an assignment or a block containing multiple assignments.
|
|
|
|
/// @param _variableNames the names of the variables to assign to
|
|
|
|
/// @param _firstValue the value to be assigned to the first variable. If there
|
|
|
|
/// is more than one variable, the values are taken from m_globalVariables.
|
|
|
|
wasm::Expression generateMultiAssignment(
|
|
|
|
std::vector<std::string> _variableNames,
|
|
|
|
std::unique_ptr<wasm::Expression> _firstValue
|
|
|
|
);
|
|
|
|
|
2019-04-18 12:39:48 +00:00
|
|
|
wasm::FunctionDefinition translateFunction(yul::FunctionDefinition const& _funDef);
|
|
|
|
|
2020-09-30 00:23:47 +00:00
|
|
|
/// Imports an external function into the current module.
|
|
|
|
/// @param _builtin _builtin the builtin that will be imported into the current module.
|
|
|
|
/// @param _module _module the module name under which the external function can be found.
|
|
|
|
/// @param _externalName the name of the external function within the module _module.
|
|
|
|
/// @param _internalName the name of the internal function under that the external function is accessible.
|
|
|
|
void importBuiltinFunction(BuiltinFunction const* _builtin, std::string const& _module, std::string const& _externalName, std::string const& _internalName);
|
|
|
|
|
2019-04-18 12:39:48 +00:00
|
|
|
std::string newLabel();
|
2020-05-28 19:44:52 +00:00
|
|
|
/// Selects a subset of global variables matching specified sequence of variable types.
|
|
|
|
/// Defines more global variables of a given type if there's not enough.
|
|
|
|
std::vector<size_t> allocateGlobals(std::vector<wasm::Type> const& _typesForGlobals);
|
2019-04-23 08:45:16 +00:00
|
|
|
|
2020-06-10 22:54:12 +00:00
|
|
|
static wasm::Type translatedType(yul::Type _yulType);
|
2020-05-28 19:44:52 +00:00
|
|
|
static wasm::Literal makeLiteral(wasm::Type _type, u256 _value);
|
2020-06-10 22:54:12 +00:00
|
|
|
|
2019-04-23 08:45:16 +00:00
|
|
|
Dialect const& m_dialect;
|
|
|
|
NameDispenser m_nameDispenser;
|
2019-04-18 12:39:48 +00:00
|
|
|
|
|
|
|
std::vector<wasm::VariableDeclaration> m_localVariables;
|
2019-04-23 08:45:16 +00:00
|
|
|
std::vector<wasm::GlobalVariableDeclaration> m_globalVariables;
|
2019-07-09 13:19:50 +00:00
|
|
|
std::map<YulString, wasm::FunctionImport> m_functionsToImport;
|
2020-05-25 21:33:59 +00:00
|
|
|
std::string m_functionBodyLabel;
|
2019-04-18 12:39:48 +00:00
|
|
|
std::stack<std::pair<std::string, std::string>> m_breakContinueLabelNames;
|
2020-05-28 19:44:52 +00:00
|
|
|
TypeInfo& m_typeInfo;
|
2019-04-18 12:39:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|