authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-01-19 21:09:46-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-01-19 21:09:46-07:00
log8098b3f84cf24878e3388e056f60aba69033e0f6
tree66a524846d0d5bea7d6bb733b2f16e5ee77238a3
parent41e6aa78bb4240941167ac4f80aaa37d26c0c315

stage2: implement TZIR printing for call instruction


1 files changed, 78 insertions(+), 80 deletions(-)

src/zir.zig+78-80
...@@ -1681,10 +1681,16 @@ const DumpTzir = struct {...@@ -1681,10 +1681,16 @@ const DumpTzir = struct {
1681 const loop = inst.castTag(.loop).?;1681 const loop = inst.castTag(.loop).?;
1682 try dtz.fetchInstsAndResolveConsts(loop.body);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 },
16841691
1685 // TODO fill out this debug printing1692 // TODO fill out this debug printing
1686 .assembly,1693 .assembly,
1687 .call,
1688 .constant,1694 .constant,
1689 .varptr,1695 .varptr,
1690 .switchbr,1696 .switchbr,
...@@ -1730,16 +1736,11 @@ const DumpTzir = struct {...@@ -1730,16 +1736,11 @@ const DumpTzir = struct {
1730 .wrap_optional,1736 .wrap_optional,
1731 => {1737 => {
1732 const un_op = inst.cast(ir.Inst.UnOp).?;1738 const un_op = inst.cast(ir.Inst.UnOp).?;
1733 if (dtz.partial_inst_table.get(un_op.operand)) |operand_index| {1739 const kinky = try dtz.writeInst(writer, un_op.operand);
1734 try writer.print("%{d})\n", .{operand_index});1740 if (kinky != null) {
1735 } else if (dtz.const_table.get(un_op.operand)) |operand_index| {1741 try writer.writeAll(") // Instruction does not dominate all uses!\n");
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 });
1741 } else {1742 } else {
1742 try writer.writeAll("!BADREF!)\n");1743 try writer.writeAll(")\n");
1743 }1744 }
1744 },1745 },
17451746
...@@ -1758,30 +1759,12 @@ const DumpTzir = struct {...@@ -1758,30 +1759,12 @@ const DumpTzir = struct {
1758 .bitor,1759 .bitor,
1759 .xor,1760 .xor,
1760 => {1761 => {
1761 var lhs_kinky: ?usize = null;
1762 var rhs_kinky: ?usize = null;
1763
1764 const bin_op = inst.cast(ir.Inst.BinOp).?;1762 const bin_op = inst.cast(ir.Inst.BinOp).?;
1765 if (dtz.partial_inst_table.get(bin_op.lhs)) |operand_index| {1763
1766 try writer.print("%{d}, ", .{operand_index});1764 const lhs_kinky = try dtz.writeInst(writer, bin_op.lhs);
1767 } else if (dtz.const_table.get(bin_op.lhs)) |operand_index| {1765 try writer.writeAll(", ");
1768 try writer.print("@{d}, ", .{operand_index});1766 const rhs_kinky = try dtz.writeInst(writer, bin_op.rhs);
1769 } else if (dtz.inst_table.get(bin_op.lhs)) |operand_index| {1767
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 }
1785 if (lhs_kinky != null or rhs_kinky != null) {1768 if (lhs_kinky != null or rhs_kinky != null) {
1786 try writer.writeAll(") // Instruction does not dominate all uses!");1769 try writer.writeAll(") // Instruction does not dominate all uses!");
1787 if (lhs_kinky) |lhs| {1770 if (lhs_kinky) |lhs| {
...@@ -1804,30 +1787,9 @@ const DumpTzir = struct {...@@ -1804,30 +1787,9 @@ const DumpTzir = struct {
1804 .br => {1787 .br => {
1805 const br = inst.castTag(.br).?;1788 const br = inst.castTag(.br).?;
18061789
1807 var lhs_kinky: ?usize = null;1790 const lhs_kinky = try dtz.writeInst(writer, &br.block.base);
1808 var rhs_kinky: ?usize = null;1791 try writer.writeAll(", ");
18091792 const rhs_kinky = try dtz.writeInst(writer, br.operand);
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 }
18311793
1832 if (lhs_kinky != null or rhs_kinky != null) {1794 if (lhs_kinky != null or rhs_kinky != null) {
1833 try writer.writeAll(") // Instruction does not dominate all uses!");1795 try writer.writeAll(") // Instruction does not dominate all uses!");
...@@ -1845,16 +1807,11 @@ const DumpTzir = struct {...@@ -1845,16 +1807,11 @@ const DumpTzir = struct {
18451807
1846 .brvoid => {1808 .brvoid => {
1847 const brvoid = inst.castTag(.brvoid).?;1809 const brvoid = inst.castTag(.brvoid).?;
1848 if (dtz.partial_inst_table.get(&brvoid.block.base)) |operand_index| {1810 const kinky = try dtz.writeInst(writer, &brvoid.block.base);
1849 try writer.print("%{d})\n", .{operand_index});1811 if (kinky) |_| {
1850 } else if (dtz.const_table.get(&brvoid.block.base)) |operand_index| {1812 try writer.writeAll(") // Instruction does not dominate all uses!\n");
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 });
1856 } else {1813 } else {
1857 try writer.writeAll("!BADREF!)\n");1814 try writer.writeAll(")\n");
1858 }1815 }
1859 },1816 },
18601817
...@@ -1875,32 +1832,25 @@ const DumpTzir = struct {...@@ -1875,32 +1832,25 @@ const DumpTzir = struct {
1875 .condbr => {1832 .condbr => {
1876 const condbr = inst.castTag(.condbr).?;1833 const condbr = inst.castTag(.condbr).?;
18771834
1878 if (dtz.partial_inst_table.get(condbr.condition)) |operand_index| {1835 const condition_kinky = try dtz.writeInst(writer, condbr.condition);
1879 try writer.print("%{d},", .{operand_index});1836 if (condition_kinky != null) {
1880 } else if (dtz.const_table.get(condbr.condition)) |operand_index| {1837 try writer.writeAll(", { // Instruction does not dominate all uses!\n");
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});
1884 } else {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");
18911841
1892 const old_indent = dtz.indent;1842 const old_indent = dtz.indent;
1893 dtz.indent += 2;1843 dtz.indent += 2;
1894 try dtz.dumpBody(condbr.then_body, writer);1844 try dtz.dumpBody(condbr.then_body, writer);
18951845
1896 try writer.writeByteNTimes(' ', old_indent);1846 try writer.writeByteNTimes(' ', old_indent);
1897 try writer.writeAll("else:\n");1847 try writer.writeAll("}, {\n");
18981848
1899 try dtz.dumpBody(condbr.else_body, writer);1849 try dtz.dumpBody(condbr.else_body, writer);
1900 dtz.indent = old_indent;1850 dtz.indent = old_indent;
19011851
1902 try writer.writeByteNTimes(' ', old_indent);1852 try writer.writeByteNTimes(' ', old_indent);
1903 try writer.writeAll(")\n");1853 try writer.writeAll("})\n");
1904 },1854 },
19051855
1906 .loop => {1856 .loop => {
...@@ -1917,9 +1867,41 @@ const DumpTzir = struct {...@@ -1917,9 +1867,41 @@ const DumpTzir = struct {
1917 try writer.writeAll(")\n");1867 try writer.writeAll(")\n");
1918 },1868 },
19191869
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 // TODO fill out this debug printing1903 // TODO fill out this debug printing
1921 .assembly,1904 .assembly,
1922 .call,
1923 .constant,1905 .constant,
1924 .varptr,1906 .varptr,
1925 .switchbr,1907 .switchbr,
...@@ -1930,6 +1912,22 @@ const DumpTzir = struct {...@@ -1930,6 +1912,22 @@ const DumpTzir = struct {
1930 }1912 }
1931 }1913 }
19321914
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 fn findConst(dtz: *DumpTzir, operand: *ir.Inst) !void {1931 fn findConst(dtz: *DumpTzir, operand: *ir.Inst) !void {
1934 if (operand.tag == .constant) {1932 if (operand.tag == .constant) {
1935 try dtz.const_table.put(operand, dtz.next_const_index);1933 try dtz.const_table.put(operand, dtz.next_const_index);