diff --git a/libyul/backends/wasm/EWasmAST.h b/libyul/backends/wasm/EWasmAST.h index f819ae3ab..39172db36 100644 --- a/libyul/backends/wasm/EWasmAST.h +++ b/libyul/backends/wasm/EWasmAST.h @@ -30,6 +30,7 @@ namespace wasm { struct Literal; +struct StringLiteral; struct LocalVariable; struct GlobalVariable; struct Label; @@ -43,12 +44,13 @@ struct Loop; struct Break; struct Continue; using Expression = boost::variant< - Literal, LocalVariable, GlobalVariable, Label, + Literal, StringLiteral, LocalVariable, GlobalVariable, Label, FunctionCall, BuiltinCall, LocalAssignment, GlobalAssignment, Block, If, Loop, Break, Continue >; struct Literal { uint64_t value; }; +struct StringLiteral { std::string value; }; struct LocalVariable { std::string name; }; struct GlobalVariable { std::string name; }; struct Label { std::string name; }; diff --git a/libyul/backends/wasm/EWasmCodeTransform.cpp b/libyul/backends/wasm/EWasmCodeTransform.cpp index 91aa37965..e345c4946 100644 --- a/libyul/backends/wasm/EWasmCodeTransform.cpp +++ b/libyul/backends/wasm/EWasmCodeTransform.cpp @@ -126,8 +126,18 @@ wasm::Expression EWasmCodeTransform::operator()(FunctionalInstruction const& _f) wasm::Expression EWasmCodeTransform::operator()(FunctionCall const& _call) { - if (m_dialect.builtin(_call.functionName.name)) - return wasm::BuiltinCall{_call.functionName.name.str(), visit(_call.arguments)}; + if (BuiltinFunction const* builtin = m_dialect.builtin(_call.functionName.name)) + { + if (builtin->literalArguments) + { + vector literals; + for (auto const& arg: _call.arguments) + literals.emplace_back(wasm::StringLiteral{boost::get(arg).value.str()}); + return wasm::BuiltinCall{_call.functionName.name.str(), std::move(literals)}; + } + else + return wasm::BuiltinCall{_call.functionName.name.str(), visit(_call.arguments)}; + } else // If this function returns multiple values, then the first one will // be returned in the expression itself and the others in global variables. diff --git a/libyul/backends/wasm/EWasmToText.cpp b/libyul/backends/wasm/EWasmToText.cpp index 4e2bf5c2b..ec6060e71 100644 --- a/libyul/backends/wasm/EWasmToText.cpp +++ b/libyul/backends/wasm/EWasmToText.cpp @@ -56,6 +56,13 @@ string EWasmToText::operator()(wasm::Literal const& _literal) return "(i64.const " + to_string(_literal.value) + ")"; } +string EWasmToText::operator()(wasm::StringLiteral const& _literal) +{ + string quoted = boost::replace_all_copy(_literal.value, "\\", "\\\\"); + boost::replace_all(quoted, "\"", "\\\""); + return "\"" + quoted + "\""; +} + string EWasmToText::operator()(wasm::LocalVariable const& _identifier) { return "(get_local $" + _identifier.name + ")"; diff --git a/libyul/backends/wasm/EWasmToText.h b/libyul/backends/wasm/EWasmToText.h index 61bcf5b32..91c64daba 100644 --- a/libyul/backends/wasm/EWasmToText.h +++ b/libyul/backends/wasm/EWasmToText.h @@ -38,6 +38,7 @@ public: public: std::string operator()(wasm::Literal const& _literal); + std::string operator()(wasm::StringLiteral const& _literal); std::string operator()(wasm::LocalVariable const& _identifier); std::string operator()(wasm::GlobalVariable const& _identifier); std::string operator()(wasm::Label const& _label);