authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-10-16 15:41:43+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-10-30 15:58:12+02:00
log95467f324909055d014e989ca9be7b0bb04237c4
tree2dfed2b9beaf813905171f134ed7c55b4beb0b38
parent570f610341e0b79a055bb5533ff4d11d0fe6534f
signaturelock-open Commit is signed but in an unrecognized format.

stage2: dump generated zir with --verbose-ir


2 files changed, 69 insertions(+), 2 deletions(-)

src/Module.zig+18
......@@ -1054,6 +1054,10 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
10541054 .param_types = param_types,
10551055 }, .{});
10561056
1057 if (self.comp.verbose_ir) {
1058 zir.dumpZir(self.gpa, "fn_type", decl.name, fn_type_scope.instructions.items) catch {};
1059 }
1060
10571061 // We need the memory for the Type to go into the arena for the Decl
10581062 var decl_arena = std.heap.ArenaAllocator.init(self.gpa);
10591063 errdefer decl_arena.deinit();
......@@ -1127,6 +1131,10 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
11271131 _ = try astgen.addZIRNoOp(self, &gen_scope.base, src, .returnvoid);
11281132 }
11291133
1134 if (self.comp.verbose_ir) {
1135 zir.dumpZir(self.gpa, "fn_body", decl.name, gen_scope.instructions.items) catch {};
1136 }
1137
11301138 const fn_zir = try gen_scope_arena.allocator.create(Fn.ZIR);
11311139 fn_zir.* = .{
11321140 .body = .{
......@@ -1258,6 +1266,9 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
12581266
12591267 const src = tree.token_locs[init_node.firstToken()].start;
12601268 const init_inst = try astgen.expr(self, &gen_scope.base, init_result_loc, init_node);
1269 if (self.comp.verbose_ir) {
1270 zir.dumpZir(self.gpa, "var_init", decl.name, gen_scope.instructions.items) catch {};
1271 }
12611272
12621273 var inner_block: Scope.Block = .{
12631274 .parent = null,
......@@ -1299,6 +1310,10 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
12991310 .val = Value.initTag(.type_type),
13001311 });
13011312 const var_type = try astgen.expr(self, &type_scope.base, .{ .ty = type_type }, type_node);
1313 if (self.comp.verbose_ir) {
1314 zir.dumpZir(self.gpa, "var_type", decl.name, type_scope.instructions.items) catch {};
1315 }
1316
13021317 const ty = try zir_sema.analyzeBodyValueAsType(self, &block_scope, var_type, .{
13031318 .instructions = type_scope.instructions.items,
13041319 });
......@@ -1372,6 +1387,9 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
13721387 defer gen_scope.instructions.deinit(self.gpa);
13731388
13741389 _ = try astgen.comptimeExpr(self, &gen_scope.base, .none, comptime_decl.expr);
1390 if (self.comp.verbose_ir) {
1391 zir.dumpZir(self.gpa, "comptime_block", decl.name, gen_scope.instructions.items) catch {};
1392 }
13751393
13761394 var block_scope: Scope.Block = .{
13771395 .parent = null,
src/zir.zig+51-2
......@@ -1255,8 +1255,8 @@ const Writer = struct {
12551255 bool => return stream.writeByte("01"[@boolToInt(param)]),
12561256 []u8, []const u8 => return stream.print("\"{Z}\"", .{param}),
12571257 BigIntConst, usize => return stream.print("{}", .{param}),
1258 TypedValue => unreachable, // this is a special case
1259 *IrModule.Decl => unreachable, // this is a special case
1258 TypedValue => return stream.print("TypedValue{{ .ty = {}, .val = {}}}", .{ param.ty, param.val }),
1259 *IrModule.Decl => return stream.print("Decl({s})", .{param.name}),
12601260 *Inst.Block => {
12611261 const name = self.block_table.get(param).?;
12621262 return stream.print("\"{Z}\"", .{name});
......@@ -2830,3 +2830,52 @@ const EmitZIR = struct {
28302830 return decl;
28312831 }
28322832};
2833
2834/// For debugging purposes, like dumpFn but for unanalyzed zir blocks
2835pub fn dumpZir(allocator: *Allocator, kind: []const u8, decl_name: [*:0]const u8, instructions: []*Inst) !void {
2836 var fib = std.heap.FixedBufferAllocator.init(&[_]u8{});
2837 var module = Module{
2838 .decls = &[_]*Decl{},
2839 .arena = std.heap.ArenaAllocator.init(&fib.allocator),
2840 .metadata = std.AutoHashMap(*Inst, Module.MetaData).init(&fib.allocator),
2841 .body_metadata = std.AutoHashMap(*Module.Body, Module.BodyMetaData).init(&fib.allocator),
2842 };
2843 var write = Writer{
2844 .module = &module,
2845 .inst_table = InstPtrTable.init(allocator),
2846 .block_table = std.AutoHashMap(*Inst.Block, []const u8).init(allocator),
2847 .loop_table = std.AutoHashMap(*Inst.Loop, []const u8).init(allocator),
2848 .arena = std.heap.ArenaAllocator.init(allocator),
2849 .indent = 2,
2850 .next_instr_index = 0,
2851 };
2852 defer write.arena.deinit();
2853 defer write.inst_table.deinit();
2854 defer write.block_table.deinit();
2855 defer write.loop_table.deinit();
2856
2857 try write.inst_table.ensureCapacity(@intCast(u32, instructions.len));
2858
2859 const stderr = std.io.getStdErr().outStream();
2860 try stderr.print("{} {s} {{ // unanalyzed\n", .{ kind, decl_name });
2861
2862 for (instructions) |inst| {
2863 const my_i = write.next_instr_index;
2864 write.next_instr_index += 1;
2865
2866 if (inst.cast(Inst.Block)) |block| {
2867 const name = try std.fmt.allocPrint(&write.arena.allocator, "label_{}", .{my_i});
2868 try write.block_table.put(block, name);
2869 } else if (inst.cast(Inst.Loop)) |loop| {
2870 const name = try std.fmt.allocPrint(&write.arena.allocator, "loop_{}", .{my_i});
2871 try write.loop_table.put(loop, name);
2872 }
2873
2874 try write.inst_table.putNoClobber(inst, .{ .inst = inst, .index = my_i, .name = "inst" });
2875 try stderr.print(" %{} ", .{my_i});
2876 try write.writeInstToStream(stderr, inst);
2877 try stderr.writeByte('\n');
2878 }
2879
2880 try stderr.print("}} // {} {s}\n\n", .{ kind, decl_name });
2881}