stage2 wasm: codegen sub op

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

View File

@ -225,6 +225,7 @@ pub const Context = struct {
.ret => self.genRet(inst.castTag(.ret).?),
.retvoid => WValue.none,
.store => self.genStore(inst.castTag(.store).?),
.sub => self.genSub(inst.castTag(.sub).?),
.unreach => self.genUnreachable(inst.castTag(.unreach).?),
else => self.fail(inst.src, "TODO: Implement wasm inst: {s}", .{inst.tag}),
};
@ -324,6 +325,25 @@ pub const Context = struct {
return .none;
}
fn genSub(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_sub,
.u64, .i64 => .i64_sub,
.f32 => .f32_sub,
.f64 => .f64_sub,
else => return self.fail(inst.base.src, "TODO - Implement wasm genSub 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

@ -121,6 +121,26 @@ pub fn addCases(ctx: *TestContext) !void {
\\ return x + y;
\\}
, "35\n");
case.addCompareOutput(
\\export fn _start() u32 {
\\ var i: u32 = 20;
\\ i -= 5;
\\ return i;
\\}
, "15\n");
case.addCompareOutput(
\\export fn _start() u32 {
\\ var i: u32 = 5;
\\ i -= 3;
\\ var result: u32 = foo(i, 10);
\\ return result;
\\}
\\fn foo(x: u32, y: u32) u32 {
\\ return y - x;
\\}
, "8\n");
}
{