Skip to content

Commit 6fe5103

Browse files
authored
[Interpreter] i32.sub (WebAssembly#7259)
Building on top of WebAssembly#7227, i32.sub is implemented and tested.
1 parent f05cf1d commit 6fe5103

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/interpreter/interpreter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ struct ExpressionInterpreter : OverriddenVisitor<ExpressionInterpreter, Flow> {
9494
if (curr->op == AddInt32) {
9595
push(lhs.add(rhs));
9696
return {};
97+
} else if (curr->op == SubInt32) {
98+
push(lhs.sub(rhs));
99+
return {};
97100
}
98101
WASM_UNREACHABLE("TODO");
99102
}

test/gtest/interpreter.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
using namespace wasm;
2727

28-
TEST(InterpreterTest, Add) {
28+
TEST(InterpreterTest, AddI32) {
2929
Module wasm;
3030
IRBuilder builder(wasm);
3131

@@ -41,3 +41,20 @@ TEST(InterpreterTest, Add) {
4141

4242
EXPECT_EQ(results, expected);
4343
}
44+
45+
TEST(InterpreterTest, SubI32) {
46+
Module wasm;
47+
IRBuilder builder(wasm);
48+
49+
ASSERT_FALSE(builder.makeConst(Literal(uint32_t(1))).getErr());
50+
ASSERT_FALSE(builder.makeConst(Literal(uint32_t(2))).getErr());
51+
ASSERT_FALSE(builder.makeBinary(SubInt32).getErr());
52+
53+
auto expr = builder.build();
54+
ASSERT_FALSE(expr.getErr());
55+
56+
auto results = Interpreter{}.run(*expr);
57+
std::vector<Literal> expected{Literal(uint32_t(-1))};
58+
59+
EXPECT_EQ(results, expected);
60+
}

0 commit comments

Comments
 (0)