Commit 4ed2c52fb7

Vexu <git@vexu.eu>
2020-10-30 14:47:12
stage2: switch put swap condbr and block
condbr is noreturn so having the other way around caused subsequent cases to be eliminated as dead
1 parent e2e0b62
Changed files (3)
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
         }, .{});
-
-        try switchCaseExpr(mod, &case_scope.base, case_rl, block, case);
-        condbr.positionals.then_body = .{
+        const cond_block = try addZIRInstBlock(mod, &else_scope.base, case_src, .block, .{
             .instructions = try scope.arena().dupe(*zir.Inst, case_scope.instructions.items),
-        };
+        });
 
-        // reset to add the empty block
+        // reset cond_scope for then_body
         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 = .{
+        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 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),
         };
     }
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}),
             }
         }
 
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;