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