Support "datasize" and "dataoffset" with literals in eWasm.

This commit is contained in:
chriseth 2019-06-24 16:51:59 +02:00
parent edf62e4d2b
commit 8cd197e572
4 changed files with 23 additions and 3 deletions

View File

@ -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; };

View File

@ -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<wasm::Expression> literals;
for (auto const& arg: _call.arguments)
literals.emplace_back(wasm::StringLiteral{boost::get<Literal>(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.

View File

@ -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 + ")";

View File

@ -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);