Skip to content

Commit

Permalink
[Interpreter] i32.mul (#7268)
Browse files Browse the repository at this point in the history
Building on top of #7227, i32.mul is implemented and tested.
  • Loading branch information
ashleynh authored Feb 3, 2025
1 parent 659cdc1 commit 4d41559
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/interpreter/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,20 @@ struct ExpressionInterpreter : OverriddenVisitor<ExpressionInterpreter, Flow> {
Flow visitBinary(Binary* curr) {
auto rhs = pop();
auto lhs = pop();
// TODO: switch-case over all operations.
if (curr->op == AddInt32) {
push(lhs.add(rhs));
return {};
} else if (curr->op == SubInt32) {
push(lhs.sub(rhs));
return {};
// TODO: add support for all operations.
switch (curr->op) {
case AddInt32:
push(lhs.add(rhs));
return {};
case SubInt32:
push(lhs.sub(rhs));
return {};
case MulInt32:
push(lhs.mul(rhs));
return {};
default:
WASM_UNREACHABLE("TODO");
}
WASM_UNREACHABLE("TODO");
}
Flow visitSelect(Select* curr) { WASM_UNREACHABLE("TODO"); }
Flow visitDrop(Drop* curr) { WASM_UNREACHABLE("TODO"); }
Expand Down
17 changes: 17 additions & 0 deletions test/gtest/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,20 @@ TEST(InterpreterTest, SubI32) {

EXPECT_EQ(results, expected);
}

TEST(InterpreterTest, MulI32) {
Module wasm;
IRBuilder builder(wasm);

ASSERT_FALSE(builder.makeConst(Literal(uint32_t(1))).getErr());
ASSERT_FALSE(builder.makeConst(Literal(uint32_t(2))).getErr());
ASSERT_FALSE(builder.makeBinary(MulInt32).getErr());

auto expr = builder.build();
ASSERT_FALSE(expr.getErr());

auto results = Interpreter{}.run(*expr);
std::vector<Literal> expected{Literal(uint32_t(2))};

EXPECT_EQ(results, expected);
}

0 comments on commit 4d41559

Please sign in to comment.