diff --git a/libyul/backends/wasm/EWasmToText.cpp b/libyul/backends/wasm/EWasmToText.cpp index ac49247fa..9d3bce0f5 100644 --- a/libyul/backends/wasm/EWasmToText.cpp +++ b/libyul/backends/wasm/EWasmToText.cpp @@ -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 const& _expressions) +string EWasmToText::joinTransformed(vector 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; diff --git a/libyul/backends/wasm/EWasmToText.h b/libyul/backends/wasm/EWasmToText.h index b8bafc0cd..7eed8c076 100644 --- a/libyul/backends/wasm/EWasmToText.h +++ b/libyul/backends/wasm/EWasmToText.h @@ -59,7 +59,10 @@ private: std::string transform(wasm::FunctionDefinition const& _function); std::string visit(wasm::Expression const& _expression); - std::string joinTransformed(std::vector const& _expressions); + std::string joinTransformed( + std::vector const& _expressions, + char _separator = ' ' + ); }; } diff --git a/test/cmdlineTests/standard_eWasm_requested/output.json b/test/cmdlineTests/standard_eWasm_requested/output.json index 2707bc702..a0956a399 100644 --- a/test/cmdlineTests/standard_eWasm_requested/output.json +++ b/test/cmdlineTests/standard_eWasm_requested/output.json @@ -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) )