From 4ed2c52fb7b3b6d541235b6e9096a6e72b99ca2a Mon Sep 17 00:00:00 2001 From: Vexu Date: Fri, 30 Oct 2020 15:47:12 +0200 Subject: [PATCH] stage2: switch put swap condbr and block condbr is noreturn so having the other way around caused subsequent cases to be eliminated as dead --- src/astgen.zig | 22 +++++++++------------- src/codegen.zig | 11 ++++++++++- src/main.zig | 1 + 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/astgen.zig b/src/astgen.zig index f098e2b8c0..37b1eab9a6 100644 --- a/src/astgen.zig +++ b/src/astgen.zig @@ -1733,32 +1733,28 @@ fn switchExpr(mod: *Module, scope: *Scope, rl: ResultLoc, switch_node: *ast.Node } } - const condbr = try addZIRInstSpecial(mod, &else_scope.base, case_src, zir.Inst.CondBr, .{ + const condbr = try addZIRInstSpecial(mod, &case_scope.base, case_src, zir.Inst.CondBr, .{ .condition = any_ok.?, .then_body = undefined, // populated below .else_body = undefined, // populated below }, .{}); + const cond_block = try addZIRInstBlock(mod, &else_scope.base, case_src, .block, .{ + .instructions = try scope.arena().dupe(*zir.Inst, case_scope.instructions.items), + }); + // reset cond_scope for then_body + case_scope.instructions.items.len = 0; try switchCaseExpr(mod, &case_scope.base, case_rl, block, case); condbr.positionals.then_body = .{ .instructions = try scope.arena().dupe(*zir.Inst, case_scope.instructions.items), }; - // reset to add the empty block - case_scope.instructions.items.len = 0; - const empty_block = try addZIRInstBlock(mod, &case_scope.base, case_src, .block, .{ - .instructions = undefined, // populated below - }); - condbr.positionals.else_body = .{ - .instructions = try scope.arena().dupe(*zir.Inst, case_scope.instructions.items), - }; - - // reset to add a break to the empty block + // reset cond_scope for else_body case_scope.instructions.items.len = 0; _ = try addZIRInst(mod, &case_scope.base, case_src, zir.Inst.BreakVoid, .{ - .block = empty_block, + .block = cond_block, }, .{}); - empty_block.positionals.body = .{ + condbr.positionals.else_body = .{ .instructions = try scope.arena().dupe(*zir.Inst, case_scope.instructions.items), }; } diff --git a/src/codegen.zig b/src/codegen.zig index 1745f86a94..cbc038ca65 100644 --- a/src/codegen.zig +++ b/src/codegen.zig @@ -2033,8 +2033,17 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type { } fn genBoolOp(self: *Self, inst: *ir.Inst.BinOp) !MCValue { + if (inst.base.isUnused()) + return MCValue.dead; switch (arch) { - else => return self.fail(inst.base.src, "TODO genBoolOp for {}", .{self.target.cpu.arch}), + .x86_64 => if (inst.base.tag == .booland) { + // lhs AND rhs + return try self.genX8664BinMath(&inst.base, inst.lhs, inst.rhs, 4, 0x20); + } else { + // lhs OR rhs + return try self.genX8664BinMath(&inst.base, inst.lhs, inst.rhs, 1, 0x08); + }, + else => return self.fail(inst.base.src, "TODO implement sub for {}", .{self.target.cpu.arch}), } } diff --git a/src/main.zig b/src/main.zig index 8162bc46b6..45fcb5ce61 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2421,6 +2421,7 @@ pub fn cmdFmt(gpa: *Allocator, args: []const []const u8) !void { var stdin_flag: bool = false; var check_flag: bool = false; var input_files = ArrayList([]const u8).init(gpa); + defer input_files.deinit(); { var i: usize = 0;