Commit 4c71942f84

gracefu <81774659+gracefuu@users.noreply.github.com>
2021-04-07 23:24:17
stage2: Add .div to ir.zig
1 parent bcc3716
Changed files (4)
src/codegen/c.zig
@@ -582,6 +582,9 @@ pub fn genBody(o: *Object, body: ir.Body) error{ AnalysisFail, OutOfMemory }!voi
             .mul => try genBinOp(o, inst.castTag(.sub).?, " * "),
             // TODO make this do wrapping multiplication for signed ints
             .mulwrap => try genBinOp(o, inst.castTag(.sub).?, " * "),
+            // TODO use a different strategy for div that communicates to the optimizer
+            // that wrapping is UB.
+            .div => try genBinOp(o, inst.castTag(.div).?, " / "),
 
             .constant => unreachable, // excluded from function bodies
             .alloc => try genAlloc(o, inst.castTag(.alloc).?),
src/codegen.zig
@@ -855,6 +855,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
                 .not => return self.genNot(inst.castTag(.not).?),
                 .mul => return self.genMul(inst.castTag(.mul).?),
                 .mulwrap => return self.genMulWrap(inst.castTag(.mulwrap).?),
+                .div => return self.genDiv(inst.castTag(.div).?),
                 .ptrtoint => return self.genPtrToInt(inst.castTag(.ptrtoint).?),
                 .ref => return self.genRef(inst.castTag(.ref).?),
                 .ret => return self.genRet(inst.castTag(.ret).?),
@@ -1092,6 +1093,15 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
             }
         }
 
+        fn genDiv(self: *Self, inst: *ir.Inst.BinOp) !MCValue {
+            // No side effects, so if it's unreferenced, do nothing.
+            if (inst.base.isUnused())
+                return MCValue.dead;
+            switch (arch) {
+                else => return self.fail(inst.base.src, "TODO implement div for {}", .{self.target.cpu.arch}),
+            }
+        }
+
         fn genBitAnd(self: *Self, inst: *ir.Inst.BinOp) !MCValue {
             // No side effects, so if it's unreferenced, do nothing.
             if (inst.base.isUnused())
src/ir.zig
@@ -115,6 +115,7 @@ pub const Inst = struct {
         unreach,
         mul,
         mulwrap,
+        div,
         not,
         floatcast,
         intcast,
@@ -181,6 +182,7 @@ pub const Inst = struct {
                 .subwrap,
                 .mul,
                 .mulwrap,
+                .div,
                 .cmp_lt,
                 .cmp_lte,
                 .cmp_eq,
@@ -752,6 +754,7 @@ const DumpTzir = struct {
                 .subwrap,
                 .mul,
                 .mulwrap,
+                .div,
                 .cmp_lt,
                 .cmp_lte,
                 .cmp_eq,
@@ -891,6 +894,7 @@ const DumpTzir = struct {
                 .subwrap,
                 .mul,
                 .mulwrap,
+                .div,
                 .cmp_lt,
                 .cmp_lte,
                 .cmp_eq,
src/Sema.zig
@@ -3540,6 +3540,7 @@ fn analyzeArithmetic(
         .subwrap => .subwrap,
         .mul => .mul,
         .mulwrap => .mulwrap,
+        .div => .div,
         else => return sema.mod.fail(&block.base, src, "TODO implement arithmetic for operand '{s}''", .{@tagName(zir_tag)}),
     };