| ... | @@ -1501,11 +1501,12 @@ fn airShlWithOverflow(self: *Self, inst: Air.Inst.Index) !void { | ... | @@ -1501,11 +1501,12 @@ fn airShlWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 1501 | return self.fail("TODO implement airShlWithOverflow for {}", .{self.target.cpu.arch}); | 1501 | return self.fail("TODO implement airShlWithOverflow for {}", .{self.target.cpu.arch}); |
| 1502 | } | 1502 | } |
| 1503 | | 1503 | |
| 1504 | /// Generates signed or unsigned integer division. | 1504 | /// Generates signed or unsigned integer multiplication/division. |
| 1505 | /// Requires use of .rax and .rdx registers. Spills them if necessary. | 1505 | /// Requires use of .rax and .rdx registers. Spills them if necessary. |
| 1506 | /// Quotient is saved in .rax and remainder in .rdx. | 1506 | /// Quotient is saved in .rax and remainder in .rdx. |
| 1507 | fn genIntDivOpMir( | 1507 | fn genIntMulDivOpMir( |
| 1508 | self: *Self, | 1508 | self: *Self, |
| | 1509 | tag: Mir.Inst.Tag, |
| 1509 | ty: Type, | 1510 | ty: Type, |
| 1510 | signedness: std.builtin.Signedness, | 1511 | signedness: std.builtin.Signedness, |
| 1511 | lhs: MCValue, | 1512 | lhs: MCValue, |
| ... | @@ -1513,7 +1514,7 @@ fn genIntDivOpMir( | ... | @@ -1513,7 +1514,7 @@ fn genIntDivOpMir( |
| 1513 | ) !void { | 1514 | ) !void { |
| 1514 | const abi_size = @intCast(u32, ty.abiSize(self.target.*)); | 1515 | const abi_size = @intCast(u32, ty.abiSize(self.target.*)); |
| 1515 | if (abi_size > 8) { | 1516 | if (abi_size > 8) { |
| 1516 | return self.fail("TODO implement genIntDivOpMir for ABI size larger than 8", .{}); | 1517 | return self.fail("TODO implement genIntMulDivOpMir for ABI size larger than 8", .{}); |
| 1517 | } | 1518 | } |
| 1518 | | 1519 | |
| 1519 | try self.register_manager.getReg(.rax, null); | 1520 | try self.register_manager.getReg(.rax, null); |
| ... | @@ -1521,17 +1522,7 @@ fn genIntDivOpMir( | ... | @@ -1521,17 +1522,7 @@ fn genIntDivOpMir( |
| 1521 | self.register_manager.freezeRegs(&.{ .rax, .rdx }); | 1522 | self.register_manager.freezeRegs(&.{ .rax, .rdx }); |
| 1522 | defer self.register_manager.unfreezeRegs(&.{ .rax, .rdx }); | 1523 | defer self.register_manager.unfreezeRegs(&.{ .rax, .rdx }); |
| 1523 | | 1524 | |
| 1524 | const dividend = switch (lhs) { | 1525 | try self.genSetReg(ty, .rax, lhs); |
| 1525 | .register => lhs, | | |
| 1526 | else => blk: { | | |
| 1527 | const reg = try self.copyToTmpRegister(ty, lhs); | | |
| 1528 | break :blk MCValue{ .register = reg }; | | |
| 1529 | }, | | |
| 1530 | }; | | |
| 1531 | try self.genSetReg(ty, .rax, dividend); | | |
| 1532 | | | |
| 1533 | self.register_manager.freezeRegs(&.{dividend.register}); | | |
| 1534 | defer self.register_manager.unfreezeRegs(&.{dividend.register}); | | |
| 1535 | | 1526 | |
| 1536 | switch (signedness) { | 1527 | switch (signedness) { |
| 1537 | .signed => { | 1528 | .signed => { |
| ... | @@ -1555,22 +1546,19 @@ fn genIntDivOpMir( | ... | @@ -1555,22 +1546,19 @@ fn genIntDivOpMir( |
| 1555 | }, | 1546 | }, |
| 1556 | } | 1547 | } |
| 1557 | | 1548 | |
| 1558 | const divisor = switch (rhs) { | 1549 | const factor = switch (rhs) { |
| 1559 | .register => rhs, | 1550 | .register => rhs, |
| | 1551 | .stack_offset => rhs, |
| 1560 | else => blk: { | 1552 | else => blk: { |
| 1561 | const reg = try self.copyToTmpRegister(ty, rhs); | 1553 | const reg = try self.copyToTmpRegister(ty, rhs); |
| 1562 | break :blk MCValue{ .register = reg }; | 1554 | break :blk MCValue{ .register = reg }; |
| 1563 | }, | 1555 | }, |
| 1564 | }; | 1556 | }; |
| 1565 | const op_tag: Mir.Inst.Tag = switch (signedness) { | | |
| 1566 | .signed => .idiv, | | |
| 1567 | .unsigned => .div, | | |
| 1568 | }; | | |
| 1569 | | 1557 | |
| 1570 | switch (divisor) { | 1558 | switch (factor) { |
| 1571 | .register => |reg| { | 1559 | .register => |reg| { |
| 1572 | _ = try self.addInst(.{ | 1560 | _ = try self.addInst(.{ |
| 1573 | .tag = op_tag, | 1561 | .tag = tag, |
| 1574 | .ops = (Mir.Ops{ | 1562 | .ops = (Mir.Ops{ |
| 1575 | .reg1 = reg, | 1563 | .reg1 = reg, |
| 1576 | }).encode(), | 1564 | }).encode(), |
| ... | @@ -1579,7 +1567,7 @@ fn genIntDivOpMir( | ... | @@ -1579,7 +1567,7 @@ fn genIntDivOpMir( |
| 1579 | }, | 1567 | }, |
| 1580 | .stack_offset => |off| { | 1568 | .stack_offset => |off| { |
| 1581 | _ = try self.addInst(.{ | 1569 | _ = try self.addInst(.{ |
| 1582 | .tag = op_tag, | 1570 | .tag = tag, |
| 1583 | .ops = (Mir.Ops{ | 1571 | .ops = (Mir.Ops{ |
| 1584 | .reg2 = .rbp, | 1572 | .reg2 = .rbp, |
| 1585 | .flags = switch (abi_size) { | 1573 | .flags = switch (abi_size) { |
| ... | @@ -1612,7 +1600,10 @@ fn genInlineIntDivFloor(self: *Self, ty: Type, lhs: MCValue, rhs: MCValue) !MCVa | ... | @@ -1612,7 +1600,10 @@ fn genInlineIntDivFloor(self: *Self, ty: Type, lhs: MCValue, rhs: MCValue) !MCVa |
| 1612 | self.register_manager.freezeRegs(&.{divisor}); | 1600 | self.register_manager.freezeRegs(&.{divisor}); |
| 1613 | defer self.register_manager.unfreezeRegs(&.{ dividend, divisor }); | 1601 | defer self.register_manager.unfreezeRegs(&.{ dividend, divisor }); |
| 1614 | | 1602 | |
| 1615 | try self.genIntDivOpMir(Type.isize, signedness, .{ .register = dividend }, .{ .register = divisor }); | 1603 | try self.genIntMulDivOpMir(switch (signedness) { |
| | 1604 | .signed => .idiv, |
| | 1605 | .unsigned => .div, |
| | 1606 | }, Type.isize, signedness, .{ .register = dividend }, .{ .register = divisor }); |
| 1616 | | 1607 | |
| 1617 | _ = try self.addInst(.{ | 1608 | _ = try self.addInst(.{ |
| 1618 | .tag = .xor, | 1609 | .tag = .xor, |
| ... | @@ -1673,13 +1664,16 @@ fn airDiv(self: *Self, inst: Air.Inst.Index) !void { | ... | @@ -1673,13 +1664,16 @@ fn airDiv(self: *Self, inst: Air.Inst.Index) !void { |
| 1673 | | 1664 | |
| 1674 | const signedness = ty.intInfo(self.target.*).signedness; | 1665 | const signedness = ty.intInfo(self.target.*).signedness; |
| 1675 | if (signedness == .unsigned) { | 1666 | if (signedness == .unsigned) { |
| 1676 | try self.genIntDivOpMir(ty, signedness, lhs, rhs); | 1667 | try self.genIntMulDivOpMir(.div, ty, signedness, lhs, rhs); |
| 1677 | break :result MCValue{ .register = .rax }; | 1668 | break :result MCValue{ .register = .rax }; |
| 1678 | } | 1669 | } |
| 1679 | | 1670 | |
| 1680 | switch (tag) { | 1671 | switch (tag) { |
| 1681 | .div_exact, .div_trunc => { | 1672 | .div_exact, .div_trunc => { |
| 1682 | try self.genIntDivOpMir(ty, signedness, lhs, rhs); | 1673 | try self.genIntMulDivOpMir(switch (signedness) { |
| | 1674 | .signed => .idiv, |
| | 1675 | .unsigned => .div, |
| | 1676 | }, ty, signedness, lhs, rhs); |
| 1683 | break :result MCValue{ .register = .rax }; | 1677 | break :result MCValue{ .register = .rax }; |
| 1684 | }, | 1678 | }, |
| 1685 | .div_floor => { | 1679 | .div_floor => { |
| ... | @@ -1704,7 +1698,10 @@ fn airRem(self: *Self, inst: Air.Inst.Index) !void { | ... | @@ -1704,7 +1698,10 @@ fn airRem(self: *Self, inst: Air.Inst.Index) !void { |
| 1704 | const lhs = try self.resolveInst(bin_op.lhs); | 1698 | const lhs = try self.resolveInst(bin_op.lhs); |
| 1705 | const rhs = try self.resolveInst(bin_op.rhs); | 1699 | const rhs = try self.resolveInst(bin_op.rhs); |
| 1706 | const signedness = ty.intInfo(self.target.*).signedness; | 1700 | const signedness = ty.intInfo(self.target.*).signedness; |
| 1707 | try self.genIntDivOpMir(ty, signedness, lhs, rhs); | 1701 | try self.genIntMulDivOpMir(switch (signedness) { |
| | 1702 | .signed => .idiv, |
| | 1703 | .unsigned => .div, |
| | 1704 | }, ty, signedness, lhs, rhs); |
| 1708 | break :result MCValue{ .register = .rdx }; | 1705 | break :result MCValue{ .register = .rdx }; |
| 1709 | }; | 1706 | }; |
| 1710 | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); | 1707 | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| ... | @@ -1725,7 +1722,10 @@ fn airMod(self: *Self, inst: Air.Inst.Index) !void { | ... | @@ -1725,7 +1722,10 @@ fn airMod(self: *Self, inst: Air.Inst.Index) !void { |
| 1725 | const signedness = ty.intInfo(self.target.*).signedness; | 1722 | const signedness = ty.intInfo(self.target.*).signedness; |
| 1726 | switch (signedness) { | 1723 | switch (signedness) { |
| 1727 | .unsigned => { | 1724 | .unsigned => { |
| 1728 | try self.genIntDivOpMir(ty, signedness, lhs, rhs); | 1725 | try self.genIntMulDivOpMir(switch (signedness) { |
| | 1726 | .signed => .idiv, |
| | 1727 | .unsigned => .div, |
| | 1728 | }, ty, signedness, lhs, rhs); |
| 1729 | break :result MCValue{ .register = .rdx }; | 1729 | break :result MCValue{ .register = .rdx }; |
| 1730 | }, | 1730 | }, |
| 1731 | .signed => { | 1731 | .signed => { |