| ... | ... | @@ -1501,11 +1501,12 @@ fn airShlWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 1501 | 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 | 1505 | /// Requires use of .rax and .rdx registers. Spills them if necessary. |
| 1506 | 1506 | /// Quotient is saved in .rax and remainder in .rdx. |
| 1507 | | fn genIntDivOpMir( |
| 1507 | fn genIntMulDivOpMir( |
| 1508 | 1508 | self: *Self, |
| 1509 | tag: Mir.Inst.Tag, |
| 1509 | 1510 | ty: Type, |
| 1510 | 1511 | signedness: std.builtin.Signedness, |
| 1511 | 1512 | lhs: MCValue, |
| ... | ... | @@ -1513,7 +1514,7 @@ fn genIntDivOpMir( |
| 1513 | 1514 | ) !void { |
| 1514 | 1515 | const abi_size = @intCast(u32, ty.abiSize(self.target.*)); |
| 1515 | 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 | 1520 | try self.register_manager.getReg(.rax, null); |
| ... | ... | @@ -1521,17 +1522,7 @@ fn genIntDivOpMir( |
| 1521 | 1522 | self.register_manager.freezeRegs(&.{ .rax, .rdx }); |
| 1522 | 1523 | defer self.register_manager.unfreezeRegs(&.{ .rax, .rdx }); |
| 1523 | 1524 | |
| 1524 | | const dividend = switch (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}); |
| 1525 | try self.genSetReg(ty, .rax, lhs); |
| 1535 | 1526 | |
| 1536 | 1527 | switch (signedness) { |
| 1537 | 1528 | .signed => { |
| ... | ... | @@ -1555,22 +1546,19 @@ fn genIntDivOpMir( |
| 1555 | 1546 | }, |
| 1556 | 1547 | } |
| 1557 | 1548 | |
| 1558 | | const divisor = switch (rhs) { |
| 1549 | const factor = switch (rhs) { |
| 1559 | 1550 | .register => rhs, |
| 1551 | .stack_offset => rhs, |
| 1560 | 1552 | else => blk: { |
| 1561 | 1553 | const reg = try self.copyToTmpRegister(ty, rhs); |
| 1562 | 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 | 1559 | .register => |reg| { |
| 1572 | 1560 | _ = try self.addInst(.{ |
| 1573 | | .tag = op_tag, |
| 1561 | .tag = tag, |
| 1574 | 1562 | .ops = (Mir.Ops{ |
| 1575 | 1563 | .reg1 = reg, |
| 1576 | 1564 | }).encode(), |
| ... | ... | @@ -1579,7 +1567,7 @@ fn genIntDivOpMir( |
| 1579 | 1567 | }, |
| 1580 | 1568 | .stack_offset => |off| { |
| 1581 | 1569 | _ = try self.addInst(.{ |
| 1582 | | .tag = op_tag, |
| 1570 | .tag = tag, |
| 1583 | 1571 | .ops = (Mir.Ops{ |
| 1584 | 1572 | .reg2 = .rbp, |
| 1585 | 1573 | .flags = switch (abi_size) { |
| ... | ... | @@ -1612,7 +1600,10 @@ fn genInlineIntDivFloor(self: *Self, ty: Type, lhs: MCValue, rhs: MCValue) !MCVa |
| 1612 | 1600 | self.register_manager.freezeRegs(&.{divisor}); |
| 1613 | 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 | 1608 | _ = try self.addInst(.{ |
| 1618 | 1609 | .tag = .xor, |
| ... | ... | @@ -1673,13 +1664,16 @@ fn airDiv(self: *Self, inst: Air.Inst.Index) !void { |
| 1673 | 1664 | |
| 1674 | 1665 | const signedness = ty.intInfo(self.target.*).signedness; |
| 1675 | 1666 | if (signedness == .unsigned) { |
| 1676 | | try self.genIntDivOpMir(ty, signedness, lhs, rhs); |
| 1667 | try self.genIntMulDivOpMir(.div, ty, signedness, lhs, rhs); |
| 1677 | 1668 | break :result MCValue{ .register = .rax }; |
| 1678 | 1669 | } |
| 1679 | 1670 | |
| 1680 | 1671 | switch (tag) { |
| 1681 | 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 | 1677 | break :result MCValue{ .register = .rax }; |
| 1684 | 1678 | }, |
| 1685 | 1679 | .div_floor => { |
| ... | ... | @@ -1704,7 +1698,10 @@ fn airRem(self: *Self, inst: Air.Inst.Index) !void { |
| 1704 | 1698 | const lhs = try self.resolveInst(bin_op.lhs); |
| 1705 | 1699 | const rhs = try self.resolveInst(bin_op.rhs); |
| 1706 | 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 | 1705 | break :result MCValue{ .register = .rdx }; |
| 1709 | 1706 | }; |
| 1710 | 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 | 1722 | const signedness = ty.intInfo(self.target.*).signedness; |
| 1726 | 1723 | switch (signedness) { |
| 1727 | 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 | 1729 | break :result MCValue{ .register = .rdx }; |
| 1730 | 1730 | }, |
| 1731 | 1731 | .signed => { |