stage2 wasm: codegen mul op

This commit is contained in:
gracefu 2021-04-02 22:57:44 +08:00
parent d1244d3608
commit 869fc06c57
No known key found for this signature in database
GPG Key ID: 2B0D39CC4E035325
2 changed files with 32 additions and 0 deletions

View File

@ -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()) {

View File

@ -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");
}
{