From f6c6871bce382c9cf56660cac1870241f53671c6 Mon Sep 17 00:00:00 2001 From: chriseth Date: Thu, 18 Apr 2019 16:37:59 +0200 Subject: [PATCH] EWasm to text transformation. --- libyul/CMakeLists.txt | 2 + libyul/backends/wasm/EWasmCodeTransform.cpp | 6 +- libyul/backends/wasm/EWasmToText.cpp | 142 ++++++++++++++++++++ libyul/backends/wasm/EWasmToText.h | 58 ++++++++ 4 files changed, 205 insertions(+), 3 deletions(-) create mode 100644 libyul/backends/wasm/EWasmToText.cpp create mode 100644 libyul/backends/wasm/EWasmToText.h diff --git a/libyul/CMakeLists.txt b/libyul/CMakeLists.txt index 90534eac7..7052e6ab7 100644 --- a/libyul/CMakeLists.txt +++ b/libyul/CMakeLists.txt @@ -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 diff --git a/libyul/backends/wasm/EWasmCodeTransform.cpp b/libyul/backends/wasm/EWasmCodeTransform.cpp index 162bbb204..1ca102a3f 100644 --- a/libyul/backends/wasm/EWasmCodeTransform.cpp +++ b/libyul/backends/wasm/EWasmCodeTransform.cpp @@ -20,6 +20,8 @@ #include +#include + #include #include #include @@ -43,9 +45,7 @@ string EWasmCodeTransform::run(yul::Block const& _ast) functions.emplace_back(translateFunction(boost::get(statement))); } - // TODO translate to text representation - - return {}; + return EWasmToText{}.run(functions); } wasm::Expression EWasmCodeTransform::operator()(VariableDeclaration const& _varDecl) diff --git a/libyul/backends/wasm/EWasmToText.cpp b/libyul/backends/wasm/EWasmToText.cpp new file mode 100644 index 000000000..0ae94c6a4 --- /dev/null +++ b/libyul/backends/wasm/EWasmToText.cpp @@ -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 . +*/ +/** + * Component that transforms interval EWasm representation to text. + */ + +#include + +#include + +#include +#include +#include + +using namespace std; +using namespace yul; + +string EWasmToText::run(vector 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 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; +} diff --git a/libyul/backends/wasm/EWasmToText.h b/libyul/backends/wasm/EWasmToText.h new file mode 100644 index 000000000..1a752c483 --- /dev/null +++ b/libyul/backends/wasm/EWasmToText.h @@ -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 . +*/ +/** + * Component that transforms interval EWasm representation to text. + */ + +#pragma once + +#include + +#include + +namespace yul +{ +struct AsmAnalysisInfo; + +class EWasmToText: public boost::static_visitor +{ +public: + std::string run(std::vector 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 const& _expressions); +}; + +}