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 {...@@ -1054,6 +1054,10 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
1054 .param_types = param_types,1054 .param_types = param_types,
1055 }, .{});1055 }, .{});
10561056
1057 if (self.comp.verbose_ir) {
1058 zir.dumpZir(self.gpa, "fn_type", decl.name, fn_type_scope.instructions.items) catch {};
1059 }
1060
1057 // We need the memory for the Type to go into the arena for the Decl1061 // We need the memory for the Type to go into the arena for the Decl
1058 var decl_arena = std.heap.ArenaAllocator.init(self.gpa);1062 var decl_arena = std.heap.ArenaAllocator.init(self.gpa);
1059 errdefer decl_arena.deinit();1063 errdefer decl_arena.deinit();
...@@ -1127,6 +1131,10 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {...@@ -1127,6 +1131,10 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
1127 _ = try astgen.addZIRNoOp(self, &gen_scope.base, src, .returnvoid);1131 _ = try astgen.addZIRNoOp(self, &gen_scope.base, src, .returnvoid);
1128 }1132 }
11291133
1134 if (self.comp.verbose_ir) {
1135 zir.dumpZir(self.gpa, "fn_body", decl.name, gen_scope.instructions.items) catch {};
1136 }
1137
1130 const fn_zir = try gen_scope_arena.allocator.create(Fn.ZIR);1138 const fn_zir = try gen_scope_arena.allocator.create(Fn.ZIR);
1131 fn_zir.* = .{1139 fn_zir.* = .{
1132 .body = .{1140 .body = .{
...@@ -1258,6 +1266,9 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {...@@ -1258,6 +1266,9 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
12581266
1259 const src = tree.token_locs[init_node.firstToken()].start;1267 const src = tree.token_locs[init_node.firstToken()].start;
1260 const init_inst = try astgen.expr(self, &gen_scope.base, init_result_loc, init_node);1268 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
1262 var inner_block: Scope.Block = .{1273 var inner_block: Scope.Block = .{
1263 .parent = null,1274 .parent = null,
...@@ -1299,6 +1310,10 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {...@@ -1299,6 +1310,10 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
1299 .val = Value.initTag(.type_type),1310 .val = Value.initTag(.type_type),
1300 });1311 });
1301 const var_type = try astgen.expr(self, &type_scope.base, .{ .ty = type_type }, type_node);1312 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
1302 const ty = try zir_sema.analyzeBodyValueAsType(self, &block_scope, var_type, .{1317 const ty = try zir_sema.analyzeBodyValueAsType(self, &block_scope, var_type, .{
1303 .instructions = type_scope.instructions.items,1318 .instructions = type_scope.instructions.items,
1304 });1319 });
...@@ -1372,6 +1387,9 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {...@@ -1372,6 +1387,9 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
1372 defer gen_scope.instructions.deinit(self.gpa);1387 defer gen_scope.instructions.deinit(self.gpa);
13731388
1374 _ = try astgen.comptimeExpr(self, &gen_scope.base, .none, comptime_decl.expr);1389 _ = 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
1376 var block_scope: Scope.Block = .{1394 var block_scope: Scope.Block = .{
1377 .parent = null,1395 .parent = null,
src/zir.zig+51-2
...@@ -1255,8 +1255,8 @@ const Writer = struct {...@@ -1255,8 +1255,8 @@ const Writer = struct {
1255 bool => return stream.writeByte("01"[@boolToInt(param)]),1255 bool => return stream.writeByte("01"[@boolToInt(param)]),
1256 []u8, []const u8 => return stream.print("\"{Z}\"", .{param}),1256 []u8, []const u8 => return stream.print("\"{Z}\"", .{param}),
1257 BigIntConst, usize => return stream.print("{}", .{param}),1257 BigIntConst, usize => return stream.print("{}", .{param}),
1258 TypedValue => unreachable, // this is a special case1258 TypedValue => return stream.print("TypedValue{{ .ty = {}, .val = {}}}", .{ param.ty, param.val }),
1259 *IrModule.Decl => unreachable, // this is a special case1259 *IrModule.Decl => return stream.print("Decl({s})", .{param.name}),
1260 *Inst.Block => {1260 *Inst.Block => {
1261 const name = self.block_table.get(param).?;1261 const name = self.block_table.get(param).?;
1262 return stream.print("\"{Z}\"", .{name});1262 return stream.print("\"{Z}\"", .{name});
...@@ -2830,3 +2830,52 @@ const EmitZIR = struct {...@@ -2830,3 +2830,52 @@ const EmitZIR = struct {
2830 return decl;2830 return decl;
2831 }2831 }
2832};2832};
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}