| ... | ... | @@ -1681,10 +1681,16 @@ const DumpTzir = struct { |
| 1681 | 1681 | const loop = inst.castTag(.loop).?; |
| 1682 | 1682 | try dtz.fetchInstsAndResolveConsts(loop.body); |
| 1683 | 1683 | }, |
| 1684 | .call => { |
| 1685 | const call = inst.castTag(.call).?; |
| 1686 | try dtz.findConst(call.func); |
| 1687 | for (call.args) |arg| { |
| 1688 | try dtz.findConst(arg); |
| 1689 | } |
| 1690 | }, |
| 1684 | 1691 | |
| 1685 | 1692 | // TODO fill out this debug printing |
| 1686 | 1693 | .assembly, |
| 1687 | | .call, |
| 1688 | 1694 | .constant, |
| 1689 | 1695 | .varptr, |
| 1690 | 1696 | .switchbr, |
| ... | ... | @@ -1730,16 +1736,11 @@ const DumpTzir = struct { |
| 1730 | 1736 | .wrap_optional, |
| 1731 | 1737 | => { |
| 1732 | 1738 | const un_op = inst.cast(ir.Inst.UnOp).?; |
| 1733 | | if (dtz.partial_inst_table.get(un_op.operand)) |operand_index| { |
| 1734 | | try writer.print("%{d})\n", .{operand_index}); |
| 1735 | | } else if (dtz.const_table.get(un_op.operand)) |operand_index| { |
| 1736 | | try writer.print("@{d})\n", .{operand_index}); |
| 1737 | | } else if (dtz.inst_table.get(un_op.operand)) |operand_index| { |
| 1738 | | try writer.print("%{d}) // Instruction does not dominate all uses!\n", .{ |
| 1739 | | operand_index, |
| 1740 | | }); |
| 1739 | const kinky = try dtz.writeInst(writer, un_op.operand); |
| 1740 | if (kinky != null) { |
| 1741 | try writer.writeAll(") // Instruction does not dominate all uses!\n"); |
| 1741 | 1742 | } else { |
| 1742 | | try writer.writeAll("!BADREF!)\n"); |
| 1743 | try writer.writeAll(")\n"); |
| 1743 | 1744 | } |
| 1744 | 1745 | }, |
| 1745 | 1746 | |
| ... | ... | @@ -1758,30 +1759,12 @@ const DumpTzir = struct { |
| 1758 | 1759 | .bitor, |
| 1759 | 1760 | .xor, |
| 1760 | 1761 | => { |
| 1761 | | var lhs_kinky: ?usize = null; |
| 1762 | | var rhs_kinky: ?usize = null; |
| 1763 | | |
| 1764 | 1762 | const bin_op = inst.cast(ir.Inst.BinOp).?; |
| 1765 | | if (dtz.partial_inst_table.get(bin_op.lhs)) |operand_index| { |
| 1766 | | try writer.print("%{d}, ", .{operand_index}); |
| 1767 | | } else if (dtz.const_table.get(bin_op.lhs)) |operand_index| { |
| 1768 | | try writer.print("@{d}, ", .{operand_index}); |
| 1769 | | } else if (dtz.inst_table.get(bin_op.lhs)) |operand_index| { |
| 1770 | | lhs_kinky = operand_index; |
| 1771 | | try writer.print("%{d}, ", .{operand_index}); |
| 1772 | | } else { |
| 1773 | | try writer.writeAll("!BADREF!, "); |
| 1774 | | } |
| 1775 | | if (dtz.partial_inst_table.get(bin_op.rhs)) |operand_index| { |
| 1776 | | try writer.print("%{d}", .{operand_index}); |
| 1777 | | } else if (dtz.const_table.get(bin_op.rhs)) |operand_index| { |
| 1778 | | try writer.print("@{d}", .{operand_index}); |
| 1779 | | } else if (dtz.inst_table.get(bin_op.rhs)) |operand_index| { |
| 1780 | | rhs_kinky = operand_index; |
| 1781 | | try writer.print("%{d}", .{operand_index}); |
| 1782 | | } else { |
| 1783 | | try writer.writeAll("!BADREF!"); |
| 1784 | | } |
| 1763 | |
| 1764 | const lhs_kinky = try dtz.writeInst(writer, bin_op.lhs); |
| 1765 | try writer.writeAll(", "); |
| 1766 | const rhs_kinky = try dtz.writeInst(writer, bin_op.rhs); |
| 1767 | |
| 1785 | 1768 | if (lhs_kinky != null or rhs_kinky != null) { |
| 1786 | 1769 | try writer.writeAll(") // Instruction does not dominate all uses!"); |
| 1787 | 1770 | if (lhs_kinky) |lhs| { |
| ... | ... | @@ -1804,30 +1787,9 @@ const DumpTzir = struct { |
| 1804 | 1787 | .br => { |
| 1805 | 1788 | const br = inst.castTag(.br).?; |
| 1806 | 1789 | |
| 1807 | | var lhs_kinky: ?usize = null; |
| 1808 | | var rhs_kinky: ?usize = null; |
| 1809 | | |
| 1810 | | if (dtz.partial_inst_table.get(&br.block.base)) |operand_index| { |
| 1811 | | try writer.print("%{d}, ", .{operand_index}); |
| 1812 | | } else if (dtz.const_table.get(&br.block.base)) |operand_index| { |
| 1813 | | try writer.print("@{d}, ", .{operand_index}); |
| 1814 | | } else if (dtz.inst_table.get(&br.block.base)) |operand_index| { |
| 1815 | | lhs_kinky = operand_index; |
| 1816 | | try writer.print("%{d}, ", .{operand_index}); |
| 1817 | | } else { |
| 1818 | | try writer.writeAll("!BADREF!, "); |
| 1819 | | } |
| 1820 | | |
| 1821 | | if (dtz.partial_inst_table.get(br.operand)) |operand_index| { |
| 1822 | | try writer.print("%{d}", .{operand_index}); |
| 1823 | | } else if (dtz.const_table.get(br.operand)) |operand_index| { |
| 1824 | | try writer.print("@{d}", .{operand_index}); |
| 1825 | | } else if (dtz.inst_table.get(br.operand)) |operand_index| { |
| 1826 | | rhs_kinky = operand_index; |
| 1827 | | try writer.print("%{d}", .{operand_index}); |
| 1828 | | } else { |
| 1829 | | try writer.writeAll("!BADREF!"); |
| 1830 | | } |
| 1790 | const lhs_kinky = try dtz.writeInst(writer, &br.block.base); |
| 1791 | try writer.writeAll(", "); |
| 1792 | const rhs_kinky = try dtz.writeInst(writer, br.operand); |
| 1831 | 1793 | |
| 1832 | 1794 | if (lhs_kinky != null or rhs_kinky != null) { |
| 1833 | 1795 | try writer.writeAll(") // Instruction does not dominate all uses!"); |
| ... | ... | @@ -1845,16 +1807,11 @@ const DumpTzir = struct { |
| 1845 | 1807 | |
| 1846 | 1808 | .brvoid => { |
| 1847 | 1809 | const brvoid = inst.castTag(.brvoid).?; |
| 1848 | | if (dtz.partial_inst_table.get(&brvoid.block.base)) |operand_index| { |
| 1849 | | try writer.print("%{d})\n", .{operand_index}); |
| 1850 | | } else if (dtz.const_table.get(&brvoid.block.base)) |operand_index| { |
| 1851 | | try writer.print("@{d})\n", .{operand_index}); |
| 1852 | | } else if (dtz.inst_table.get(&brvoid.block.base)) |operand_index| { |
| 1853 | | try writer.print("%{d}) // Instruction does not dominate all uses!\n", .{ |
| 1854 | | operand_index, |
| 1855 | | }); |
| 1810 | const kinky = try dtz.writeInst(writer, &brvoid.block.base); |
| 1811 | if (kinky) |_| { |
| 1812 | try writer.writeAll(") // Instruction does not dominate all uses!\n"); |
| 1856 | 1813 | } else { |
| 1857 | | try writer.writeAll("!BADREF!)\n"); |
| 1814 | try writer.writeAll(")\n"); |
| 1858 | 1815 | } |
| 1859 | 1816 | }, |
| 1860 | 1817 | |
| ... | ... | @@ -1875,32 +1832,25 @@ const DumpTzir = struct { |
| 1875 | 1832 | .condbr => { |
| 1876 | 1833 | const condbr = inst.castTag(.condbr).?; |
| 1877 | 1834 | |
| 1878 | | if (dtz.partial_inst_table.get(condbr.condition)) |operand_index| { |
| 1879 | | try writer.print("%{d},", .{operand_index}); |
| 1880 | | } else if (dtz.const_table.get(condbr.condition)) |operand_index| { |
| 1881 | | try writer.print("@{d},", .{operand_index}); |
| 1882 | | } else if (dtz.inst_table.get(condbr.condition)) |operand_index| { |
| 1883 | | try writer.print("%{d}, // Instruction does not dominate all uses!", .{operand_index}); |
| 1835 | const condition_kinky = try dtz.writeInst(writer, condbr.condition); |
| 1836 | if (condition_kinky != null) { |
| 1837 | try writer.writeAll(", { // Instruction does not dominate all uses!\n"); |
| 1884 | 1838 | } else { |
| 1885 | | try writer.writeAll("!BADREF!,"); |
| 1839 | try writer.writeAll(", {\n"); |
| 1886 | 1840 | } |
| 1887 | | try writer.writeAll("\n"); |
| 1888 | | |
| 1889 | | try writer.writeByteNTimes(' ', dtz.indent); |
| 1890 | | try writer.writeAll("then:\n"); |
| 1891 | 1841 | |
| 1892 | 1842 | const old_indent = dtz.indent; |
| 1893 | 1843 | dtz.indent += 2; |
| 1894 | 1844 | try dtz.dumpBody(condbr.then_body, writer); |
| 1895 | 1845 | |
| 1896 | 1846 | try writer.writeByteNTimes(' ', old_indent); |
| 1897 | | try writer.writeAll("else:\n"); |
| 1847 | try writer.writeAll("}, {\n"); |
| 1898 | 1848 | |
| 1899 | 1849 | try dtz.dumpBody(condbr.else_body, writer); |
| 1900 | 1850 | dtz.indent = old_indent; |
| 1901 | 1851 | |
| 1902 | 1852 | try writer.writeByteNTimes(' ', old_indent); |
| 1903 | | try writer.writeAll(")\n"); |
| 1853 | try writer.writeAll("})\n"); |
| 1904 | 1854 | }, |
| 1905 | 1855 | |
| 1906 | 1856 | .loop => { |
| ... | ... | @@ -1917,9 +1867,41 @@ const DumpTzir = struct { |
| 1917 | 1867 | try writer.writeAll(")\n"); |
| 1918 | 1868 | }, |
| 1919 | 1869 | |
| 1870 | .call => { |
| 1871 | const call = inst.castTag(.call).?; |
| 1872 | |
| 1873 | const args_kinky = try dtz.allocator.alloc(?usize, call.args.len); |
| 1874 | defer dtz.allocator.free(args_kinky); |
| 1875 | std.mem.set(?usize, args_kinky, null); |
| 1876 | var any_kinky_args = false; |
| 1877 | |
| 1878 | const func_kinky = try dtz.writeInst(writer, call.func); |
| 1879 | |
| 1880 | for (call.args) |arg, i| { |
| 1881 | try writer.writeAll(", "); |
| 1882 | |
| 1883 | args_kinky[i] = try dtz.writeInst(writer, arg); |
| 1884 | any_kinky_args = any_kinky_args or args_kinky[i] != null; |
| 1885 | } |
| 1886 | |
| 1887 | if (func_kinky != null or any_kinky_args) { |
| 1888 | try writer.writeAll(") // Instruction does not dominate all uses!"); |
| 1889 | if (func_kinky) |func_index| { |
| 1890 | try writer.print(" %{d}", .{func_index}); |
| 1891 | } |
| 1892 | for (args_kinky) |arg_kinky| { |
| 1893 | if (arg_kinky) |arg_index| { |
| 1894 | try writer.print(" %{d}", .{arg_index}); |
| 1895 | } |
| 1896 | } |
| 1897 | try writer.writeAll("\n"); |
| 1898 | } else { |
| 1899 | try writer.writeAll(")\n"); |
| 1900 | } |
| 1901 | }, |
| 1902 | |
| 1920 | 1903 | // TODO fill out this debug printing |
| 1921 | 1904 | .assembly, |
| 1922 | | .call, |
| 1923 | 1905 | .constant, |
| 1924 | 1906 | .varptr, |
| 1925 | 1907 | .switchbr, |
| ... | ... | @@ -1930,6 +1912,22 @@ const DumpTzir = struct { |
| 1930 | 1912 | } |
| 1931 | 1913 | } |
| 1932 | 1914 | |
| 1915 | fn writeInst(dtz: *DumpTzir, writer: std.fs.File.Writer, inst: *ir.Inst) !?usize { |
| 1916 | if (dtz.partial_inst_table.get(inst)) |operand_index| { |
| 1917 | try writer.print("%{d}", .{operand_index}); |
| 1918 | return null; |
| 1919 | } else if (dtz.const_table.get(inst)) |operand_index| { |
| 1920 | try writer.print("@{d}", .{operand_index}); |
| 1921 | return null; |
| 1922 | } else if (dtz.inst_table.get(inst)) |operand_index| { |
| 1923 | try writer.print("%{d}", .{operand_index}); |
| 1924 | return operand_index; |
| 1925 | } else { |
| 1926 | try writer.writeAll("!BADREF!"); |
| 1927 | return null; |
| 1928 | } |
| 1929 | } |
| 1930 | |
| 1933 | 1931 | fn findConst(dtz: *DumpTzir, operand: *ir.Inst) !void { |
| 1934 | 1932 | if (operand.tag == .constant) { |
| 1935 | 1933 | try dtz.const_table.put(operand, dtz.next_const_index); |