Add support for generating code with i32 variables in text and binary wasm

This commit is contained in:
Kamil Śliwak
2020-06-11 02:44:23 +02:00
parent 6a82d32ef6
commit e67f5072df
7 changed files with 177 additions and 29 deletions
+9 -6
View File
@@ -23,6 +23,7 @@
#include <libyul/Exceptions.h>
#include <libsolutil/StringUtils.h>
#include <libsolutil/Visitor.h>
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/replace.hpp>
@@ -58,7 +59,7 @@ string TextTransform::run(wasm::Module const& _module)
ret += " (export \"main\" (func $main))\n";
for (auto const& g: _module.globals)
ret += " (global $" + g.variableName + " (mut i64) (i64.const 0))\n";
ret += " (global $" + g.variableName + " (mut " + encodeType(g.type) + ") (" + encodeType(g.type) + ".const 0))\n";
ret += "\n";
for (auto const& f: _module.functions)
ret += transform(f) + "\n";
@@ -67,8 +68,10 @@ string TextTransform::run(wasm::Module const& _module)
string TextTransform::operator()(wasm::Literal const& _literal)
{
yulAssert(holds_alternative<uint64_t>(_literal.value), "");
return "(i64.const " + to_string(get<uint64_t>(_literal.value)) + ")";
return std::visit(GenericVisitor{
[&](uint32_t _value) -> string { return "(i32.const " + to_string(_value) + ")"; },
[&](uint64_t _value) -> string { return "(i64.const " + to_string(_value) + ")"; },
}, _literal.value);
}
string TextTransform::operator()(wasm::StringLiteral const& _literal)
@@ -166,11 +169,11 @@ string TextTransform::transform(wasm::FunctionDefinition const& _function)
{
string ret = "(func $" + _function.name + "\n";
for (auto const& param: _function.parameters)
ret += " (param $" + param.name + " i64)\n";
ret += " (param $" + param.name + " " + encodeType(param.type) + ")\n";
if (_function.returnType.has_value())
ret += " (result i64)\n";
ret += " (result " + encodeType(_function.returnType.value()) + ")\n";
for (auto const& local: _function.locals)
ret += " (local $" + local.variableName + " i64)\n";
ret += " (local $" + local.variableName + " " + encodeType(local.type) + ")\n";
ret += indented(joinTransformed(_function.body, '\n'));
if (ret.back() != '\n')
ret += '\n';