mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
EWasm to text transformation.
This commit is contained in:
parent
ec27f484a1
commit
f6c6871bce
@ -40,6 +40,8 @@ add_library(yul
|
||||
backends/wasm/EWasmCodeTransform.h
|
||||
backends/wasm/EWasmObjectCompiler.cpp
|
||||
backends/wasm/EWasmObjectCompiler.h
|
||||
backends/wasm/EWasmToText.cpp
|
||||
backends/wasm/EWasmToText.h
|
||||
backends/evm/NoOutputAssembly.h
|
||||
backends/evm/NoOutputAssembly.cpp
|
||||
backends/wasm/WasmDialect.cpp
|
||||
|
@ -20,6 +20,8 @@
|
||||
|
||||
#include <libyul/backends/wasm/EWasmCodeTransform.h>
|
||||
|
||||
#include <libyul/backends/wasm/EWasmToText.h>
|
||||
|
||||
#include <libyul/AsmData.h>
|
||||
#include <libyul/Dialect.h>
|
||||
#include <libyul/Utilities.h>
|
||||
@ -43,9 +45,7 @@ string EWasmCodeTransform::run(yul::Block const& _ast)
|
||||
functions.emplace_back(translateFunction(boost::get<yul::FunctionDefinition>(statement)));
|
||||
}
|
||||
|
||||
// TODO translate to text representation
|
||||
|
||||
return {};
|
||||
return EWasmToText{}.run(functions);
|
||||
}
|
||||
|
||||
wasm::Expression EWasmCodeTransform::operator()(VariableDeclaration const& _varDecl)
|
||||
|
142
libyul/backends/wasm/EWasmToText.cpp
Normal file
142
libyul/backends/wasm/EWasmToText.cpp
Normal file
@ -0,0 +1,142 @@
|
||||
/*
|
||||
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/>.
|
||||
*/
|
||||
/**
|
||||
* Component that transforms interval EWasm representation to text.
|
||||
*/
|
||||
|
||||
#include <libyul/backends/wasm/EWasmToText.h>
|
||||
|
||||
#include <libdevcore/StringUtils.h>
|
||||
|
||||
#include <boost/algorithm/string/join.hpp>
|
||||
#include <boost/algorithm/string/replace.hpp>
|
||||
#include <boost/range/adaptor/transformed.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace yul;
|
||||
|
||||
string EWasmToText::run(vector<wasm::FunctionDefinition> const& _functions)
|
||||
{
|
||||
string ret = "(module\n\n";
|
||||
for (auto const& f: _functions)
|
||||
ret += transform(f) + "\n";
|
||||
return move(ret) + ")\n";
|
||||
}
|
||||
|
||||
string EWasmToText::operator()(wasm::Literal const& _literal)
|
||||
{
|
||||
return "(i64.const " + to_string(_literal.value) + ")";
|
||||
}
|
||||
|
||||
string EWasmToText::operator()(wasm::Identifier const& _identifier)
|
||||
{
|
||||
return "(get.local $" + _identifier.name + ")";
|
||||
}
|
||||
|
||||
string EWasmToText::operator()(wasm::Label const& _label)
|
||||
{
|
||||
return "$" + _label.name;
|
||||
}
|
||||
|
||||
string EWasmToText::operator()(wasm::BuiltinCall const& _builtinCall)
|
||||
{
|
||||
return "(" + _builtinCall.functionName + " " + joinTransformed(_builtinCall.arguments) + ")";
|
||||
}
|
||||
|
||||
string EWasmToText::operator()(wasm::FunctionCall const& _functionCall)
|
||||
{
|
||||
return "(call $" + _functionCall.functionName + " " + joinTransformed(_functionCall.arguments) + ")";
|
||||
}
|
||||
|
||||
string EWasmToText::operator()(wasm::LocalAssignment const& _assignment)
|
||||
{
|
||||
return "(set_local $" + _assignment.variableName + " " + visit(*_assignment.value) + ")\n";
|
||||
}
|
||||
|
||||
string EWasmToText::operator()(wasm::If const& _if)
|
||||
{
|
||||
return "(if " + visit(*_if.condition) + " (then\n" + indented(joinTransformed(_if.statements)) + "\n))\n";
|
||||
}
|
||||
|
||||
string EWasmToText::operator()(wasm::Loop const& _loop)
|
||||
{
|
||||
string label = _loop.labelName.empty() ? "" : " $" + _loop.labelName;
|
||||
return "(loop" + move(label) + "\n" + indented(joinTransformed(_loop.statements)) + ")\n";
|
||||
}
|
||||
|
||||
string EWasmToText::operator()(wasm::Break const& _break)
|
||||
{
|
||||
return "(break $" + _break.label.name + ")\n";
|
||||
}
|
||||
|
||||
string EWasmToText::operator()(wasm::Continue const& _continue)
|
||||
{
|
||||
return "(continue $" + _continue.label.name + ")\n";
|
||||
}
|
||||
|
||||
string EWasmToText::operator()(wasm::Block const& _block)
|
||||
{
|
||||
string label = _block.labelName.empty() ? "" : " $" + _block.labelName;
|
||||
return "(block" + move(label) + "\n" + indented(joinTransformed(_block.statements)) + "\n)\n";
|
||||
}
|
||||
|
||||
string EWasmToText::indented(string const& _in)
|
||||
{
|
||||
string replacement;
|
||||
if (!_in.empty())
|
||||
{
|
||||
replacement = " " + boost::replace_all_copy(_in, "\n", "\n ");
|
||||
if (_in.back() == '\n')
|
||||
replacement = replacement.substr(0, replacement.size() - 4);
|
||||
}
|
||||
return replacement;
|
||||
}
|
||||
|
||||
string EWasmToText::transform(wasm::FunctionDefinition const& _function)
|
||||
{
|
||||
string ret = "(func $" + _function.name + "\n";
|
||||
for (auto const& param: _function.parameterNames)
|
||||
ret += " (param $" + param + " i64)\n";
|
||||
if (_function.returns)
|
||||
ret += " (result i64)\n";
|
||||
for (auto const& local: _function.locals)
|
||||
ret += " (local $" + local.variableName + " i64)\n";
|
||||
ret += indented(joinTransformed(_function.body));
|
||||
if (ret.back() != '\n')
|
||||
ret += '\n';
|
||||
ret += ")\n";
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
string EWasmToText::visit(wasm::Expression const& _expression)
|
||||
{
|
||||
return boost::apply_visitor(*this, _expression);
|
||||
}
|
||||
|
||||
string EWasmToText::joinTransformed(vector<wasm::Expression> const& _expressions)
|
||||
{
|
||||
string ret;
|
||||
for (auto const& e: _expressions)
|
||||
{
|
||||
string t = visit(e);
|
||||
if (!t.empty() && !ret.empty() && ret.back() != '\n')
|
||||
ret += ' ';
|
||||
ret += move(t);
|
||||
}
|
||||
return ret;
|
||||
}
|
58
libyul/backends/wasm/EWasmToText.h
Normal file
58
libyul/backends/wasm/EWasmToText.h
Normal file
@ -0,0 +1,58 @@
|
||||
/*
|
||||
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/>.
|
||||
*/
|
||||
/**
|
||||
* Component that transforms interval EWasm representation to text.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <libyul/backends/wasm/EWasmAST.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace yul
|
||||
{
|
||||
struct AsmAnalysisInfo;
|
||||
|
||||
class EWasmToText: public boost::static_visitor<std::string>
|
||||
{
|
||||
public:
|
||||
std::string run(std::vector<wasm::FunctionDefinition> const& _functions);
|
||||
|
||||
public:
|
||||
std::string operator()(wasm::Literal const& _literal);
|
||||
std::string operator()(wasm::Identifier const& _identifier);
|
||||
std::string operator()(wasm::Label const& _label);
|
||||
std::string operator()(wasm::BuiltinCall const& _builinCall);
|
||||
std::string operator()(wasm::FunctionCall const& _functionCall);
|
||||
std::string operator()(wasm::LocalAssignment const& _assignment);
|
||||
std::string operator()(wasm::If const& _if);
|
||||
std::string operator()(wasm::Loop const& _loop);
|
||||
std::string operator()(wasm::Break const& _break);
|
||||
std::string operator()(wasm::Continue const& _continue);
|
||||
std::string operator()(wasm::Block const& _block);
|
||||
|
||||
private:
|
||||
std::string indented(std::string const& _in);
|
||||
|
||||
std::string transform(wasm::FunctionDefinition const& _function);
|
||||
|
||||
std::string visit(wasm::Expression const& _expression);
|
||||
std::string joinTransformed(std::vector<wasm::Expression> const& _expressions);
|
||||
};
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user