authorgravatar for 81774659+gracefuu@users.noreply.github.comgracefu <81774659+gracefuu@users.noreply.github.com> 2021-04-02 22:57:44+08:00
committergravatar for 81774659+gracefuu@users.noreply.github.comgracefu <81774659+gracefuu@users.noreply.github.com> 2021-04-05 14:37:04+08:00
log869fc06c57bca38f1af16f54453a9468603b22a0
treeaa1f60e3cc1110b1d9d1d7ed73c3f048029f7ccb
parentd1244d3608361d416e9a2b58b58b67985650165c
signaturelock-open Commit is signed but in an unrecognized format.

stage2 wasm: codegen `mul` op


2 files changed, 32 insertions(+), 0 deletions(-)

src/codegen/wasm.zig+20
......@@ -221,6 +221,7 @@ pub const Context = struct {
221221 .dbg_stmt => WValue.none,
222222 .load => self.genLoad(inst.castTag(.load).?),
223223 .loop => self.genLoop(inst.castTag(.loop).?),
224 .mul => self.genMul(inst.castTag(.mul).?),
224225 .not => self.genNot(inst.castTag(.not).?),
225226 .ret => self.genRet(inst.castTag(.ret).?),
226227 .retvoid => WValue.none,
......@@ -344,6 +345,25 @@ pub const Context = struct {
344345 return .none;
345346 }
346347
348 fn genMul(self: *Context, inst: *Inst.BinOp) InnerError!WValue {
349 const lhs = self.resolveInst(inst.lhs);
350 const rhs = self.resolveInst(inst.rhs);
351
352 try self.emitWValue(lhs);
353 try self.emitWValue(rhs);
354
355 const opcode: wasm.Opcode = switch (inst.base.ty.tag()) {
356 .u32, .i32 => .i32_mul,
357 .u64, .i64 => .i64_mul,
358 .f32 => .f32_mul,
359 .f64 => .f64_mul,
360 else => return self.fail(inst.base.src, "TODO - Implement wasm genMul for type '{s}'", .{inst.base.ty.tag()}),
361 };
362
363 try self.code.append(wasm.opcode(opcode));
364 return .none;
365 }
366
347367 fn emitConstant(self: *Context, inst: *Inst.Constant) InnerError!void {
348368 const writer = self.code.writer();
349369 switch (inst.base.ty.tag()) {
test/stage2/wasm.zig+12
......@@ -141,6 +141,18 @@ pub fn addCases(ctx: *TestContext) !void {
141141 \\ return y - x;
142142 \\}
143143 , "8\n");
144
145 case.addCompareOutput(
146 \\export fn _start() u32 {
147 \\ var i: u32 = 5;
148 \\ i *= 7;
149 \\ var result: u32 = foo(i, 10);
150 \\ return result;
151 \\}
152 \\fn foo(x: u32, y: u32) u32 {
153 \\ return x * y;
154 \\}
155 , "350\n");
144156 }
145157
146158 {