authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-07-08 05:44:51+00:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-07-08 05:44:51+00:00
log88496041312023948cfd34f6e96acc5d27ee4cf6
tree8938f9fe13c2c3c881907714cbdcc0b0e123ccb7
parentab9df5b04b862800ffee720635b6bec0185b5054

stage2: proper indenting when printing ZIR text


1 files changed, 48 insertions(+), 42 deletions(-)

src-self-hosted/zir.zig+48-42
...@@ -648,7 +648,7 @@ pub const Module = struct {...@@ -648,7 +648,7 @@ pub const Module = struct {
648648
649 for (self.decls) |decl, i| {649 for (self.decls) |decl, i| {
650 try stream.print("@{} ", .{decl.name});650 try stream.print("@{} ", .{decl.name});
651 try self.writeInstToStream(stream, decl.inst, &inst_table);651 try self.writeInstToStream(stream, decl.inst, &inst_table, 2);
652 try stream.writeByte('\n');652 try stream.writeByte('\n');
653 }653 }
654 }654 }
...@@ -658,43 +658,44 @@ pub const Module = struct {...@@ -658,43 +658,44 @@ pub const Module = struct {
658 stream: var,658 stream: var,
659 inst: *Inst,659 inst: *Inst,
660 inst_table: *const InstPtrTable,660 inst_table: *const InstPtrTable,
661 indent: usize,
661 ) @TypeOf(stream).Error!void {662 ) @TypeOf(stream).Error!void {
662 // TODO I tried implementing this with an inline for loop and hit a compiler bug663 // TODO I tried implementing this with an inline for loop and hit a compiler bug
663 switch (inst.tag) {664 switch (inst.tag) {
664 .arg => return self.writeInstToStreamGeneric(stream, .arg, inst, inst_table),665 .arg => return self.writeInstToStreamGeneric(stream, .arg, inst, inst_table, indent),
665 .block => return self.writeInstToStreamGeneric(stream, .block, inst, inst_table),666 .block => return self.writeInstToStreamGeneric(stream, .block, inst, inst_table, indent),
666 .breakpoint => return self.writeInstToStreamGeneric(stream, .breakpoint, inst, inst_table),667 .breakpoint => return self.writeInstToStreamGeneric(stream, .breakpoint, inst, inst_table, indent),
667 .breakvoid => return self.writeInstToStreamGeneric(stream, .breakvoid, inst, inst_table),668 .breakvoid => return self.writeInstToStreamGeneric(stream, .breakvoid, inst, inst_table, indent),
668 .call => return self.writeInstToStreamGeneric(stream, .call, inst, inst_table),669 .call => return self.writeInstToStreamGeneric(stream, .call, inst, inst_table, indent),
669 .declref => return self.writeInstToStreamGeneric(stream, .declref, inst, inst_table),670 .declref => return self.writeInstToStreamGeneric(stream, .declref, inst, inst_table, indent),
670 .declref_str => return self.writeInstToStreamGeneric(stream, .declref_str, inst, inst_table),671 .declref_str => return self.writeInstToStreamGeneric(stream, .declref_str, inst, inst_table, indent),
671 .declval => return self.writeInstToStreamGeneric(stream, .declval, inst, inst_table),672 .declval => return self.writeInstToStreamGeneric(stream, .declval, inst, inst_table, indent),
672 .declval_in_module => return self.writeInstToStreamGeneric(stream, .declval_in_module, inst, inst_table),673 .declval_in_module => return self.writeInstToStreamGeneric(stream, .declval_in_module, inst, inst_table, indent),
673 .compileerror => return self.writeInstToStreamGeneric(stream, .compileerror, inst, inst_table),674 .compileerror => return self.writeInstToStreamGeneric(stream, .compileerror, inst, inst_table, indent),
674 .@"const" => return self.writeInstToStreamGeneric(stream, .@"const", inst, inst_table),675 .@"const" => return self.writeInstToStreamGeneric(stream, .@"const", inst, inst_table, indent),
675 .str => return self.writeInstToStreamGeneric(stream, .str, inst, inst_table),676 .str => return self.writeInstToStreamGeneric(stream, .str, inst, inst_table, indent),
676 .int => return self.writeInstToStreamGeneric(stream, .int, inst, inst_table),677 .int => return self.writeInstToStreamGeneric(stream, .int, inst, inst_table, indent),
677 .ptrtoint => return self.writeInstToStreamGeneric(stream, .ptrtoint, inst, inst_table),678 .ptrtoint => return self.writeInstToStreamGeneric(stream, .ptrtoint, inst, inst_table, indent),
678 .fieldptr => return self.writeInstToStreamGeneric(stream, .fieldptr, inst, inst_table),679 .fieldptr => return self.writeInstToStreamGeneric(stream, .fieldptr, inst, inst_table, indent),
679 .deref => return self.writeInstToStreamGeneric(stream, .deref, inst, inst_table),680 .deref => return self.writeInstToStreamGeneric(stream, .deref, inst, inst_table, indent),
680 .as => return self.writeInstToStreamGeneric(stream, .as, inst, inst_table),681 .as => return self.writeInstToStreamGeneric(stream, .as, inst, inst_table, indent),
681 .@"asm" => return self.writeInstToStreamGeneric(stream, .@"asm", inst, inst_table),682 .@"asm" => return self.writeInstToStreamGeneric(stream, .@"asm", inst, inst_table, indent),
682 .@"unreachable" => return self.writeInstToStreamGeneric(stream, .@"unreachable", inst, inst_table),683 .@"unreachable" => return self.writeInstToStreamGeneric(stream, .@"unreachable", inst, inst_table, indent),
683 .@"return" => return self.writeInstToStreamGeneric(stream, .@"return", inst, inst_table),684 .@"return" => return self.writeInstToStreamGeneric(stream, .@"return", inst, inst_table, indent),
684 .returnvoid => return self.writeInstToStreamGeneric(stream, .returnvoid, inst, inst_table),685 .returnvoid => return self.writeInstToStreamGeneric(stream, .returnvoid, inst, inst_table, indent),
685 .@"fn" => return self.writeInstToStreamGeneric(stream, .@"fn", inst, inst_table),686 .@"fn" => return self.writeInstToStreamGeneric(stream, .@"fn", inst, inst_table, indent),
686 .@"export" => return self.writeInstToStreamGeneric(stream, .@"export", inst, inst_table),687 .@"export" => return self.writeInstToStreamGeneric(stream, .@"export", inst, inst_table, indent),
687 .primitive => return self.writeInstToStreamGeneric(stream, .primitive, inst, inst_table),688 .primitive => return self.writeInstToStreamGeneric(stream, .primitive, inst, inst_table, indent),
688 .fntype => return self.writeInstToStreamGeneric(stream, .fntype, inst, inst_table),689 .fntype => return self.writeInstToStreamGeneric(stream, .fntype, inst, inst_table, indent),
689 .intcast => return self.writeInstToStreamGeneric(stream, .intcast, inst, inst_table),690 .intcast => return self.writeInstToStreamGeneric(stream, .intcast, inst, inst_table, indent),
690 .bitcast => return self.writeInstToStreamGeneric(stream, .bitcast, inst, inst_table),691 .bitcast => return self.writeInstToStreamGeneric(stream, .bitcast, inst, inst_table, indent),
691 .elemptr => return self.writeInstToStreamGeneric(stream, .elemptr, inst, inst_table),692 .elemptr => return self.writeInstToStreamGeneric(stream, .elemptr, inst, inst_table, indent),
692 .add => return self.writeInstToStreamGeneric(stream, .add, inst, inst_table),693 .add => return self.writeInstToStreamGeneric(stream, .add, inst, inst_table, indent),
693 .sub => return self.writeInstToStreamGeneric(stream, .sub, inst, inst_table),694 .sub => return self.writeInstToStreamGeneric(stream, .sub, inst, inst_table, indent),
694 .cmp => return self.writeInstToStreamGeneric(stream, .cmp, inst, inst_table),695 .cmp => return self.writeInstToStreamGeneric(stream, .cmp, inst, inst_table, indent),
695 .condbr => return self.writeInstToStreamGeneric(stream, .condbr, inst, inst_table),696 .condbr => return self.writeInstToStreamGeneric(stream, .condbr, inst, inst_table, indent),
696 .isnull => return self.writeInstToStreamGeneric(stream, .isnull, inst, inst_table),697 .isnull => return self.writeInstToStreamGeneric(stream, .isnull, inst, inst_table, indent),
697 .isnonnull => return self.writeInstToStreamGeneric(stream, .isnonnull, inst, inst_table),698 .isnonnull => return self.writeInstToStreamGeneric(stream, .isnonnull, inst, inst_table, indent),
698 }699 }
699 }700 }
700701
...@@ -704,7 +705,8 @@ pub const Module = struct {...@@ -704,7 +705,8 @@ pub const Module = struct {
704 comptime inst_tag: Inst.Tag,705 comptime inst_tag: Inst.Tag,
705 base: *Inst,706 base: *Inst,
706 inst_table: *const InstPtrTable,707 inst_table: *const InstPtrTable,
707 ) !void {708 indent: usize,
709 ) @TypeOf(stream).Error!void {
708 const SpecificInst = Inst.TagToType(inst_tag);710 const SpecificInst = Inst.TagToType(inst_tag);
709 const inst = @fieldParentPtr(SpecificInst, "base", base);711 const inst = @fieldParentPtr(SpecificInst, "base", base);
710 const Positionals = @TypeOf(inst.positionals);712 const Positionals = @TypeOf(inst.positionals);
...@@ -714,7 +716,7 @@ pub const Module = struct {...@@ -714,7 +716,7 @@ pub const Module = struct {
714 if (i != 0) {716 if (i != 0) {
715 try stream.writeAll(", ");717 try stream.writeAll(", ");
716 }718 }
717 try self.writeParamToStream(stream, @field(inst.positionals, arg_field.name), inst_table);719 try self.writeParamToStream(stream, @field(inst.positionals, arg_field.name), inst_table, indent);
718 }720 }
719721
720 comptime var need_comma = pos_fields.len != 0;722 comptime var need_comma = pos_fields.len != 0;
...@@ -724,13 +726,13 @@ pub const Module = struct {...@@ -724,13 +726,13 @@ pub const Module = struct {
724 if (@field(inst.kw_args, arg_field.name)) |non_optional| {726 if (@field(inst.kw_args, arg_field.name)) |non_optional| {
725 if (need_comma) try stream.writeAll(", ");727 if (need_comma) try stream.writeAll(", ");
726 try stream.print("{}=", .{arg_field.name});728 try stream.print("{}=", .{arg_field.name});
727 try self.writeParamToStream(stream, non_optional, inst_table);729 try self.writeParamToStream(stream, non_optional, inst_table, indent);
728 need_comma = true;730 need_comma = true;
729 }731 }
730 } else {732 } else {
731 if (need_comma) try stream.writeAll(", ");733 if (need_comma) try stream.writeAll(", ");
732 try stream.print("{}=", .{arg_field.name});734 try stream.print("{}=", .{arg_field.name});
733 try self.writeParamToStream(stream, @field(inst.kw_args, arg_field.name), inst_table);735 try self.writeParamToStream(stream, @field(inst.kw_args, arg_field.name), inst_table, indent);
734 need_comma = true;736 need_comma = true;
735 }737 }
736 }738 }
...@@ -738,7 +740,7 @@ pub const Module = struct {...@@ -738,7 +740,7 @@ pub const Module = struct {
738 try stream.writeByte(')');740 try stream.writeByte(')');
739 }741 }
740742
741 fn writeParamToStream(self: Module, stream: var, param: var, inst_table: *const InstPtrTable) !void {743 fn writeParamToStream(self: Module, stream: var, param: var, inst_table: *const InstPtrTable, indent: usize) !void {
742 if (@typeInfo(@TypeOf(param)) == .Enum) {744 if (@typeInfo(@TypeOf(param)) == .Enum) {
743 return stream.writeAll(@tagName(param));745 return stream.writeAll(@tagName(param));
744 }746 }
...@@ -757,10 +759,12 @@ pub const Module = struct {...@@ -757,10 +759,12 @@ pub const Module = struct {
757 Module.Body => {759 Module.Body => {
758 try stream.writeAll("{\n");760 try stream.writeAll("{\n");
759 for (param.instructions) |inst, i| {761 for (param.instructions) |inst, i| {
760 try stream.print(" %{} ", .{i});762 try stream.writeByteNTimes(' ', indent);
761 try self.writeInstToStream(stream, inst, inst_table);763 try stream.print("%{} ", .{i});
764 try self.writeInstToStream(stream, inst, inst_table, indent + 2);
762 try stream.writeByte('\n');765 try stream.writeByte('\n');
763 }766 }
767 try stream.writeByteNTimes(' ', indent - 2);
764 try stream.writeByte('}');768 try stream.writeByte('}');
765 },769 },
766 bool => return stream.writeByte("01"[@boolToInt(param)]),770 bool => return stream.writeByte("01"[@boolToInt(param)]),
...@@ -1205,6 +1209,7 @@ pub fn emit(allocator: *Allocator, old_module: IrModule) !Module {...@@ -1205,6 +1209,7 @@ pub fn emit(allocator: *Allocator, old_module: IrModule) !Module {
1205 .next_auto_name = 0,1209 .next_auto_name = 0,
1206 .names = std.StringHashMap(void).init(allocator),1210 .names = std.StringHashMap(void).init(allocator),
1207 .primitive_table = std.AutoHashMap(Inst.Primitive.Builtin, *Decl).init(allocator),1211 .primitive_table = std.AutoHashMap(Inst.Primitive.Builtin, *Decl).init(allocator),
1212 .indent = 0,
1208 };1213 };
1209 defer ctx.decls.deinit(allocator);1214 defer ctx.decls.deinit(allocator);
1210 defer ctx.names.deinit();1215 defer ctx.names.deinit();
...@@ -1227,6 +1232,7 @@ const EmitZIR = struct {...@@ -1227,6 +1232,7 @@ const EmitZIR = struct {
1227 names: std.StringHashMap(void),1232 names: std.StringHashMap(void),
1228 next_auto_name: usize,1233 next_auto_name: usize,
1229 primitive_table: std.AutoHashMap(Inst.Primitive.Builtin, *Decl),1234 primitive_table: std.AutoHashMap(Inst.Primitive.Builtin, *Decl),
1235 indent: usize,
12301236
1231 fn emit(self: *EmitZIR) !void {1237 fn emit(self: *EmitZIR) !void {
1232 // Put all the Decls in a list and sort them by name to avoid nondeterminism introduced1238 // Put all the Decls in a list and sort them by name to avoid nondeterminism introduced