Commit 869fc06c57

gracefu <81774659+gracefuu@users.noreply.github.com>
2021-04-02 16:57:44
stage2 wasm: codegen `mul` op
1 parent d1244d3
Changed files (2)
src
codegen
test
stage2
src/codegen/wasm.zig
@@ -221,6 +221,7 @@ pub const Context = struct {
             .dbg_stmt => WValue.none,
             .load => self.genLoad(inst.castTag(.load).?),
             .loop => self.genLoop(inst.castTag(.loop).?),
+            .mul => self.genMul(inst.castTag(.mul).?),
             .not => self.genNot(inst.castTag(.not).?),
             .ret => self.genRet(inst.castTag(.ret).?),
             .retvoid => WValue.none,
@@ -344,6 +345,25 @@ pub const Context = struct {
         return .none;
     }
 
+    fn genMul(self: *Context, inst: *Inst.BinOp) InnerError!WValue {
+        const lhs = self.resolveInst(inst.lhs);
+        const rhs = self.resolveInst(inst.rhs);
+
+        try self.emitWValue(lhs);
+        try self.emitWValue(rhs);
+
+        const opcode: wasm.Opcode = switch (inst.base.ty.tag()) {
+            .u32, .i32 => .i32_mul,
+            .u64, .i64 => .i64_mul,
+            .f32 => .f32_mul,
+            .f64 => .f64_mul,
+            else => return self.fail(inst.base.src, "TODO - Implement wasm genMul for type '{s}'", .{inst.base.ty.tag()}),
+        };
+
+        try self.code.append(wasm.opcode(opcode));
+        return .none;
+    }
+
     fn emitConstant(self: *Context, inst: *Inst.Constant) InnerError!void {
         const writer = self.code.writer();
         switch (inst.base.ty.tag()) {
test/stage2/wasm.zig
@@ -141,6 +141,18 @@ pub fn addCases(ctx: *TestContext) !void {
             \\    return y - x;
             \\}
         , "8\n");
+
+        case.addCompareOutput(
+            \\export fn _start() u32 {
+            \\    var i: u32 = 5;
+            \\    i *= 7;
+            \\    var result: u32 = foo(i, 10);
+            \\    return result;
+            \\}
+            \\fn foo(x: u32, y: u32) u32 {
+            \\    return x * y;
+            \\}
+        , "350\n");
     }
 
     {