Rename EWasmToText to TextTransform

This commit is contained in:
Alex Beregszaszi 2019-12-09 16:43:22 +00:00
parent 7247e72d8b
commit ae7cc58b55
5 changed files with 34 additions and 29 deletions

View File

@ -50,10 +50,10 @@ add_library(yul
backends/wasm/EWasmCodeTransform.h backends/wasm/EWasmCodeTransform.h
backends/wasm/EWasmObjectCompiler.cpp backends/wasm/EWasmObjectCompiler.cpp
backends/wasm/EWasmObjectCompiler.h backends/wasm/EWasmObjectCompiler.h
backends/wasm/EWasmToText.cpp
backends/wasm/EWasmToText.h
backends/wasm/BinaryTransform.cpp backends/wasm/BinaryTransform.cpp
backends/wasm/BinaryTransform.h backends/wasm/BinaryTransform.h
backends/wasm/TextTransform.cpp
backends/wasm/TextTransform.h
backends/wasm/WasmDialect.cpp backends/wasm/WasmDialect.cpp
backends/wasm/WasmDialect.h backends/wasm/WasmDialect.h
backends/wasm/WordSizeTransform.cpp backends/wasm/WordSizeTransform.cpp

View File

@ -15,14 +15,14 @@
along with solidity. If not, see <http://www.gnu.org/licenses/>. along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/ */
/** /**
* Compiler that transforms Yul Objects to EWasm text representation. * Compiler that transforms Yul Objects to Wasm text and binary representation (Ewasm flavoured).
*/ */
#include <libyul/backends/wasm/EWasmObjectCompiler.h> #include <libyul/backends/wasm/EWasmObjectCompiler.h>
#include <libyul/backends/wasm/EWasmCodeTransform.h> #include <libyul/backends/wasm/EWasmCodeTransform.h>
#include <libyul/backends/wasm/BinaryTransform.h> #include <libyul/backends/wasm/BinaryTransform.h>
#include <libyul/backends/wasm/EWasmToText.h> #include <libyul/backends/wasm/TextTransform.h>
#include <libyul/Object.h> #include <libyul/Object.h>
#include <libyul/Exceptions.h> #include <libyul/Exceptions.h>
@ -36,7 +36,7 @@ pair<string, dev::bytes> EWasmObjectCompiler::compile(Object& _object, Dialect c
{ {
EWasmObjectCompiler compiler(_dialect); EWasmObjectCompiler compiler(_dialect);
wasm::Module module = compiler.run(_object); wasm::Module module = compiler.run(_object);
return {EWasmToText().run(module), wasm::BinaryTransform::run(module)}; return {wasm::TextTransform().run(module), wasm::BinaryTransform::run(module)};
} }
wasm::Module EWasmObjectCompiler::run(Object& _object) wasm::Module EWasmObjectCompiler::run(Object& _object)

View File

@ -15,7 +15,7 @@
along with solidity. If not, see <http://www.gnu.org/licenses/>. along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/ */
/** /**
* Compiler that transforms Yul Objects to EWasm text representation. * Compiler that transforms Yul Objects to Wasm text and binary representation (Ewasm flavoured).
*/ */
#pragma once #pragma once

View File

@ -15,10 +15,10 @@
along with solidity. If not, see <http://www.gnu.org/licenses/>. along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/ */
/** /**
* Component that transforms interval EWasm representation to text. * Component that transforms interval Wasm representation to text.
*/ */
#include <libyul/backends/wasm/EWasmToText.h> #include <libyul/backends/wasm/TextTransform.h>
#include <libdevcore/StringUtils.h> #include <libdevcore/StringUtils.h>
@ -29,8 +29,9 @@
using namespace std; using namespace std;
using namespace yul; using namespace yul;
using namespace dev; using namespace dev;
using namespace yul::wasm;
string EWasmToText::run(wasm::Module const& _module) string TextTransform::run(wasm::Module const& _module)
{ {
string ret = "(module\n"; string ret = "(module\n";
for (auto const& sub: _module.subModules) for (auto const& sub: _module.subModules)
@ -61,51 +62,51 @@ string EWasmToText::run(wasm::Module const& _module)
return move(ret) + ")\n"; return move(ret) + ")\n";
} }
string EWasmToText::operator()(wasm::Literal const& _literal) string TextTransform::operator()(wasm::Literal const& _literal)
{ {
return "(i64.const " + to_string(_literal.value) + ")"; return "(i64.const " + to_string(_literal.value) + ")";
} }
string EWasmToText::operator()(wasm::StringLiteral const& _literal) string TextTransform::operator()(wasm::StringLiteral const& _literal)
{ {
string quoted = boost::replace_all_copy(_literal.value, "\\", "\\\\"); string quoted = boost::replace_all_copy(_literal.value, "\\", "\\\\");
boost::replace_all(quoted, "\"", "\\\""); boost::replace_all(quoted, "\"", "\\\"");
return "\"" + quoted + "\""; return "\"" + quoted + "\"";
} }
string EWasmToText::operator()(wasm::LocalVariable const& _identifier) string TextTransform::operator()(wasm::LocalVariable const& _identifier)
{ {
return "(local.get $" + _identifier.name + ")"; return "(local.get $" + _identifier.name + ")";
} }
string EWasmToText::operator()(wasm::GlobalVariable const& _identifier) string TextTransform::operator()(wasm::GlobalVariable const& _identifier)
{ {
return "(global.get $" + _identifier.name + ")"; return "(global.get $" + _identifier.name + ")";
} }
string EWasmToText::operator()(wasm::BuiltinCall const& _builtinCall) string TextTransform::operator()(wasm::BuiltinCall const& _builtinCall)
{ {
string args = joinTransformed(_builtinCall.arguments); string args = joinTransformed(_builtinCall.arguments);
return "(" + _builtinCall.functionName + (args.empty() ? "" : " " + args) + ")"; return "(" + _builtinCall.functionName + (args.empty() ? "" : " " + args) + ")";
} }
string EWasmToText::operator()(wasm::FunctionCall const& _functionCall) string TextTransform::operator()(wasm::FunctionCall const& _functionCall)
{ {
string args = joinTransformed(_functionCall.arguments); string args = joinTransformed(_functionCall.arguments);
return "(call $" + _functionCall.functionName + (args.empty() ? "" : " " + args) + ")"; return "(call $" + _functionCall.functionName + (args.empty() ? "" : " " + args) + ")";
} }
string EWasmToText::operator()(wasm::LocalAssignment const& _assignment) string TextTransform::operator()(wasm::LocalAssignment const& _assignment)
{ {
return "(local.set $" + _assignment.variableName + " " + visit(*_assignment.value) + ")\n"; return "(local.set $" + _assignment.variableName + " " + visit(*_assignment.value) + ")\n";
} }
string EWasmToText::operator()(wasm::GlobalAssignment const& _assignment) string TextTransform::operator()(wasm::GlobalAssignment const& _assignment)
{ {
return "(global.set $" + _assignment.variableName + " " + visit(*_assignment.value) + ")\n"; return "(global.set $" + _assignment.variableName + " " + visit(*_assignment.value) + ")\n";
} }
string EWasmToText::operator()(wasm::If const& _if) string TextTransform::operator()(wasm::If const& _if)
{ {
string text = "(if " + visit(*_if.condition) + " (then\n" + indented(joinTransformed(_if.statements, '\n')) + ")"; string text = "(if " + visit(*_if.condition) + " (then\n" + indented(joinTransformed(_if.statements, '\n')) + ")";
if (_if.elseStatements) if (_if.elseStatements)
@ -113,34 +114,34 @@ string EWasmToText::operator()(wasm::If const& _if)
return std::move(text) + ")\n"; return std::move(text) + ")\n";
} }
string EWasmToText::operator()(wasm::Loop const& _loop) string TextTransform::operator()(wasm::Loop const& _loop)
{ {
string label = _loop.labelName.empty() ? "" : " $" + _loop.labelName; string label = _loop.labelName.empty() ? "" : " $" + _loop.labelName;
return "(loop" + move(label) + "\n" + indented(joinTransformed(_loop.statements, '\n')) + ")\n"; return "(loop" + move(label) + "\n" + indented(joinTransformed(_loop.statements, '\n')) + ")\n";
} }
string EWasmToText::operator()(wasm::Break const& _break) string TextTransform::operator()(wasm::Break const& _break)
{ {
return "(break $" + _break.label.name + ")\n"; return "(break $" + _break.label.name + ")\n";
} }
string EWasmToText::operator()(wasm::BreakIf const& _break) string TextTransform::operator()(wasm::BreakIf const& _break)
{ {
return "(br_if $" + _break.label.name + " " + visit(*_break.condition) + ")\n"; return "(br_if $" + _break.label.name + " " + visit(*_break.condition) + ")\n";
} }
string EWasmToText::operator()(wasm::Return const&) string TextTransform::operator()(wasm::Return const&)
{ {
return "(return)\n"; return "(return)\n";
} }
string EWasmToText::operator()(wasm::Block const& _block) string TextTransform::operator()(wasm::Block const& _block)
{ {
string label = _block.labelName.empty() ? "" : " $" + _block.labelName; string label = _block.labelName.empty() ? "" : " $" + _block.labelName;
return "(block" + move(label) + "\n" + indented(joinTransformed(_block.statements, '\n')) + "\n)\n"; return "(block" + move(label) + "\n" + indented(joinTransformed(_block.statements, '\n')) + "\n)\n";
} }
string EWasmToText::indented(string const& _in) string TextTransform::indented(string const& _in)
{ {
string replacement; string replacement;
@ -157,7 +158,7 @@ string EWasmToText::indented(string const& _in)
return replacement; return replacement;
} }
string EWasmToText::transform(wasm::FunctionDefinition const& _function) string TextTransform::transform(wasm::FunctionDefinition const& _function)
{ {
string ret = "(func $" + _function.name + "\n"; string ret = "(func $" + _function.name + "\n";
for (auto const& param: _function.parameterNames) for (auto const& param: _function.parameterNames)
@ -174,12 +175,12 @@ string EWasmToText::transform(wasm::FunctionDefinition const& _function)
} }
string EWasmToText::visit(wasm::Expression const& _expression) string TextTransform::visit(wasm::Expression const& _expression)
{ {
return std::visit(*this, _expression); return std::visit(*this, _expression);
} }
string EWasmToText::joinTransformed(vector<wasm::Expression> const& _expressions, char _separator) string TextTransform::joinTransformed(vector<wasm::Expression> const& _expressions, char _separator)
{ {
string ret; string ret;
for (auto const& e: _expressions) for (auto const& e: _expressions)

View File

@ -15,7 +15,7 @@
along with solidity. If not, see <http://www.gnu.org/licenses/>. along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/ */
/** /**
* Component that transforms interval EWasm representation to text. * Component that transforms interval Wasm representation to text.
*/ */
#pragma once #pragma once
@ -28,7 +28,10 @@ namespace yul
{ {
struct AsmAnalysisInfo; struct AsmAnalysisInfo;
class EWasmToText namespace wasm
{
class TextTransform
{ {
public: public:
std::string run(wasm::Module const& _module); std::string run(wasm::Module const& _module);
@ -62,3 +65,4 @@ private:
}; };
} }
}