Improve formatting.

This commit is contained in:
chriseth 2019-07-11 17:02:53 +02:00
parent e3433aa4eb
commit 6c31a5f3dd
3 changed files with 28 additions and 18 deletions

View File

@ -89,12 +89,14 @@ string EWasmToText::operator()(wasm::Label const& _label)
string EWasmToText::operator()(wasm::BuiltinCall const& _builtinCall)
{
return "(" + _builtinCall.functionName + " " + joinTransformed(_builtinCall.arguments) + ")";
string args = joinTransformed(_builtinCall.arguments);
return "(" + _builtinCall.functionName + (args.empty() ? "" : " " + args) + ")";
}
string EWasmToText::operator()(wasm::FunctionCall const& _functionCall)
{
return "(call $" + _functionCall.functionName + " " + joinTransformed(_functionCall.arguments) + ")";
string args = joinTransformed(_functionCall.arguments);
return "(call $" + _functionCall.functionName + (args.empty() ? "" : " " + args) + ")";
}
string EWasmToText::operator()(wasm::LocalAssignment const& _assignment)
@ -109,16 +111,16 @@ string EWasmToText::operator()(wasm::GlobalAssignment const& _assignment)
string EWasmToText::operator()(wasm::If const& _if)
{
string text = "(if " + visit(*_if.condition) + " (then\n" + indented(joinTransformed(_if.statements)) + ")";
string text = "(if " + visit(*_if.condition) + " (then\n" + indented(joinTransformed(_if.statements, '\n')) + ")";
if (_if.elseStatements)
text += "(else\n" + indented(joinTransformed(*_if.elseStatements)) + ")";
text += "(else\n" + indented(joinTransformed(*_if.elseStatements, '\n')) + ")";
return std::move(text) + ")\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";
return "(loop" + move(label) + "\n" + indented(joinTransformed(_loop.statements, '\n')) + ")\n";
}
string EWasmToText::operator()(wasm::Break const& _break)
@ -134,17 +136,22 @@ string EWasmToText::operator()(wasm::Continue const& _continue)
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";
return "(block" + move(label) + "\n" + indented(joinTransformed(_block.statements, '\n')) + "\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);
replacement.reserve(_in.size() + 4);
replacement += " ";
for (auto it = _in.begin(); it != _in.end(); ++it)
if (*it == '\n' && it + 1 != _in.end() && *(it + 1) != '\n')
replacement += "\n ";
else
replacement += *it;
}
return replacement;
}
@ -171,14 +178,14 @@ string EWasmToText::visit(wasm::Expression const& _expression)
return boost::apply_visitor(*this, _expression);
}
string EWasmToText::joinTransformed(vector<wasm::Expression> const& _expressions)
string EWasmToText::joinTransformed(vector<wasm::Expression> const& _expressions, char _separator)
{
string ret;
for (auto const& e: _expressions)
{
string t = visit(e);
if (!t.empty() && !ret.empty() && ret.back() != '\n')
ret += ' ';
ret += _separator;
ret += move(t);
}
return ret;

View File

@ -59,7 +59,10 @@ private:
std::string transform(wasm::FunctionDefinition const& _function);
std::string visit(wasm::Expression const& _expression);
std::string joinTransformed(std::vector<wasm::Expression> const& _expressions);
std::string joinTransformed(
std::vector<wasm::Expression> const& _expressions,
char _separator = ' '
);
};
}

View File

@ -35,9 +35,9 @@
(result i64)
(local $v i64)
(if (i64.ne (i64.const 0) (i64.or (i64.or (get_local $x1) (get_local $x2)) (get_local $x3))) (then
(unreachable )))
(unreachable)))
(if (i64.ne (i64.const 0) (i64.shr_u (get_local $x4) (i64.const 32))) (then
(unreachable )))
(unreachable)))
(set_local $v (get_local $x4))
(get_local $v)
)
@ -102,14 +102,14 @@
(set_local $_5 (get_global $global_))
(set_local $_6 (get_global $global__1))
(set_local $_7 (get_global $global__2))
)
(block
(set_local $_8 (dataoffset \"C_2_deployed\"))
(set_local $_9 (get_global $global_))
(set_local $_10 (get_global $global__1))
(set_local $_11 (get_global $global__2))
)
(call $eth.codeCopy (call $u256_to_i32 (get_local $_1) (get_local $_1) (get_local $_1) (get_local $_1)) (call $u256_to_i32 (get_local $_8) (get_local $_9) (get_local $_10) (get_local $_11)) (call $u256_to_i32 (get_local $_4) (get_local $_5) (get_local $_6) (get_local $_7))) (call $eth.finish (call $u256_to_i32 (get_local $_1) (get_local $_1) (get_local $_1) (get_local $_1)) (call $u256_to_i32 (get_local $_4) (get_local $_5) (get_local $_6) (get_local $_7)))
)
@ -122,9 +122,9 @@
(result i64)
(local $v i64)
(if (i64.ne (i64.const 0) (i64.or (i64.or (get_local $x1) (get_local $x2)) (get_local $x3))) (then
(unreachable )))
(unreachable)))
(if (i64.ne (i64.const 0) (i64.shr_u (get_local $x4) (i64.const 32))) (then
(unreachable )))
(unreachable)))
(set_local $v (get_local $x4))
(get_local $v)
)