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 {
16811681 const loop = inst.castTag(.loop).?;
16821682 try dtz.fetchInstsAndResolveConsts(loop.body);
16831683 },
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
16851692 // TODO fill out this debug printing
16861693 .assembly,
1687 .call,
16881694 .constant,
16891695 .varptr,
16901696 .switchbr,
......@@ -1730,16 +1736,11 @@ const DumpTzir = struct {
17301736 .wrap_optional,
17311737 => {
17321738 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");
17411742 } else {
1742 try writer.writeAll("!BADREF!)\n");
1743 try writer.writeAll(")\n");
17431744 }
17441745 },
17451746
......@@ -1758,30 +1759,12 @@ const DumpTzir = struct {
17581759 .bitor,
17591760 .xor,
17601761 => {
1761 var lhs_kinky: ?usize = null;
1762 var rhs_kinky: ?usize = null;
1763
17641762 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
17851768 if (lhs_kinky != null or rhs_kinky != null) {
17861769 try writer.writeAll(") // Instruction does not dominate all uses!");
17871770 if (lhs_kinky) |lhs| {
......@@ -1804,30 +1787,9 @@ const DumpTzir = struct {
18041787 .br => {
18051788 const br = inst.castTag(.br).?;
18061789
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);
18311793
18321794 if (lhs_kinky != null or rhs_kinky != null) {
18331795 try writer.writeAll(") // Instruction does not dominate all uses!");
......@@ -1845,16 +1807,11 @@ const DumpTzir = struct {
18451807
18461808 .brvoid => {
18471809 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");
18561813 } else {
1857 try writer.writeAll("!BADREF!)\n");
1814 try writer.writeAll(")\n");
18581815 }
18591816 },
18601817
......@@ -1875,32 +1832,25 @@ const DumpTzir = struct {
18751832 .condbr => {
18761833 const condbr = inst.castTag(.condbr).?;
18771834
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");
18841838 } else {
1885 try writer.writeAll("!BADREF!,");
1839 try writer.writeAll(", {\n");
18861840 }
1887 try writer.writeAll("\n");
1888
1889 try writer.writeByteNTimes(' ', dtz.indent);
1890 try writer.writeAll("then:\n");
18911841
18921842 const old_indent = dtz.indent;
18931843 dtz.indent += 2;
18941844 try dtz.dumpBody(condbr.then_body, writer);
18951845
18961846 try writer.writeByteNTimes(' ', old_indent);
1897 try writer.writeAll("else:\n");
1847 try writer.writeAll("}, {\n");
18981848
18991849 try dtz.dumpBody(condbr.else_body, writer);
19001850 dtz.indent = old_indent;
19011851
19021852 try writer.writeByteNTimes(' ', old_indent);
1903 try writer.writeAll(")\n");
1853 try writer.writeAll("})\n");
19041854 },
19051855
19061856 .loop => {
......@@ -1917,9 +1867,41 @@ const DumpTzir = struct {
19171867 try writer.writeAll(")\n");
19181868 },
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
19201903 // TODO fill out this debug printing
19211904 .assembly,
1922 .call,
19231905 .constant,
19241906 .varptr,
19251907 .switchbr,
......@@ -1930,6 +1912,22 @@ const DumpTzir = struct {
19301912 }
19311913 }
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
19331931 fn findConst(dtz: *DumpTzir, operand: *ir.Inst) !void {
19341932 if (operand.tag == .constant) {
19351933 try dtz.const_table.put(operand, dtz.next_const_index);