From 4b854b75d2d0add085ac202c611bbe10fa79b51c Mon Sep 17 00:00:00 2001 From: Luuk de Gram Date: Sat, 6 Feb 2021 13:10:30 +0100 Subject: [PATCH] Fix getNot and add test cases --- src/codegen/wasm.zig | 4 ++-- test/stage2/wasm.zig | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/codegen/wasm.zig b/src/codegen/wasm.zig index 0e40ebc312..d5f68eca81 100644 --- a/src/codegen/wasm.zig +++ b/src/codegen/wasm.zig @@ -207,8 +207,8 @@ pub const Context = struct { .add => self.genAdd(inst.castTag(.add).?), .alloc => self.genAlloc(inst.castTag(.alloc).?), .arg => self.genArg(inst.castTag(.arg).?), - .breakpoint => self.genBreakpoint(inst.castTag(.breakpoint).?), .block => self.genBlock(inst.castTag(.block).?), + .breakpoint => self.genBreakpoint(inst.castTag(.breakpoint).?), .br => self.genBr(inst.castTag(.br).?), .call => self.genCall(inst.castTag(.call).?), .cmp_eq => self.genCmp(inst.castTag(.cmp_eq).?, .eq), @@ -546,7 +546,7 @@ pub const Context = struct { try writer.writeByte(wasm.opcode(.i32_const)); try leb.writeILEB128(writer, @as(i32, 0)); - try self.code.append(wasm.opcode(.i32_ne)); + try writer.writeByte(wasm.opcode(.i32_eq)); return WValue{ .code_offset = offset }; } diff --git a/test/stage2/wasm.zig b/test/stage2/wasm.zig index 06ede2d735..51df6bccf0 100644 --- a/test/stage2/wasm.zig +++ b/test/stage2/wasm.zig @@ -175,6 +175,41 @@ pub fn addCases(ctx: *TestContext) !void { \\ return i; \\} , "31\n"); + + case.addCompareOutput( + \\export fn _start() void { + \\ assert(foo(true) != @as(i32, 30)); + \\} + \\ + \\fn assert(ok: bool) void { + \\ if (!ok) unreachable; + \\} + \\ + \\fn foo(ok: bool) i32 { + \\ const x = if(ok) @as(i32, 20) else @as(i32, 10); + \\ return x; + \\} + , ""); + + case.addCompareOutput( + \\export fn _start() void { + \\ assert(foo(false) == @as(i32, 20)); + \\ assert(foo(true) == @as(i32, 30)); + \\} + \\ + \\fn assert(ok: bool) void { + \\ if (!ok) unreachable; + \\} + \\ + \\fn foo(ok: bool) i32 { + \\ const val: i32 = blk: { + \\ var x: i32 = 1; + \\ if (!ok) break :blk x + @as(i32, 9); + \\ break :blk x + @as(i32, 19); + \\ }; + \\ return val + 10; + \\} + , ""); } {