Skip to content

Commit 4d41559

Browse files
authored
[Interpreter] i32.mul (#7268)
Building on top of #7227, i32.mul is implemented and tested.
1 parent 659cdc1 commit 4d41559

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

src/interpreter/interpreter.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,20 @@ struct ExpressionInterpreter : OverriddenVisitor<ExpressionInterpreter, Flow> {
9090
Flow visitBinary(Binary* curr) {
9191
auto rhs = pop();
9292
auto lhs = pop();
93-
// TODO: switch-case over all operations.
94-
if (curr->op == AddInt32) {
95-
push(lhs.add(rhs));
96-
return {};
97-
} else if (curr->op == SubInt32) {
98-
push(lhs.sub(rhs));
99-
return {};
93+
// TODO: add support for all operations.
94+
switch (curr->op) {
95+
case AddInt32:
96+
push(lhs.add(rhs));
97+
return {};
98+
case SubInt32:
99+
push(lhs.sub(rhs));
100+
return {};
101+
case MulInt32:
102+
push(lhs.mul(rhs));
103+
return {};
104+
default:
105+
WASM_UNREACHABLE("TODO");
100106
}
101-
WASM_UNREACHABLE("TODO");
102107
}
103108
Flow visitSelect(Select* curr) { WASM_UNREACHABLE("TODO"); }
104109
Flow visitDrop(Drop* curr) { WASM_UNREACHABLE("TODO"); }

test/gtest/interpreter.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,20 @@ TEST(InterpreterTest, SubI32) {
5858

5959
EXPECT_EQ(results, expected);
6060
}
61+
62+
TEST(InterpreterTest, MulI32) {
63+
Module wasm;
64+
IRBuilder builder(wasm);
65+
66+
ASSERT_FALSE(builder.makeConst(Literal(uint32_t(1))).getErr());
67+
ASSERT_FALSE(builder.makeConst(Literal(uint32_t(2))).getErr());
68+
ASSERT_FALSE(builder.makeBinary(MulInt32).getErr());
69+
70+
auto expr = builder.build();
71+
ASSERT_FALSE(expr.getErr());
72+
73+
auto results = Interpreter{}.run(*expr);
74+
std::vector<Literal> expected{Literal(uint32_t(2))};
75+
76+
EXPECT_EQ(results, expected);
77+
}

0 commit comments

Comments
 (0)