Remove label from expression and add br_if.

This commit is contained in:
chriseth 2019-10-17 13:14:19 +02:00
parent 0657aff937
commit f8ade0122a
4 changed files with 11 additions and 11 deletions

View File

@ -33,7 +33,6 @@ struct Literal;
struct StringLiteral;
struct LocalVariable;
struct GlobalVariable;
struct Label;
struct FunctionCall;
struct BuiltinCall;
struct LocalAssignment;
@ -42,10 +41,11 @@ struct Block;
struct If;
struct Loop;
struct Break;
struct BreakIf;
using Expression = boost::variant<
Literal, StringLiteral, LocalVariable, GlobalVariable, Label,
Literal, StringLiteral, LocalVariable, GlobalVariable,
FunctionCall, BuiltinCall, LocalAssignment, GlobalAssignment,
Block, If, Loop, Break
Block, If, Loop, Break, BreakIf
>;
struct Literal { uint64_t value; };
@ -65,6 +65,7 @@ struct If {
};
struct Loop { std::string labelName; std::vector<Expression> statements; };
struct Break { Label label; };
struct BreakIf { Label label; std::unique_ptr<Expression> condition; };
struct VariableDeclaration { std::string variableName; };
struct GlobalVariableDeclaration { std::string variableName; };

View File

@ -248,8 +248,7 @@ wasm::Expression EWasmCodeTransform::operator()(ForLoop const& _for)
wasm::Loop loop;
loop.statements = visit(_for.pre.statements);
loop.statements.emplace_back(wasm::BuiltinCall{"br_if", make_vector<wasm::Expression>(
wasm::Label{breakLabel},
loop.statements.emplace_back(wasm::BreakIf{wasm::Label{breakLabel}, make_unique<wasm::Expression>(
wasm::BuiltinCall{"i64.eqz", make_vector<wasm::Expression>(
visitReturnByValue(*_for.condition)
)}

View File

@ -82,11 +82,6 @@ string EWasmToText::operator()(wasm::GlobalVariable const& _identifier)
return "(get_global $" + _identifier.name + ")";
}
string EWasmToText::operator()(wasm::Label const& _label)
{
return "$" + _label.name;
}
string EWasmToText::operator()(wasm::BuiltinCall const& _builtinCall)
{
string args = joinTransformed(_builtinCall.arguments);
@ -128,6 +123,11 @@ string EWasmToText::operator()(wasm::Break const& _break)
return "(break $" + _break.label.name + ")\n";
}
string EWasmToText::operator()(wasm::BreakIf const& _break)
{
return "(br_if $" + _break.label.name + " " + visit(*_break.condition) + ")\n";
}
string EWasmToText::operator()(wasm::Block const& _block)
{
string label = _block.labelName.empty() ? "" : " $" + _block.labelName;

View File

@ -42,7 +42,6 @@ public:
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);
std::string operator()(wasm::BuiltinCall const& _builinCall);
std::string operator()(wasm::FunctionCall const& _functionCall);
std::string operator()(wasm::LocalAssignment const& _assignment);
@ -50,6 +49,7 @@ public:
std::string operator()(wasm::If const& _if);
std::string operator()(wasm::Loop const& _loop);
std::string operator()(wasm::Break const& _break);
std::string operator()(wasm::BreakIf const& _break);
std::string operator()(wasm::Block const& _block);
private: