| ... | ... | @@ -1915,10 +1915,28 @@ const DumpTzir = struct { |
| 1915 | 1915 | |
| 1916 | 1916 | const InstTable = std.AutoArrayHashMap(*ir.Inst, usize); |
| 1917 | 1917 | |
| 1918 | /// TODO: Improve this code to include a stack of ir.Body and store the instructions |
| 1919 | /// in there. Now we are putting all the instructions in a function local table, |
| 1920 | /// however instructions that are in a Body can be thown away when the Body ends. |
| 1918 | 1921 | fn dump(dtz: *DumpTzir, body: ir.Body, writer: std.fs.File.Writer) !void { |
| 1919 | 1922 | // First pass to pre-populate the table so that we can show even invalid references. |
| 1920 | 1923 | // Must iterate the same order we iterate the second time. |
| 1921 | 1924 | // We also look for constants and put them in the const_table. |
| 1925 | try dtz.fetchInstsAndResolveConsts(body); |
| 1926 | |
| 1927 | std.debug.print("Module.Function(name={s}):\n", .{dtz.module_fn.owner_decl.name}); |
| 1928 | |
| 1929 | for (dtz.const_table.items()) |entry| { |
| 1930 | const constant = entry.key.castTag(.constant).?; |
| 1931 | try writer.print(" @{d}: {} = {};\n", .{ |
| 1932 | entry.value, constant.base.ty, constant.val, |
| 1933 | }); |
| 1934 | } |
| 1935 | |
| 1936 | return dtz.dumpBody(body, writer); |
| 1937 | } |
| 1938 | |
| 1939 | fn fetchInstsAndResolveConsts(dtz: *DumpTzir, body: ir.Body) error{OutOfMemory}!void { |
| 1922 | 1940 | for (body.instructions) |inst| { |
| 1923 | 1941 | try dtz.inst_table.put(inst, dtz.next_index); |
| 1924 | 1942 | dtz.next_index += 1; |
| ... | ... | @@ -1981,11 +1999,21 @@ const DumpTzir = struct { |
| 1981 | 1999 | try dtz.findConst(&brvoid.block.base); |
| 1982 | 2000 | }, |
| 1983 | 2001 | |
| 2002 | .block => { |
| 2003 | const block = inst.castTag(.block).?; |
| 2004 | try dtz.fetchInstsAndResolveConsts(block.body); |
| 2005 | }, |
| 2006 | |
| 2007 | .condbr => { |
| 2008 | const condbr = inst.castTag(.condbr).?; |
| 2009 | try dtz.findConst(condbr.condition); |
| 2010 | try dtz.fetchInstsAndResolveConsts(condbr.then_body); |
| 2011 | try dtz.fetchInstsAndResolveConsts(condbr.else_body); |
| 2012 | }, |
| 2013 | |
| 1984 | 2014 | // TODO fill out this debug printing |
| 1985 | 2015 | .assembly, |
| 1986 | | .block, |
| 1987 | 2016 | .call, |
| 1988 | | .condbr, |
| 1989 | 2017 | .constant, |
| 1990 | 2018 | .loop, |
| 1991 | 2019 | .varptr, |
| ... | ... | @@ -1993,20 +2021,9 @@ const DumpTzir = struct { |
| 1993 | 2021 | => {}, |
| 1994 | 2022 | } |
| 1995 | 2023 | } |
| 1996 | | |
| 1997 | | std.debug.print("Module.Function(name={s}):\n", .{dtz.module_fn.owner_decl.name}); |
| 1998 | | |
| 1999 | | for (dtz.const_table.items()) |entry| { |
| 2000 | | const constant = entry.key.castTag(.constant).?; |
| 2001 | | try writer.print(" @{d}: {} = {};\n", .{ |
| 2002 | | entry.value, constant.base.ty, constant.val, |
| 2003 | | }); |
| 2004 | | } |
| 2005 | | |
| 2006 | | return dtz.dumpBody(body, writer); |
| 2007 | 2024 | } |
| 2008 | 2025 | |
| 2009 | | fn dumpBody(dtz: *DumpTzir, body: ir.Body, writer: std.fs.File.Writer) !void { |
| 2026 | fn dumpBody(dtz: *DumpTzir, body: ir.Body, writer: std.fs.File.Writer) (std.fs.File.WriteError || error{OutOfMemory})!void { |
| 2010 | 2027 | for (body.instructions) |inst| { |
| 2011 | 2028 | const my_index = dtz.next_partial_index; |
| 2012 | 2029 | try dtz.partial_inst_table.put(inst, my_index); |
| ... | ... | @@ -2167,11 +2184,54 @@ const DumpTzir = struct { |
| 2167 | 2184 | } |
| 2168 | 2185 | }, |
| 2169 | 2186 | |
| 2187 | .block => { |
| 2188 | const block = inst.castTag(.block).?; |
| 2189 | |
| 2190 | try writer.writeAll("\n"); |
| 2191 | |
| 2192 | const old_indent = dtz.indent; |
| 2193 | dtz.indent += 2; |
| 2194 | try dtz.dumpBody(block.body, writer); |
| 2195 | dtz.indent = old_indent; |
| 2196 | |
| 2197 | try writer.writeByteNTimes(' ', dtz.indent); |
| 2198 | try writer.writeAll(")\n"); |
| 2199 | }, |
| 2200 | |
| 2201 | .condbr => { |
| 2202 | const condbr = inst.castTag(.condbr).?; |
| 2203 | |
| 2204 | if (dtz.partial_inst_table.get(condbr.condition)) |operand_index| { |
| 2205 | try writer.print("%{d},", .{operand_index}); |
| 2206 | } else if (dtz.const_table.get(condbr.condition)) |operand_index| { |
| 2207 | try writer.print("@{d},", .{operand_index}); |
| 2208 | } else if (dtz.inst_table.get(condbr.condition)) |operand_index| { |
| 2209 | try writer.print("%{d}, // Instruction does not dominate all uses!", .{operand_index}); |
| 2210 | } else { |
| 2211 | try writer.writeAll("!BADREF!,"); |
| 2212 | } |
| 2213 | try writer.writeAll("\n"); |
| 2214 | |
| 2215 | try writer.writeByteNTimes(' ', dtz.indent); |
| 2216 | try writer.writeAll("then:\n"); |
| 2217 | |
| 2218 | const old_indent = dtz.indent; |
| 2219 | dtz.indent += 2; |
| 2220 | try dtz.dumpBody(condbr.then_body, writer); |
| 2221 | |
| 2222 | try writer.writeByteNTimes(' ', old_indent); |
| 2223 | try writer.writeAll("else:\n"); |
| 2224 | |
| 2225 | try dtz.dumpBody(condbr.else_body, writer); |
| 2226 | dtz.indent = old_indent; |
| 2227 | |
| 2228 | try writer.writeByteNTimes(' ', old_indent); |
| 2229 | try writer.writeAll(")\n"); |
| 2230 | }, |
| 2231 | |
| 2170 | 2232 | // TODO fill out this debug printing |
| 2171 | 2233 | .assembly, |
| 2172 | | .block, |
| 2173 | 2234 | .call, |
| 2174 | | .condbr, |
| 2175 | 2235 | .constant, |
| 2176 | 2236 | .loop, |
| 2177 | 2237 | .varptr, |