diff --git a/src/Sema.zig b/src/Sema.zig index 0b5a21ce42..1a9c60a680 100644 --- a/src/Sema.zig +++ b/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)}), }; diff --git a/src/codegen.zig b/src/codegen.zig index fbd412ceba..3958577d95 100644 --- a/src/codegen.zig +++ b/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()) diff --git a/src/codegen/c.zig b/src/codegen/c.zig index 876f86ed02..d3ddbdeef9 100644 --- a/src/codegen/c.zig +++ b/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).?), diff --git a/src/ir.zig b/src/ir.zig index ab6fb29e03..85d4175c43 100644 --- a/src/ir.zig +++ b/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,