authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-18 22:19:28-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-18 22:19:28-07:00
logb2682237dbe90306b569cb36914f8823cd7b0431
tree9f35683ad3d3a2b254de6c2041734d37d8385be2
parentf5aca4a6a1ba867d3bc343a3740454468a7eff13

stage2: get Module and Sema compiling again

There are some `@panic("TODO")` in there but I'm trying to get the branch to the point where collaborators can jump in. Next is to repair the seam between LazySrcLoc and codegen's expected absolute file offsets.

9 files changed, 524 insertions(+), 599 deletions(-)

BRANCH_TODO+2
...@@ -27,6 +27,8 @@ Performance optimizations to look into:...@@ -27,6 +27,8 @@ Performance optimizations to look into:
27 and have it reference source code bytes. Another idea: null terminated27 and have it reference source code bytes. Another idea: null terminated
28 string variants which avoid having to store the length.28 string variants which avoid having to store the length.
29 - Look into this for enum literals too29 - Look into this for enum literals too
30 * make ret_type and ret_ptr instructions be implied indexes; no need to have
31 tags associated with them.
3032
3133
32Random snippets of code that I deleted and need to make sure get34Random snippets of code that I deleted and need to make sure get
src/Module.zig+131-98
...@@ -462,11 +462,11 @@ pub const Scope = struct {...@@ -462,11 +462,11 @@ pub const Scope = struct {
462 switch (scope.tag) {462 switch (scope.tag) {
463 .file => return &scope.cast(File).?.tree,463 .file => return &scope.cast(File).?.tree,
464 .block => return &scope.cast(Block).?.src_decl.container.file_scope.tree,464 .block => return &scope.cast(Block).?.src_decl.container.file_scope.tree,
465 .gen_zir => return &scope.cast(GenZir).?.decl.container.file_scope.tree,465 .gen_zir => return &scope.cast(GenZir).?.zir_code.decl.container.file_scope.tree,
466 .local_val => return &scope.cast(LocalVal).?.gen_zir.zir_code.decl.container.file_scope.tree,466 .local_val => return &scope.cast(LocalVal).?.gen_zir.zir_code.decl.container.file_scope.tree,
467 .local_ptr => return &scope.cast(LocalPtr).?.gen_zir.zir_code.decl.container.file_scope.tree,467 .local_ptr => return &scope.cast(LocalPtr).?.gen_zir.zir_code.decl.container.file_scope.tree,
468 .container => return &scope.cast(Container).?.file_scope.tree,468 .container => return &scope.cast(Container).?.file_scope.tree,
469 .gen_suspend => return &scope.cast(GenZir).?.decl.container.file_scope.tree,469 .gen_suspend => return &scope.cast(GenZir).?.zir_code.decl.container.file_scope.tree,
470 .gen_nosuspend => return &scope.cast(Nosuspend).?.gen_zir.zir_code.decl.container.file_scope.tree,470 .gen_nosuspend => return &scope.cast(Nosuspend).?.gen_zir.zir_code.decl.container.file_scope.tree,
471 .decl_ref => return &scope.cast(DeclRef).?.decl.container.file_scope.tree,471 .decl_ref => return &scope.cast(DeclRef).?.decl.container.file_scope.tree,
472 }472 }
...@@ -968,18 +968,42 @@ pub const Scope = struct {...@@ -968,18 +968,42 @@ pub const Scope = struct {
968 used: bool = false,968 used: bool = false,
969 };969 };
970970
971 /// Only valid to call on the top of the `GenZir` stack. Completes the
972 /// `WipZirCode` into a `zir.Code`. Leaves the `WipZirCode` in an
973 /// initialized, but empty, state.
974 pub fn finish(gz: *GenZir) !zir.Code {
975 const gpa = gz.zir_code.gpa;
976 const root_start = @intCast(u32, gz.zir_code.extra.items.len);
977 const root_len = @intCast(u32, gz.instructions.items.len);
978 try gz.zir_code.extra.appendSlice(gpa, gz.instructions.items);
979 return zir.Code{
980 .instructions = gz.zir_code.instructions.toOwnedSlice(),
981 .string_bytes = gz.zir_code.string_bytes.toOwnedSlice(gpa),
982 .extra = gz.zir_code.extra.toOwnedSlice(gpa),
983 .root_start = root_start,
984 .root_len = root_len,
985 };
986 }
987
988 pub fn tokSrcLoc(gz: *GenZir, token_index: ast.TokenIndex) LazySrcLoc {
989 const decl_token = gz.zir_code.decl.srcToken();
990 return .{ .token_offset = token_index - decl_token };
991 }
992
971 pub fn addFnTypeCc(gz: *GenZir, args: struct {993 pub fn addFnTypeCc(gz: *GenZir, args: struct {
972 param_types: []const zir.Inst.Ref,994 param_types: []const zir.Inst.Ref,
973 ret_ty: zir.Inst.Ref,995 ret_ty: zir.Inst.Ref,
974 cc: zir.Inst.Ref,996 cc: zir.Inst.Ref,
975 }) !zir.Inst.Index {997 }) !zir.Inst.Index {
998 assert(args.ret_ty != 0);
999 assert(args.cc != 0);
976 const gpa = gz.zir_code.gpa;1000 const gpa = gz.zir_code.gpa;
977 try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1);1001 try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1);
978 try gz.zir_code.instructions.ensureCapacity(gpa, gz.zir_code.instructions.len + 1);1002 try gz.zir_code.instructions.ensureCapacity(gpa, gz.zir_code.instructions.len + 1);
979 try gz.zir_code.extra.ensureCapacity(gpa, gz.zir_code.extra.len +1003 try gz.zir_code.extra.ensureCapacity(gpa, gz.zir_code.extra.items.len +
980 @typeInfo(zir.Inst.FnTypeCc).Struct.fields.len + args.param_types.len);1004 @typeInfo(zir.Inst.FnTypeCc).Struct.fields.len + args.param_types.len);
9811005
982 const payload_index = gz.addExtra(zir.Inst.FnTypeCc, .{1006 const payload_index = gz.zir_code.addExtra(zir.Inst.FnTypeCc{
983 .cc = args.cc,1007 .cc = args.cc,
984 .param_types_len = @intCast(u32, args.param_types.len),1008 .param_types_len = @intCast(u32, args.param_types.len),
985 }) catch unreachable; // Capacity is ensured above.1009 }) catch unreachable; // Capacity is ensured above.
...@@ -989,7 +1013,7 @@ pub const Scope = struct {...@@ -989,7 +1013,7 @@ pub const Scope = struct {
989 gz.zir_code.instructions.appendAssumeCapacity(.{1013 gz.zir_code.instructions.appendAssumeCapacity(.{
990 .tag = .fn_type_cc,1014 .tag = .fn_type_cc,
991 .data = .{ .fn_type = .{1015 .data = .{ .fn_type = .{
992 .return_type = ret_ty,1016 .return_type = args.ret_ty,
993 .payload_index = payload_index,1017 .payload_index = payload_index,
994 } },1018 } },
995 });1019 });
...@@ -1003,13 +1027,14 @@ pub const Scope = struct {...@@ -1003,13 +1027,14 @@ pub const Scope = struct {
1003 ret_ty: zir.Inst.Ref,1027 ret_ty: zir.Inst.Ref,
1004 param_types: []const zir.Inst.Ref,1028 param_types: []const zir.Inst.Ref,
1005 ) !zir.Inst.Index {1029 ) !zir.Inst.Index {
1030 assert(ret_ty != 0);
1006 const gpa = gz.zir_code.gpa;1031 const gpa = gz.zir_code.gpa;
1007 try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1);1032 try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1);
1008 try gz.zir_code.instructions.ensureCapacity(gpa, gz.zir_code.instructions.len + 1);1033 try gz.zir_code.instructions.ensureCapacity(gpa, gz.zir_code.instructions.len + 1);
1009 try gz.zir_code.extra.ensureCapacity(gpa, gz.zir_code.extra.len +1034 try gz.zir_code.extra.ensureCapacity(gpa, gz.zir_code.extra.items.len +
1010 @typeInfo(zir.Inst.FnType).Struct.fields.len + param_types.len);1035 @typeInfo(zir.Inst.FnType).Struct.fields.len + param_types.len);
10111036
1012 const payload_index = gz.addExtra(zir.Inst.FnTypeCc, .{1037 const payload_index = gz.zir_code.addExtra(zir.Inst.FnType{
1013 .param_types_len = @intCast(u32, param_types.len),1038 .param_types_len = @intCast(u32, param_types.len),
1014 }) catch unreachable; // Capacity is ensured above.1039 }) catch unreachable; // Capacity is ensured above.
1015 gz.zir_code.extra.appendSliceAssumeCapacity(param_types);1040 gz.zir_code.extra.appendSliceAssumeCapacity(param_types);
...@@ -1027,42 +1052,11 @@ pub const Scope = struct {...@@ -1027,42 +1052,11 @@ pub const Scope = struct {
1027 return result;1052 return result;
1028 }1053 }
10291054
1030 pub fn addRetTok(
1031 gz: *GenZir,
1032 operand: zir.Inst.Ref,
1033 /// Absolute token index. This function does the conversion to Decl offset.
1034 abs_tok_index: ast.TokenIndex,
1035 ) !zir.Inst.Index {
1036 const gpa = gz.zir_code.gpa;
1037 try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1);
1038 try gz.zir_code.instructions.ensureCapacity(gpa, gz.zir_code.instructions.len + 1);
1039
1040 const new_index = gz.zir_code.instructions.len;
1041 gz.zir_code.instructions.appendAssumeCapacity(.{
1042 .tag = .ret_tok,
1043 .data = .{ .fn_type = .{
1044 .operand = operand,
1045 .src_tok = abs_tok_index - gz.zir_code.decl.srcToken(),
1046 } },
1047 });
1048 const result = @intCast(zir.Inst.Ref, new_index + gz.zir_code.ref_start_index);
1049 gz.instructions.appendAssumeCapacity(result);
1050 return result;
1051 }
1052
1053 pub fn addInt(gz: *GenZir, integer: u64) !zir.Inst.Index {1055 pub fn addInt(gz: *GenZir, integer: u64) !zir.Inst.Index {
1054 const gpa = gz.zir_code.gpa;1056 return gz.add(.{
1055 try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1);
1056 try gz.zir_code.instructions.ensureCapacity(gpa, gz.zir_code.instructions.len + 1);
1057
1058 const new_index = gz.zir_code.instructions.len;
1059 gz.zir_code.instructions.appendAssumeCapacity(.{
1060 .tag = .int,1057 .tag = .int,
1061 .data = .{ .int = integer },1058 .data = .{ .int = integer },
1062 });1059 });
1063 const result = @intCast(zir.Inst.Ref, new_index + gz.zir_code.ref_start_index);
1064 gz.instructions.appendAssumeCapacity(result);
1065 return result;
1066 }1060 }
10671061
1068 pub fn addUnNode(1062 pub fn addUnNode(
...@@ -1072,21 +1066,14 @@ pub const Scope = struct {...@@ -1072,21 +1066,14 @@ pub const Scope = struct {
1072 /// Absolute node index. This function does the conversion to offset from Decl.1066 /// Absolute node index. This function does the conversion to offset from Decl.
1073 abs_node_index: ast.Node.Index,1067 abs_node_index: ast.Node.Index,
1074 ) !zir.Inst.Ref {1068 ) !zir.Inst.Ref {
1075 const gpa = gz.zir_code.gpa;1069 assert(operand != 0);
1076 try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1);1070 return gz.add(.{
1077 try gz.zir_code.instructions.ensureCapacity(gpa, gz.zir_code.instructions.len + 1);
1078
1079 const new_index = gz.zir_code.instructions.len;
1080 gz.zir_code.instructions.appendAssumeCapacity(.{
1081 .tag = tag,1071 .tag = tag,
1082 .data = .{ .un_node = .{1072 .data = .{ .un_node = .{
1083 .operand = operand,1073 .operand = operand,
1084 .src_node = abs_node_index - gz.zir_code.decl.srcNode(),1074 .src_node = abs_node_index - gz.zir_code.decl.srcNode(),
1085 } },1075 } },
1086 });1076 });
1087 const result = @intCast(zir.Inst.Ref, new_index + gz.zir_code.ref_start_index);
1088 gz.instructions.appendAssumeCapacity(result);
1089 return result;
1090 }1077 }
10911078
1092 pub fn addUnTok(1079 pub fn addUnTok(
...@@ -1096,21 +1083,14 @@ pub const Scope = struct {...@@ -1096,21 +1083,14 @@ pub const Scope = struct {
1096 /// Absolute token index. This function does the conversion to Decl offset.1083 /// Absolute token index. This function does the conversion to Decl offset.
1097 abs_tok_index: ast.TokenIndex,1084 abs_tok_index: ast.TokenIndex,
1098 ) !zir.Inst.Ref {1085 ) !zir.Inst.Ref {
1099 const gpa = gz.zir_code.gpa;1086 assert(operand != 0);
1100 try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1);1087 return gz.add(.{
1101 try gz.zir_code.instructions.ensureCapacity(gpa, gz.zir_code.instructions.len + 1);
1102
1103 const new_index = gz.zir_code.instructions.len;
1104 gz.zir_code.instructions.appendAssumeCapacity(.{
1105 .tag = tag,1088 .tag = tag,
1106 .data = .{ .un_tok = .{1089 .data = .{ .un_tok = .{
1107 .operand = operand,1090 .operand = operand,
1108 .src_tok = abs_tok_index - gz.zir_code.decl.srcToken(),1091 .src_tok = abs_tok_index - gz.zir_code.decl.srcToken(),
1109 } },1092 } },
1110 });1093 });
1111 const result = @intCast(zir.Inst.Ref, new_index + gz.zir_code.ref_start_index);
1112 gz.instructions.appendAssumeCapacity(result);
1113 return result;
1114 }1094 }
11151095
1116 pub fn addBin(1096 pub fn addBin(
...@@ -1119,18 +1099,52 @@ pub const Scope = struct {...@@ -1119,18 +1099,52 @@ pub const Scope = struct {
1119 lhs: zir.Inst.Ref,1099 lhs: zir.Inst.Ref,
1120 rhs: zir.Inst.Ref,1100 rhs: zir.Inst.Ref,
1121 ) !zir.Inst.Ref {1101 ) !zir.Inst.Ref {
1122 const gpa = gz.zir_code.gpa;1102 assert(lhs != 0);
1123 try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1);1103 assert(rhs != 0);
1124 try gz.zir_code.instructions.ensureCapacity(gpa, gz.zir_code.instructions.len + 1);1104 return gz.add(.{
1125
1126 const new_index = gz.zir_code.instructions.len;
1127 gz.zir_code.instructions.appendAssumeCapacity(.{
1128 .tag = tag,1105 .tag = tag,
1129 .data = .{ .bin = .{1106 .data = .{ .bin = .{
1130 .lhs = lhs,1107 .lhs = lhs,
1131 .rhs = rhs,1108 .rhs = rhs,
1132 } },1109 } },
1133 });1110 });
1111 }
1112
1113 pub fn addNode(
1114 gz: *GenZir,
1115 tag: zir.Inst.Tag,
1116 /// Absolute node index. This function does the conversion to offset from Decl.
1117 abs_node_index: ast.Node.Index,
1118 ) !zir.Inst.Ref {
1119 return gz.add(.{
1120 .tag = tag,
1121 .data = .{ .node = abs_node_index - gz.zir_code.decl.srcNode() },
1122 });
1123 }
1124
1125 /// Asserts that `str` is 8 or fewer bytes.
1126 pub fn addSmallStr(
1127 gz: *GenZir,
1128 tag: zir.Inst.Tag,
1129 str: []const u8,
1130 ) !zir.Inst.Ref {
1131 var buf: [9]u8 = undefined;
1132 mem.copy(u8, &buf, str);
1133 buf[str.len] = 0;
1134
1135 return gz.add(.{
1136 .tag = tag,
1137 .data = .{ .small_str = .{ .bytes = buf[0..8].* } },
1138 });
1139 }
1140
1141 fn add(gz: *GenZir, inst: zir.Inst) !zir.Inst.Ref {
1142 const gpa = gz.zir_code.gpa;
1143 try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1);
1144 try gz.zir_code.instructions.ensureCapacity(gpa, gz.zir_code.instructions.len + 1);
1145
1146 const new_index = gz.zir_code.instructions.len;
1147 gz.zir_code.instructions.appendAssumeCapacity(inst);
1134 const result = @intCast(zir.Inst.Ref, new_index + gz.zir_code.ref_start_index);1148 const result = @intCast(zir.Inst.Ref, new_index + gz.zir_code.ref_start_index);
1135 gz.instructions.appendAssumeCapacity(result);1149 gz.instructions.appendAssumeCapacity(result);
1136 return result;1150 return result;
...@@ -1183,6 +1197,7 @@ pub const Scope = struct {...@@ -1183,6 +1197,7 @@ pub const Scope = struct {
1183/// A Work-In-Progress `zir.Code`. This is a shared parent of all1197/// A Work-In-Progress `zir.Code`. This is a shared parent of all
1184/// `GenZir` scopes. Once the `zir.Code` is produced, this struct1198/// `GenZir` scopes. Once the `zir.Code` is produced, this struct
1185/// is deinitialized.1199/// is deinitialized.
1200/// The `GenZir.finish` function converts this to a `zir.Code`.
1186pub const WipZirCode = struct {1201pub const WipZirCode = struct {
1187 instructions: std.MultiArrayList(zir.Inst) = .{},1202 instructions: std.MultiArrayList(zir.Inst) = .{},
1188 string_bytes: std.ArrayListUnmanaged(u8) = .{},1203 string_bytes: std.ArrayListUnmanaged(u8) = .{},
...@@ -1194,9 +1209,20 @@ pub const WipZirCode = struct {...@@ -1194,9 +1209,20 @@ pub const WipZirCode = struct {
1194 gpa: *Allocator,1209 gpa: *Allocator,
1195 arena: *Allocator,1210 arena: *Allocator,
11961211
1197 fn deinit(wip_zir_code: *WipZirCode) void {1212 pub fn addExtra(wzc: *WipZirCode, extra: anytype) Allocator.Error!u32 {
1198 wip_zir_code.instructions.deinit(wip_zir_code.gpa);1213 const fields = std.meta.fields(@TypeOf(extra));
1199 wip_zir_code.extra.deinit(wip_zir_code.gpa);1214 try wzc.extra.ensureCapacity(wzc.gpa, wzc.extra.items.len + fields.len);
1215 const result = @intCast(u32, wzc.extra.items.len);
1216 inline for (fields) |field| {
1217 comptime assert(field.field_type == u32);
1218 wzc.extra.appendAssumeCapacity(@field(extra, field.name));
1219 }
1220 return result;
1221 }
1222
1223 pub fn deinit(wzc: *WipZirCode) void {
1224 wzc.instructions.deinit(wzc.gpa);
1225 wzc.extra.deinit(wzc.gpa);
1200 }1226 }
1201};1227};
12021228
...@@ -1763,18 +1789,22 @@ fn astgenAndSemaDecl(mod: *Module, decl: *Decl) !bool {...@@ -1763,18 +1789,22 @@ fn astgenAndSemaDecl(mod: *Module, decl: *Decl) !bool {
1763 .gpa = mod.gpa,1789 .gpa = mod.gpa,
1764 };1790 };
1765 defer wip_zir_code.deinit();1791 defer wip_zir_code.deinit();
1792
1766 var gen_scope: Scope.GenZir = .{1793 var gen_scope: Scope.GenZir = .{
1767 .force_comptime = true,1794 .force_comptime = true,
1768 .parent = &decl.container.base,1795 .parent = &decl.container.base,
1769 .zir_code = &wip_zir_code,1796 .zir_code = &wip_zir_code,
1770 };1797 };
1798 defer gen_scope.instructions.deinit(mod.gpa);
17711799
1772 const block_expr = node_datas[decl_node].lhs;1800 const block_expr = node_datas[decl_node].lhs;
1773 _ = try astgen.comptimeExpr(mod, &gen_scope.base, .none, block_expr);1801 _ = try astgen.comptimeExpr(mod, &gen_scope.base, .none, block_expr);
1802
1803 const code = try gen_scope.finish();
1774 if (std.builtin.mode == .Debug and mod.comp.verbose_ir) {1804 if (std.builtin.mode == .Debug and mod.comp.verbose_ir) {
1775 zir.dumpZir(mod.gpa, "comptime_block", decl.name, gen_scope.instructions.items) catch {};1805 zir.dumpZir(mod.gpa, "comptime_block", decl.name, code) catch {};
1776 }1806 }
1777 break :blk wip_zir_code.finish();1807 break :blk code;
1778 };1808 };
17791809
1780 var sema: Sema = .{1810 var sema: Sema = .{
...@@ -1836,11 +1866,13 @@ fn astgenAndSemaFn(...@@ -1836,11 +1866,13 @@ fn astgenAndSemaFn(
1836 .gpa = mod.gpa,1866 .gpa = mod.gpa,
1837 };1867 };
1838 defer fn_type_wip_zir_exec.deinit();1868 defer fn_type_wip_zir_exec.deinit();
1869
1839 var fn_type_scope: Scope.GenZir = .{1870 var fn_type_scope: Scope.GenZir = .{
1840 .force_comptime = true,1871 .force_comptime = true,
1841 .parent = &decl.container.base,1872 .parent = &decl.container.base,
1842 .zir_code = &fn_type_wip_zir_exec,1873 .zir_code = &fn_type_wip_zir_exec,
1843 };1874 };
1875 defer fn_type_scope.instructions.deinit(mod.gpa);
18441876
1845 decl.is_pub = fn_proto.visib_token != null;1877 decl.is_pub = fn_proto.visib_token != null;
18461878
...@@ -1855,7 +1887,7 @@ fn astgenAndSemaFn(...@@ -1855,7 +1887,7 @@ fn astgenAndSemaFn(
1855 }1887 }
1856 break :blk count;1888 break :blk count;
1857 };1889 };
1858 const param_types = try fn_type_scope_arena.allocator.alloc(zir.Inst.Index, param_count);1890 const param_types = try fn_type_scope_arena.allocator.alloc(zir.Inst.Ref, param_count);
1859 const type_type_rl: astgen.ResultLoc = .{ .ty = @enumToInt(zir.Const.type_type) };1891 const type_type_rl: astgen.ResultLoc = .{ .ty = @enumToInt(zir.Const.type_type) };
18601892
1861 var is_var_args = false;1893 var is_var_args = false;
...@@ -1970,11 +2002,11 @@ fn astgenAndSemaFn(...@@ -1970,11 +2002,11 @@ fn astgenAndSemaFn(
1970 .ty = @enumToInt(zir.Const.enum_literal_type),2002 .ty = @enumToInt(zir.Const.enum_literal_type),
1971 }, fn_proto.ast.callconv_expr)2003 }, fn_proto.ast.callconv_expr)
1972 else if (is_extern) // note: https://github.com/ziglang/zig/issues/52692004 else if (is_extern) // note: https://github.com/ziglang/zig/issues/5269
1973 try fn_type_scope.addStrBytes(.enum_literal, "C")2005 try fn_type_scope.addSmallStr(.enum_literal_small, "C")
1974 else2006 else
1975 0;2007 0;
19762008
1977 const fn_type_inst: zir.Inst.Index = if (cc != 0) fn_type: {2009 const fn_type_inst: zir.Inst.Ref = if (cc != 0) fn_type: {
1978 const tag: zir.Inst.Tag = if (is_var_args) .fn_type_cc_var_args else .fn_type_cc;2010 const tag: zir.Inst.Tag = if (is_var_args) .fn_type_cc_var_args else .fn_type_cc;
1979 break :fn_type try fn_type_scope.addFnTypeCc(.{2011 break :fn_type try fn_type_scope.addFnTypeCc(.{
1980 .ret_ty = return_type_inst,2012 .ret_ty = return_type_inst,
...@@ -1983,22 +2015,19 @@ fn astgenAndSemaFn(...@@ -1983,22 +2015,19 @@ fn astgenAndSemaFn(
1983 });2015 });
1984 } else fn_type: {2016 } else fn_type: {
1985 const tag: zir.Inst.Tag = if (is_var_args) .fn_type_var_args else .fn_type;2017 const tag: zir.Inst.Tag = if (is_var_args) .fn_type_var_args else .fn_type;
1986 break :fn_type try fn_type_scope.addFnType(.{2018 break :fn_type try fn_type_scope.addFnType(return_type_inst, param_types);
1987 .ret_ty = return_type_inst,
1988 .param_types = param_types,
1989 });
1990 };2019 };
19912020
1992 if (std.builtin.mode == .Debug and mod.comp.verbose_ir) {
1993 zir.dumpZir(mod.gpa, "fn_type", decl.name, fn_type_scope.instructions.items) catch {};
1994 }
1995
1996 // We need the memory for the Type to go into the arena for the Decl2021 // We need the memory for the Type to go into the arena for the Decl
1997 var decl_arena = std.heap.ArenaAllocator.init(mod.gpa);2022 var decl_arena = std.heap.ArenaAllocator.init(mod.gpa);
1998 errdefer decl_arena.deinit();2023 errdefer decl_arena.deinit();
1999 const decl_arena_state = try decl_arena.allocator.create(std.heap.ArenaAllocator.State);2024 const decl_arena_state = try decl_arena.allocator.create(std.heap.ArenaAllocator.State);
20002025
2001 const fn_type_code = fn_type_wip_zir_exec.finish();2026 const fn_type_code = try fn_type_scope.finish();
2027 if (std.builtin.mode == .Debug and mod.comp.verbose_ir) {
2028 zir.dumpZir(mod.gpa, "fn_type", decl.name, fn_type_code) catch {};
2029 }
2030
2002 var fn_type_sema: Sema = .{2031 var fn_type_sema: Sema = .{
2003 .mod = mod,2032 .mod = mod,
2004 .gpa = mod.gpa,2033 .gpa = mod.gpa,
...@@ -2021,7 +2050,7 @@ fn astgenAndSemaFn(...@@ -2021,7 +2050,7 @@ fn astgenAndSemaFn(
2021 };2050 };
2022 defer block_scope.instructions.deinit(mod.gpa);2051 defer block_scope.instructions.deinit(mod.gpa);
20232052
2024 const fn_type = try fn_type_sema.rootAsType(mod, &block_scope, fn_type_inst);2053 const fn_type = try fn_type_sema.rootAsType(&block_scope, fn_type_inst);
2025 if (body_node == 0) {2054 if (body_node == 0) {
2026 if (!is_extern) {2055 if (!is_extern) {
2027 return mod.failNode(&block_scope.base, fn_proto.ast.fn_token, "non-extern function has no body", .{});2056 return mod.failNode(&block_scope.base, fn_proto.ast.fn_token, "non-extern function has no body", .{});
...@@ -2063,13 +2092,12 @@ fn astgenAndSemaFn(...@@ -2063,13 +2092,12 @@ fn astgenAndSemaFn(
2063 const new_func = try decl_arena.allocator.create(Fn);2092 const new_func = try decl_arena.allocator.create(Fn);
2064 const fn_payload = try decl_arena.allocator.create(Value.Payload.Function);2093 const fn_payload = try decl_arena.allocator.create(Value.Payload.Function);
20652094
2066 const fn_zir: zir.Body = blk: {2095 const fn_zir: zir.Code = blk: {
2067 // We put the ZIR inside the Decl arena.2096 // We put the ZIR inside the Decl arena.
2068 var wip_zir_code: WipZirCode = .{2097 var wip_zir_code: WipZirCode = .{
2069 .decl = decl,2098 .decl = decl,
2070 .arena = &decl_arena.allocator,2099 .arena = &decl_arena.allocator,
2071 .gpa = mod.gpa,2100 .gpa = mod.gpa,
2072 .arg_count = param_count,
2073 };2101 };
2074 defer wip_zir_code.deinit();2102 defer wip_zir_code.deinit();
20752103
...@@ -2078,6 +2106,8 @@ fn astgenAndSemaFn(...@@ -2078,6 +2106,8 @@ fn astgenAndSemaFn(
2078 .parent = &decl.container.base,2106 .parent = &decl.container.base,
2079 .zir_code = &wip_zir_code,2107 .zir_code = &wip_zir_code,
2080 };2108 };
2109 defer gen_scope.instructions.deinit(mod.gpa);
2110
2081 // Iterate over the parameters. We put the param names as the first N2111 // Iterate over the parameters. We put the param names as the first N
2082 // items inside `extra` so that debug info later can refer to the parameter names2112 // items inside `extra` so that debug info later can refer to the parameter names
2083 // even while the respective source code is unloaded.2113 // even while the respective source code is unloaded.
...@@ -2095,7 +2125,7 @@ fn astgenAndSemaFn(...@@ -2095,7 +2125,7 @@ fn astgenAndSemaFn(
2095 .gen_zir = &gen_scope,2125 .gen_zir = &gen_scope,
2096 .name = param_name,2126 .name = param_name,
2097 // Implicit const list first, then implicit arg list.2127 // Implicit const list first, then implicit arg list.
2098 .inst = zir.const_inst_list.len + i,2128 .inst = @intCast(u32, zir.const_inst_list.len + i),
2099 };2129 };
2100 params_scope = &sub_scope.base;2130 params_scope = &sub_scope.base;
21012131
...@@ -2111,18 +2141,19 @@ fn astgenAndSemaFn(...@@ -2111,18 +2141,19 @@ fn astgenAndSemaFn(
2111 _ = try astgen.expr(mod, params_scope, .none, body_node);2141 _ = try astgen.expr(mod, params_scope, .none, body_node);
21122142
2113 if (gen_scope.instructions.items.len == 0 or2143 if (gen_scope.instructions.items.len == 0 or
2114 !gen_scope.instructions.items[gen_scope.instructions.items.len - 1].tag.isNoReturn())2144 !wip_zir_code.instructions.items(.tag)[gen_scope.instructions.items.len - 1]
2145 .isNoReturn())
2115 {2146 {
2116 _ = try gen_scope.addRetTok(@enumToInt(zir.Const.void_value), tree.lastToken(body_node));2147 const void_operand = @enumToInt(zir.Const.void_value);
2148 _ = try gen_scope.addUnTok(.ret_tok, void_operand, tree.lastToken(body_node));
2117 }2149 }
21182150
2151 const code = try gen_scope.finish();
2119 if (std.builtin.mode == .Debug and mod.comp.verbose_ir) {2152 if (std.builtin.mode == .Debug and mod.comp.verbose_ir) {
2120 zir.dumpZir(mod.gpa, "fn_body", decl.name, gen_scope.instructions.items) catch {};2153 zir.dumpZir(mod.gpa, "fn_body", decl.name, code) catch {};
2121 }2154 }
21222155
2123 break :blk .{2156 break :blk code;
2124 .instructions = try gen_scope.arena.dupe(*zir.Inst, gen_scope.instructions.items),
2125 };
2126 };2157 };
21272158
2128 const is_inline = fn_type.fnCallingConvention() == .Inline;2159 const is_inline = fn_type.fnCallingConvention() == .Inline;
...@@ -2190,7 +2221,8 @@ fn astgenAndSemaFn(...@@ -2190,7 +2221,8 @@ fn astgenAndSemaFn(
2190 .{},2221 .{},
2191 );2222 );
2192 }2223 }
2193 const export_src = token_starts[maybe_export_token];2224 // TODO use a Decl-local source location instead.
2225 const export_src: LazySrcLoc = .{ .token_abs = maybe_export_token };
2194 const name = tree.tokenSlice(fn_proto.name_token.?); // TODO identifierTokenString2226 const name = tree.tokenSlice(fn_proto.name_token.?); // TODO identifierTokenString
2195 // The scope needs to have the decl in it.2227 // The scope needs to have the decl in it.
2196 try mod.analyzeExport(&block_scope.base, export_src, name, decl);2228 try mod.analyzeExport(&block_scope.base, export_src, name, decl);
...@@ -2294,7 +2326,7 @@ fn astgenAndSemaVarDecl(...@@ -2294,7 +2326,7 @@ fn astgenAndSemaVarDecl(
2294 init_result_loc,2326 init_result_loc,
2295 var_decl.ast.init_node,2327 var_decl.ast.init_node,
2296 );2328 );
2297 const code = wip_zir_code.finish();2329 const code = try gen_scope.finish();
2298 if (std.builtin.mode == .Debug and mod.comp.verbose_ir) {2330 if (std.builtin.mode == .Debug and mod.comp.verbose_ir) {
2299 zir.dumpZir(mod.gpa, "var_init", decl.name, code) catch {};2331 zir.dumpZir(mod.gpa, "var_init", decl.name, code) catch {};
2300 }2332 }
...@@ -2324,13 +2356,13 @@ fn astgenAndSemaVarDecl(...@@ -2324,13 +2356,13 @@ fn astgenAndSemaVarDecl(
2324 try sema.root(&block_scope);2356 try sema.root(&block_scope);
23252357
2326 // The result location guarantees the type coercion.2358 // The result location guarantees the type coercion.
2327 const analyzed_init_inst = sema.resolveInst(&block_scope, init_inst);2359 const analyzed_init_inst = try sema.resolveInst(init_inst);
2328 // The is_comptime in the Scope.Block guarantees the result is comptime-known.2360 // The is_comptime in the Scope.Block guarantees the result is comptime-known.
2329 const val = analyzed_init_inst.value().?;2361 const val = analyzed_init_inst.value().?;
23302362
2331 break :vi .{2363 break :vi .{
2332 .ty = try analyzed_init_inst.ty.copy(decl_arena),2364 .ty = try analyzed_init_inst.ty.copy(&decl_arena.allocator),
2333 .val = try val.copy(decl_arena),2365 .val = try val.copy(&decl_arena.allocator),
2334 };2366 };
2335 } else if (!is_extern) {2367 } else if (!is_extern) {
2336 return mod.failTok(2368 return mod.failTok(
...@@ -2358,7 +2390,7 @@ fn astgenAndSemaVarDecl(...@@ -2358,7 +2390,7 @@ fn astgenAndSemaVarDecl(
2358 defer type_scope.instructions.deinit(mod.gpa);2390 defer type_scope.instructions.deinit(mod.gpa);
23592391
2360 const var_type = try astgen.typeExpr(mod, &type_scope.base, var_decl.ast.type_node);2392 const var_type = try astgen.typeExpr(mod, &type_scope.base, var_decl.ast.type_node);
2361 const code = wip_zir_code.finish();2393 const code = try type_scope.finish();
2362 if (std.builtin.mode == .Debug and mod.comp.verbose_ir) {2394 if (std.builtin.mode == .Debug and mod.comp.verbose_ir) {
2363 zir.dumpZir(mod.gpa, "var_type", decl.name, code) catch {};2395 zir.dumpZir(mod.gpa, "var_type", decl.name, code) catch {};
2364 }2396 }
...@@ -2388,7 +2420,7 @@ fn astgenAndSemaVarDecl(...@@ -2388,7 +2420,7 @@ fn astgenAndSemaVarDecl(
2388 const ty = try sema.rootAsType(&block_scope, var_type);2420 const ty = try sema.rootAsType(&block_scope, var_type);
23892421
2390 break :vi .{2422 break :vi .{
2391 .ty = try ty.copy(decl_arena),2423 .ty = try ty.copy(&decl_arena.allocator),
2392 .val = null,2424 .val = null,
2393 };2425 };
2394 } else {2426 } else {
...@@ -2441,7 +2473,8 @@ fn astgenAndSemaVarDecl(...@@ -2441,7 +2473,8 @@ fn astgenAndSemaVarDecl(
24412473
2442 if (var_decl.extern_export_token) |maybe_export_token| {2474 if (var_decl.extern_export_token) |maybe_export_token| {
2443 if (token_tags[maybe_export_token] == .keyword_export) {2475 if (token_tags[maybe_export_token] == .keyword_export) {
2444 const export_src = token_starts[maybe_export_token];2476 // TODO make this src relative to containing Decl
2477 const export_src: LazySrcLoc = .{ .token_abs = maybe_export_token };
2445 const name_token = var_decl.ast.mut_token + 1;2478 const name_token = var_decl.ast.mut_token + 1;
2446 const name = tree.tokenSlice(name_token); // TODO identifierTokenString2479 const name = tree.tokenSlice(name_token); // TODO identifierTokenString
2447 // The scope needs to have the decl in it.2480 // The scope needs to have the decl in it.
src/Sema.zig+17-8
...@@ -12,7 +12,7 @@ gpa: *Allocator,...@@ -12,7 +12,7 @@ gpa: *Allocator,
12arena: *Allocator,12arena: *Allocator,
13code: zir.Code,13code: zir.Code,
14/// Maps ZIR to TZIR.14/// Maps ZIR to TZIR.
15inst_map: []*const Inst,15inst_map: []*Inst,
16/// When analyzing an inline function call, owner_decl is the Decl of the caller16/// When analyzing an inline function call, owner_decl is the Decl of the caller
17/// and `src_decl` of `Scope.Block` is the `Decl` of the callee.17/// and `src_decl` of `Scope.Block` is the `Decl` of the callee.
18/// This `Decl` owns the arena memory of this `Sema`.18/// This `Decl` owns the arena memory of this `Sema`.
...@@ -58,15 +58,10 @@ pub fn root(sema: *Sema, root_block: *Scope.Block) !void {...@@ -58,15 +58,10 @@ pub fn root(sema: *Sema, root_block: *Scope.Block) !void {
58 return sema.analyzeBody(root_block, root_body);58 return sema.analyzeBody(root_block, root_body);
59}59}
6060
61pub fn rootAsType(61pub fn rootAsType(sema: *Sema, root_block: *Scope.Block, result_inst: zir.Inst.Ref) !Type {
62 sema: *Sema,
63 root_block: *Scope.Block,
64 zir_result_inst: zir.Inst.Index,
65) !Type {
66 const root_body = sema.code.extra[sema.code.root_start..][0..sema.code.root_len];62 const root_body = sema.code.extra[sema.code.root_start..][0..sema.code.root_len];
67 try sema.analyzeBody(root_block, root_body);63 try sema.analyzeBody(root_block, root_body);
6864
69 const result_inst = sema.inst_map[zir_result_inst];
70 // Source location is unneeded because resolveConstValue must have already65 // Source location is unneeded because resolveConstValue must have already
71 // been successfully called when coercing the value to a type, from the66 // been successfully called when coercing the value to a type, from the
72 // result location.67 // result location.
...@@ -203,6 +198,7 @@ pub fn analyzeBody(sema: *Sema, block: *Scope.Block, body: []const zir.Inst.Inde...@@ -203,6 +198,7 @@ pub fn analyzeBody(sema: *Sema, block: *Scope.Block, body: []const zir.Inst.Inde
203 .array_type => try sema.zirArrayType(block, zir_inst),198 .array_type => try sema.zirArrayType(block, zir_inst),
204 .array_type_sentinel => try sema.zirArrayTypeSentinel(block, zir_inst),199 .array_type_sentinel => try sema.zirArrayTypeSentinel(block, zir_inst),
205 .enum_literal => try sema.zirEnumLiteral(block, zir_inst),200 .enum_literal => try sema.zirEnumLiteral(block, zir_inst),
201 .enum_literal_small => try sema.zirEnumLiteralSmall(block, zir_inst),
206 .merge_error_sets => try sema.zirMergeErrorSets(block, zir_inst),202 .merge_error_sets => try sema.zirMergeErrorSets(block, zir_inst),
207 .error_union_type => try sema.zirErrorUnionType(block, zir_inst),203 .error_union_type => try sema.zirErrorUnionType(block, zir_inst),
208 .anyframe_type => try sema.zirAnyframeType(block, zir_inst),204 .anyframe_type => try sema.zirAnyframeType(block, zir_inst),
...@@ -232,7 +228,7 @@ pub fn analyzeBody(sema: *Sema, block: *Scope.Block, body: []const zir.Inst.Inde...@@ -232,7 +228,7 @@ pub fn analyzeBody(sema: *Sema, block: *Scope.Block, body: []const zir.Inst.Inde
232228
233/// TODO when we rework TZIR memory layout, this function will no longer have a possible error.229/// TODO when we rework TZIR memory layout, this function will no longer have a possible error.
234pub fn resolveInst(sema: *Sema, zir_ref: zir.Inst.Ref) error{OutOfMemory}!*ir.Inst {230pub fn resolveInst(sema: *Sema, zir_ref: zir.Inst.Ref) error{OutOfMemory}!*ir.Inst {
235 var i = zir_ref;231 var i: usize = zir_ref;
236232
237 // First section of indexes correspond to a set number of constant values.233 // First section of indexes correspond to a set number of constant values.
238 if (i < zir.const_inst_list.len) {234 if (i < zir.const_inst_list.len) {
...@@ -1435,6 +1431,19 @@ fn zirEnumLiteral(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerE...@@ -1435,6 +1431,19 @@ fn zirEnumLiteral(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerE
1435 });1431 });
1436}1432}
14371433
1434fn zirEnumLiteralSmall(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {
1435 const tracy = trace(@src());
1436 defer tracy.end();
1437
1438 const name = sema.code.instructions.items(.data)[inst].small_str.get();
1439 const src: LazySrcLoc = .unneeded;
1440 const duped_name = try sema.arena.dupe(u8, name);
1441 return sema.mod.constInst(sema.arena, src, .{
1442 .ty = Type.initTag(.enum_literal),
1443 .val = try Value.Tag.enum_literal.create(sema.arena, duped_name),
1444 });
1445}
1446
1438/// Pointer in, pointer out.1447/// Pointer in, pointer out.
1439fn zirOptionalPayloadPtr(1448fn zirOptionalPayloadPtr(
1440 sema: *Sema,1449 sema: *Sema,
src/astgen.zig+219-343
...@@ -58,20 +58,14 @@ pub const ResultLoc = union(enum) {...@@ -58,20 +58,14 @@ pub const ResultLoc = union(enum) {
58 };58 };
59};59};
6060
61pub fn typeExpr(mod: *Module, scope: *Scope, type_node: ast.Node.Index) InnerError!*zir.Inst {61const void_inst: zir.Inst.Ref = @enumToInt(zir.Const.void_value);
62 const tree = scope.tree();
63 const token_starts = tree.tokens.items(.start);
6462
65 const type_src = token_starts[tree.firstToken(type_node)];63pub fn typeExpr(mod: *Module, scope: *Scope, type_node: ast.Node.Index) InnerError!zir.Inst.Ref {
66 const type_type = try addZIRInstConst(mod, scope, type_src, .{64 const type_rl: ResultLoc = .{ .ty = @enumToInt(zir.Const.type_type) };
67 .ty = Type.initTag(.type),
68 .val = Value.initTag(.type_type),
69 });
70 const type_rl: ResultLoc = .{ .ty = type_type };
71 return expr(mod, scope, type_rl, type_node);65 return expr(mod, scope, type_rl, type_node);
72}66}
7367
74fn lvalExpr(mod: *Module, scope: *Scope, node: ast.Node.Index) InnerError!*zir.Inst {68fn lvalExpr(mod: *Module, scope: *Scope, node: ast.Node.Index) InnerError!zir.Inst.Ref {
75 const tree = scope.tree();69 const tree = scope.tree();
76 const node_tags = tree.nodes.items(.tag);70 const node_tags = tree.nodes.items(.tag);
77 const main_tokens = tree.nodes.items(.main_token);71 const main_tokens = tree.nodes.items(.main_token);
...@@ -265,7 +259,7 @@ fn lvalExpr(mod: *Module, scope: *Scope, node: ast.Node.Index) InnerError!*zir.I...@@ -265,7 +259,7 @@ fn lvalExpr(mod: *Module, scope: *Scope, node: ast.Node.Index) InnerError!*zir.I
265/// When `rl` is discard, ptr, inferred_ptr, bitcasted_ptr, or inferred_ptr, the259/// When `rl` is discard, ptr, inferred_ptr, bitcasted_ptr, or inferred_ptr, the
266/// result instruction can be used to inspect whether it is isNoReturn() but that is it,260/// result instruction can be used to inspect whether it is isNoReturn() but that is it,
267/// it must otherwise not be used.261/// it must otherwise not be used.
268pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) InnerError!*zir.Inst {262pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) InnerError!zir.Inst.Ref {
269 const tree = scope.tree();263 const tree = scope.tree();
270 const main_tokens = tree.nodes.items(.main_token);264 const main_tokens = tree.nodes.items(.main_token);
271 const token_tags = tree.tokens.items(.tag);265 const token_tags = tree.tokens.items(.tag);
...@@ -294,20 +288,62 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In...@@ -294,20 +288,62 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In
294 .asm_output => unreachable, // Handled in `asmExpr`.288 .asm_output => unreachable, // Handled in `asmExpr`.
295 .asm_input => unreachable, // Handled in `asmExpr`.289 .asm_input => unreachable, // Handled in `asmExpr`.
296290
297 .assign => return rvalueVoid(mod, scope, rl, node, try assign(mod, scope, node)),291 .assign => {
298 .assign_bit_and => return rvalueVoid(mod, scope, rl, node, try assignOp(mod, scope, node, .bit_and)),292 try assign(mod, scope, node);
299 .assign_bit_or => return rvalueVoid(mod, scope, rl, node, try assignOp(mod, scope, node, .bit_or)),293 return rvalue(mod, scope, rl, void_inst, node);
300 .assign_bit_shift_left => return rvalueVoid(mod, scope, rl, node, try assignOp(mod, scope, node, .shl)),294 },
301 .assign_bit_shift_right => return rvalueVoid(mod, scope, rl, node, try assignOp(mod, scope, node, .shr)),295 .assign_bit_and => {
302 .assign_bit_xor => return rvalueVoid(mod, scope, rl, node, try assignOp(mod, scope, node, .xor)),296 try assignOp(mod, scope, node, .bit_and);
303 .assign_div => return rvalueVoid(mod, scope, rl, node, try assignOp(mod, scope, node, .div)),297 return rvalue(mod, scope, rl, void_inst, node);
304 .assign_sub => return rvalueVoid(mod, scope, rl, node, try assignOp(mod, scope, node, .sub)),298 },
305 .assign_sub_wrap => return rvalueVoid(mod, scope, rl, node, try assignOp(mod, scope, node, .subwrap)),299 .assign_bit_or => {
306 .assign_mod => return rvalueVoid(mod, scope, rl, node, try assignOp(mod, scope, node, .mod_rem)),300 try assignOp(mod, scope, node, .bit_or);
307 .assign_add => return rvalueVoid(mod, scope, rl, node, try assignOp(mod, scope, node, .add)),301 return rvalue(mod, scope, rl, void_inst, node);
308 .assign_add_wrap => return rvalueVoid(mod, scope, rl, node, try assignOp(mod, scope, node, .addwrap)),302 },
309 .assign_mul => return rvalueVoid(mod, scope, rl, node, try assignOp(mod, scope, node, .mul)),303 .assign_bit_shift_left => {
310 .assign_mul_wrap => return rvalueVoid(mod, scope, rl, node, try assignOp(mod, scope, node, .mulwrap)),304 try assignOp(mod, scope, node, .shl);
305 return rvalue(mod, scope, rl, void_inst, node);
306 },
307 .assign_bit_shift_right => {
308 try assignOp(mod, scope, node, .shr);
309 return rvalue(mod, scope, rl, void_inst, node);
310 },
311 .assign_bit_xor => {
312 try assignOp(mod, scope, node, .xor);
313 return rvalue(mod, scope, rl, void_inst, node);
314 },
315 .assign_div => {
316 try assignOp(mod, scope, node, .div);
317 return rvalue(mod, scope, rl, void_inst, node);
318 },
319 .assign_sub => {
320 try assignOp(mod, scope, node, .sub);
321 return rvalue(mod, scope, rl, void_inst, node);
322 },
323 .assign_sub_wrap => {
324 try assignOp(mod, scope, node, .subwrap);
325 return rvalue(mod, scope, rl, void_inst, node);
326 },
327 .assign_mod => {
328 try assignOp(mod, scope, node, .mod_rem);
329 return rvalue(mod, scope, rl, void_inst, node);
330 },
331 .assign_add => {
332 try assignOp(mod, scope, node, .add);
333 return rvalue(mod, scope, rl, void_inst, node);
334 },
335 .assign_add_wrap => {
336 try assignOp(mod, scope, node, .addwrap);
337 return rvalue(mod, scope, rl, void_inst, node);
338 },
339 .assign_mul => {
340 try assignOp(mod, scope, node, .mul);
341 return rvalue(mod, scope, rl, void_inst, node);
342 },
343 .assign_mul_wrap => {
344 try assignOp(mod, scope, node, .mulwrap);
345 return rvalue(mod, scope, rl, void_inst, node);
346 },
311347
312 .add => return simpleBinOp(mod, scope, rl, node, .add),348 .add => return simpleBinOp(mod, scope, rl, node, .add),
313 .add_wrap => return simpleBinOp(mod, scope, rl, node, .addwrap),349 .add_wrap => return simpleBinOp(mod, scope, rl, node, .addwrap),
...@@ -336,10 +372,14 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In...@@ -336,10 +372,14 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In
336 .bool_and => return boolBinOp(mod, scope, rl, node, true),372 .bool_and => return boolBinOp(mod, scope, rl, node, true),
337 .bool_or => return boolBinOp(mod, scope, rl, node, false),373 .bool_or => return boolBinOp(mod, scope, rl, node, false),
338374
339 .bool_not => return rvalue(mod, scope, rl, try boolNot(mod, scope, node)),375 .bool_not => @panic("TODO"),
340 .bit_not => return rvalue(mod, scope, rl, try bitNot(mod, scope, node)),376 .bit_not => @panic("TODO"),
341 .negation => return rvalue(mod, scope, rl, try negation(mod, scope, node, .sub)),377 .negation => @panic("TODO"),
342 .negation_wrap => return rvalue(mod, scope, rl, try negation(mod, scope, node, .subwrap)),378 .negation_wrap => @panic("TODO"),
379 //.bool_not => return rvalue(mod, scope, rl, try boolNot(mod, scope, node)),
380 //.bit_not => return rvalue(mod, scope, rl, try bitNot(mod, scope, node)),
381 //.negation => return rvalue(mod, scope, rl, try negation(mod, scope, node, .sub)),
382 //.negation_wrap => return rvalue(mod, scope, rl, try negation(mod, scope, node, .subwrap)),
343383
344 .identifier => return identifier(mod, scope, rl, node),384 .identifier => return identifier(mod, scope, rl, node),
345385
...@@ -377,6 +417,7 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In...@@ -377,6 +417,7 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In
377 },417 },
378418
379 .unreachable_literal => {419 .unreachable_literal => {
420 if (true) @panic("TODO update for zir-memory-layout");
380 const main_token = main_tokens[node];421 const main_token = main_tokens[node];
381 const src = token_starts[main_token];422 const src = token_starts[main_token];
382 return addZIRNoOp(mod, scope, src, .unreachable_safe);423 return addZIRNoOp(mod, scope, src, .unreachable_safe);
...@@ -402,16 +443,19 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In...@@ -402,16 +443,19 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In
402 .slice_sentinel => return sliceExpr(mod, scope, rl, tree.sliceSentinel(node)),443 .slice_sentinel => return sliceExpr(mod, scope, rl, tree.sliceSentinel(node)),
403444
404 .deref => {445 .deref => {
446 if (true) @panic("TODO update for zir-memory-layout");
405 const lhs = try expr(mod, scope, .none, node_datas[node].lhs);447 const lhs = try expr(mod, scope, .none, node_datas[node].lhs);
406 const src = token_starts[main_tokens[node]];448 const src = token_starts[main_tokens[node]];
407 const result = try addZIRUnOp(mod, scope, src, .deref, lhs);449 const result = try addZIRUnOp(mod, scope, src, .deref, lhs);
408 return rvalue(mod, scope, rl, result);450 return rvalue(mod, scope, rl, result);
409 },451 },
410 .address_of => {452 .address_of => {
453 if (true) @panic("TODO update for zir-memory-layout");
411 const result = try expr(mod, scope, .ref, node_datas[node].lhs);454 const result = try expr(mod, scope, .ref, node_datas[node].lhs);
412 return rvalue(mod, scope, rl, result);455 return rvalue(mod, scope, rl, result);
413 },456 },
414 .undefined_literal => {457 .undefined_literal => {
458 if (true) @panic("TODO update for zir-memory-layout");
415 const main_token = main_tokens[node];459 const main_token = main_tokens[node];
416 const src = token_starts[main_token];460 const src = token_starts[main_token];
417 const result = try addZIRInstConst(mod, scope, src, .{461 const result = try addZIRInstConst(mod, scope, src, .{
...@@ -421,6 +465,7 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In...@@ -421,6 +465,7 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In
421 return rvalue(mod, scope, rl, result);465 return rvalue(mod, scope, rl, result);
422 },466 },
423 .true_literal => {467 .true_literal => {
468 if (true) @panic("TODO update for zir-memory-layout");
424 const main_token = main_tokens[node];469 const main_token = main_tokens[node];
425 const src = token_starts[main_token];470 const src = token_starts[main_token];
426 const result = try addZIRInstConst(mod, scope, src, .{471 const result = try addZIRInstConst(mod, scope, src, .{
...@@ -430,6 +475,7 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In...@@ -430,6 +475,7 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In
430 return rvalue(mod, scope, rl, result);475 return rvalue(mod, scope, rl, result);
431 },476 },
432 .false_literal => {477 .false_literal => {
478 if (true) @panic("TODO update for zir-memory-layout");
433 const main_token = main_tokens[node];479 const main_token = main_tokens[node];
434 const src = token_starts[main_token];480 const src = token_starts[main_token];
435 const result = try addZIRInstConst(mod, scope, src, .{481 const result = try addZIRInstConst(mod, scope, src, .{
...@@ -439,6 +485,7 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In...@@ -439,6 +485,7 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In
439 return rvalue(mod, scope, rl, result);485 return rvalue(mod, scope, rl, result);
440 },486 },
441 .null_literal => {487 .null_literal => {
488 if (true) @panic("TODO update for zir-memory-layout");
442 const main_token = main_tokens[node];489 const main_token = main_tokens[node];
443 const src = token_starts[main_token];490 const src = token_starts[main_token];
444 const result = try addZIRInstConst(mod, scope, src, .{491 const result = try addZIRInstConst(mod, scope, src, .{
...@@ -448,12 +495,14 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In...@@ -448,12 +495,14 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In
448 return rvalue(mod, scope, rl, result);495 return rvalue(mod, scope, rl, result);
449 },496 },
450 .optional_type => {497 .optional_type => {
498 if (true) @panic("TODO update for zir-memory-layout");
451 const src = token_starts[main_tokens[node]];499 const src = token_starts[main_tokens[node]];
452 const operand = try typeExpr(mod, scope, node_datas[node].lhs);500 const operand = try typeExpr(mod, scope, node_datas[node].lhs);
453 const result = try addZIRUnOp(mod, scope, src, .optional_type, operand);501 const result = try addZIRUnOp(mod, scope, src, .optional_type, operand);
454 return rvalue(mod, scope, rl, result);502 return rvalue(mod, scope, rl, result);
455 },503 },
456 .unwrap_optional => {504 .unwrap_optional => {
505 if (true) @panic("TODO update for zir-memory-layout");
457 const src = token_starts[main_tokens[node]];506 const src = token_starts[main_tokens[node]];
458 switch (rl) {507 switch (rl) {
459 .ref => return addZIRUnOp(508 .ref => return addZIRUnOp(
...@@ -473,6 +522,7 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In...@@ -473,6 +522,7 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In
473 }522 }
474 },523 },
475 .block_two, .block_two_semicolon => {524 .block_two, .block_two_semicolon => {
525 if (true) @panic("TODO update for zir-memory-layout");
476 const statements = [2]ast.Node.Index{ node_datas[node].lhs, node_datas[node].rhs };526 const statements = [2]ast.Node.Index{ node_datas[node].lhs, node_datas[node].rhs };
477 if (node_datas[node].lhs == 0) {527 if (node_datas[node].lhs == 0) {
478 return blockExpr(mod, scope, rl, node, statements[0..0]);528 return blockExpr(mod, scope, rl, node, statements[0..0]);
...@@ -483,10 +533,12 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In...@@ -483,10 +533,12 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In
483 }533 }
484 },534 },
485 .block, .block_semicolon => {535 .block, .block_semicolon => {
536 if (true) @panic("TODO update for zir-memory-layout");
486 const statements = tree.extra_data[node_datas[node].lhs..node_datas[node].rhs];537 const statements = tree.extra_data[node_datas[node].lhs..node_datas[node].rhs];
487 return blockExpr(mod, scope, rl, node, statements);538 return blockExpr(mod, scope, rl, node, statements);
488 },539 },
489 .enum_literal => {540 .enum_literal => {
541 if (true) @panic("TODO update for zir-memory-layout");
490 const ident_token = main_tokens[node];542 const ident_token = main_tokens[node];
491 const gen_zir = scope.getGenZir();543 const gen_zir = scope.getGenZir();
492 const string_bytes = &gen_zir.zir_exec.string_bytes;544 const string_bytes = &gen_zir.zir_exec.string_bytes;
...@@ -497,6 +549,7 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In...@@ -497,6 +549,7 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In
497 return rvalue(mod, scope, rl, result);549 return rvalue(mod, scope, rl, result);
498 },550 },
499 .error_value => {551 .error_value => {
552 if (true) @panic("TODO update for zir-memory-layout");
500 const ident_token = node_datas[node].rhs;553 const ident_token = node_datas[node].rhs;
501 const name = try mod.identifierTokenString(scope, ident_token);554 const name = try mod.identifierTokenString(scope, ident_token);
502 const src = token_starts[ident_token];555 const src = token_starts[ident_token];
...@@ -504,6 +557,7 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In...@@ -504,6 +557,7 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In
504 return rvalue(mod, scope, rl, result);557 return rvalue(mod, scope, rl, result);
505 },558 },
506 .error_union => {559 .error_union => {
560 if (true) @panic("TODO update for zir-memory-layout");
507 const error_set = try typeExpr(mod, scope, node_datas[node].lhs);561 const error_set = try typeExpr(mod, scope, node_datas[node].lhs);
508 const payload = try typeExpr(mod, scope, node_datas[node].rhs);562 const payload = try typeExpr(mod, scope, node_datas[node].rhs);
509 const src = token_starts[main_tokens[node]];563 const src = token_starts[main_tokens[node]];
...@@ -511,6 +565,7 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In...@@ -511,6 +565,7 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In
511 return rvalue(mod, scope, rl, result);565 return rvalue(mod, scope, rl, result);
512 },566 },
513 .merge_error_sets => {567 .merge_error_sets => {
568 if (true) @panic("TODO update for zir-memory-layout");
514 const lhs = try typeExpr(mod, scope, node_datas[node].lhs);569 const lhs = try typeExpr(mod, scope, node_datas[node].lhs);
515 const rhs = try typeExpr(mod, scope, node_datas[node].rhs);570 const rhs = try typeExpr(mod, scope, node_datas[node].rhs);
516 const src = token_starts[main_tokens[node]];571 const src = token_starts[main_tokens[node]];
...@@ -518,6 +573,7 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In...@@ -518,6 +573,7 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In
518 return rvalue(mod, scope, rl, result);573 return rvalue(mod, scope, rl, result);
519 },574 },
520 .anyframe_literal => {575 .anyframe_literal => {
576 if (true) @panic("TODO update for zir-memory-layout");
521 const main_token = main_tokens[node];577 const main_token = main_tokens[node];
522 const src = token_starts[main_token];578 const src = token_starts[main_token];
523 const result = try addZIRInstConst(mod, scope, src, .{579 const result = try addZIRInstConst(mod, scope, src, .{
...@@ -527,12 +583,14 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In...@@ -527,12 +583,14 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In
527 return rvalue(mod, scope, rl, result);583 return rvalue(mod, scope, rl, result);
528 },584 },
529 .anyframe_type => {585 .anyframe_type => {
586 if (true) @panic("TODO update for zir-memory-layout");
530 const src = token_starts[node_datas[node].lhs];587 const src = token_starts[node_datas[node].lhs];
531 const return_type = try typeExpr(mod, scope, node_datas[node].rhs);588 const return_type = try typeExpr(mod, scope, node_datas[node].rhs);
532 const result = try addZIRUnOp(mod, scope, src, .anyframe_type, return_type);589 const result = try addZIRUnOp(mod, scope, src, .anyframe_type, return_type);
533 return rvalue(mod, scope, rl, result);590 return rvalue(mod, scope, rl, result);
534 },591 },
535 .@"catch" => {592 .@"catch" => {
593 if (true) @panic("TODO update for zir-memory-layout");
536 const catch_token = main_tokens[node];594 const catch_token = main_tokens[node];
537 const payload_token: ?ast.TokenIndex = if (token_tags[catch_token + 1] == .pipe)595 const payload_token: ?ast.TokenIndex = if (token_tags[catch_token + 1] == .pipe)
538 catch_token + 2596 catch_token + 2
...@@ -631,9 +689,11 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In...@@ -631,9 +689,11 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In
631 .@"switch", .switch_comma => return switchExpr(mod, scope, rl, node),689 .@"switch", .switch_comma => return switchExpr(mod, scope, rl, node),
632690
633 .@"nosuspend" => return nosuspendExpr(mod, scope, rl, node),691 .@"nosuspend" => return nosuspendExpr(mod, scope, rl, node),
634 .@"suspend" => return rvalue(mod, scope, rl, try suspendExpr(mod, scope, node)),692 .@"suspend" => @panic("TODO"),
693 //.@"suspend" => return rvalue(mod, scope, rl, try suspendExpr(mod, scope, node)),
635 .@"await" => return awaitExpr(mod, scope, rl, node),694 .@"await" => return awaitExpr(mod, scope, rl, node),
636 .@"resume" => return rvalue(mod, scope, rl, try resumeExpr(mod, scope, node)),695 .@"resume" => @panic("TODO"),
696 //.@"resume" => return rvalue(mod, scope, rl, try resumeExpr(mod, scope, node)),
637697
638 .@"defer" => return mod.failNode(scope, node, "TODO implement astgen.expr for .defer", .{}),698 .@"defer" => return mod.failNode(scope, node, "TODO implement astgen.expr for .defer", .{}),
639 .@"errdefer" => return mod.failNode(scope, node, "TODO implement astgen.expr for .errdefer", .{}),699 .@"errdefer" => return mod.failNode(scope, node, "TODO implement astgen.expr for .errdefer", .{}),
...@@ -673,20 +733,22 @@ pub fn comptimeExpr(...@@ -673,20 +733,22 @@ pub fn comptimeExpr(
673 parent_scope: *Scope,733 parent_scope: *Scope,
674 rl: ResultLoc,734 rl: ResultLoc,
675 node: ast.Node.Index,735 node: ast.Node.Index,
676) InnerError!*zir.Inst {736) InnerError!zir.Inst.Ref {
737 if (true) @panic("TODO update for zir-memory-layout branch");
738
677 // If we are already in a comptime scope, no need to make another one.739 // If we are already in a comptime scope, no need to make another one.
678 if (parent_scope.isComptime()) {740 if (parent_scope.isComptime()) {
679 return expr(mod, parent_scope, rl, node);741 return expr(mod, parent_scope, rl, node);
680 }742 }
681743
744 const gz = parent_scope.getGenZir();
682 const tree = parent_scope.tree();745 const tree = parent_scope.tree();
683 const token_starts = tree.tokens.items(.start);746 const token_starts = tree.tokens.items(.start);
684747
685 // Make a scope to collect generated instructions in the sub-expression.748 // Make a scope to collect generated instructions in the sub-expression.
686 var block_scope: Scope.GenZir = .{749 var block_scope: Scope.GenZir = .{
687 .parent = parent_scope,750 .parent = parent_scope,
688 .decl = parent_scope.ownerDecl().?,751 .zir_code = gz.zir_code,
689 .arena = parent_scope.arena(),
690 .force_comptime = true,752 .force_comptime = true,
691 .instructions = .{},753 .instructions = .{},
692 };754 };
...@@ -698,7 +760,7 @@ pub fn comptimeExpr(...@@ -698,7 +760,7 @@ pub fn comptimeExpr(
698760
699 const src = token_starts[tree.firstToken(node)];761 const src = token_starts[tree.firstToken(node)];
700 const block = try addZIRInstBlock(mod, parent_scope, src, .block_comptime_flat, .{762 const block = try addZIRInstBlock(mod, parent_scope, src, .block_comptime_flat, .{
701 .instructions = try block_scope.arena.dupe(*zir.Inst, block_scope.instructions.items),763 .instructions = try block_scope.arena.dupe(zir.Inst.Ref, block_scope.instructions.items),
702 });764 });
703765
704 return &block.base;766 return &block.base;
...@@ -709,7 +771,8 @@ fn breakExpr(...@@ -709,7 +771,8 @@ fn breakExpr(
709 parent_scope: *Scope,771 parent_scope: *Scope,
710 rl: ResultLoc,772 rl: ResultLoc,
711 node: ast.Node.Index,773 node: ast.Node.Index,
712) InnerError!*zir.Inst {774) InnerError!zir.Inst.Ref {
775 if (true) @panic("TODO update for zir-memory-layout");
713 const tree = parent_scope.tree();776 const tree = parent_scope.tree();
714 const node_datas = tree.nodes.items(.data);777 const node_datas = tree.nodes.items(.data);
715 const main_tokens = tree.nodes.items(.main_token);778 const main_tokens = tree.nodes.items(.main_token);
...@@ -787,7 +850,8 @@ fn continueExpr(...@@ -787,7 +850,8 @@ fn continueExpr(
787 parent_scope: *Scope,850 parent_scope: *Scope,
788 rl: ResultLoc,851 rl: ResultLoc,
789 node: ast.Node.Index,852 node: ast.Node.Index,
790) InnerError!*zir.Inst {853) InnerError!zir.Inst.Ref {
854 if (true) @panic("TODO update for zir-memory-layout");
791 const tree = parent_scope.tree();855 const tree = parent_scope.tree();
792 const node_datas = tree.nodes.items(.data);856 const node_datas = tree.nodes.items(.data);
793 const main_tokens = tree.nodes.items(.main_token);857 const main_tokens = tree.nodes.items(.main_token);
...@@ -843,7 +907,7 @@ pub fn blockExpr(...@@ -843,7 +907,7 @@ pub fn blockExpr(
843 rl: ResultLoc,907 rl: ResultLoc,
844 block_node: ast.Node.Index,908 block_node: ast.Node.Index,
845 statements: []const ast.Node.Index,909 statements: []const ast.Node.Index,
846) InnerError!*zir.Inst {910) InnerError!zir.Inst.Ref {
847 const tracy = trace(@src());911 const tracy = trace(@src());
848 defer tracy.end();912 defer tracy.end();
849913
...@@ -859,7 +923,7 @@ pub fn blockExpr(...@@ -859,7 +923,7 @@ pub fn blockExpr(
859 }923 }
860924
861 try blockExprStmts(mod, scope, block_node, statements);925 try blockExprStmts(mod, scope, block_node, statements);
862 return rvalueVoid(mod, scope, rl, block_node, {});926 return rvalue(mod, scope, rl, void_inst, block_node);
863}927}
864928
865fn checkLabelRedefinition(mod: *Module, parent_scope: *Scope, label: ast.TokenIndex) !void {929fn checkLabelRedefinition(mod: *Module, parent_scope: *Scope, label: ast.TokenIndex) !void {
...@@ -875,21 +939,18 @@ fn checkLabelRedefinition(mod: *Module, parent_scope: *Scope, label: ast.TokenIn...@@ -875,21 +939,18 @@ fn checkLabelRedefinition(mod: *Module, parent_scope: *Scope, label: ast.TokenIn
875 const main_tokens = tree.nodes.items(.main_token);939 const main_tokens = tree.nodes.items(.main_token);
876 const token_starts = tree.tokens.items(.start);940 const token_starts = tree.tokens.items(.start);
877941
878 const label_src = token_starts[label];
879 const prev_label_src = token_starts[prev_label.token];
880
881 const label_name = try mod.identifierTokenString(parent_scope, label);942 const label_name = try mod.identifierTokenString(parent_scope, label);
882 const msg = msg: {943 const msg = msg: {
883 const msg = try mod.errMsg(944 const msg = try mod.errMsg(
884 parent_scope,945 parent_scope,
885 label_src,946 gen_zir.tokSrcLoc(label),
886 "redefinition of label '{s}'",947 "redefinition of label '{s}'",
887 .{label_name},948 .{label_name},
888 );949 );
889 errdefer msg.destroy(mod.gpa);950 errdefer msg.destroy(mod.gpa);
890 try mod.errNote(951 try mod.errNote(
891 parent_scope,952 parent_scope,
892 prev_label_src,953 gen_zir.tokSrcLoc(prev_label.token),
893 msg,954 msg,
894 "previous definition is here",955 "previous definition is here",
895 .{},956 .{},
...@@ -917,7 +978,7 @@ fn labeledBlockExpr(...@@ -917,7 +978,7 @@ fn labeledBlockExpr(
917 block_node: ast.Node.Index,978 block_node: ast.Node.Index,
918 statements: []const ast.Node.Index,979 statements: []const ast.Node.Index,
919 zir_tag: zir.Inst.Tag,980 zir_tag: zir.Inst.Tag,
920) InnerError!*zir.Inst {981) InnerError!zir.Inst.Ref {
921 const tracy = trace(@src());982 const tracy = trace(@src());
922 defer tracy.end();983 defer tracy.end();
923984
...@@ -1285,6 +1346,7 @@ fn assignOp(...@@ -1285,6 +1346,7 @@ fn assignOp(
1285 infix_node: ast.Node.Index,1346 infix_node: ast.Node.Index,
1286 op_inst_tag: zir.Inst.Tag,1347 op_inst_tag: zir.Inst.Tag,
1287) InnerError!void {1348) InnerError!void {
1349 if (true) @panic("TODO update for zir-memory-layout");
1288 const tree = scope.tree();1350 const tree = scope.tree();
1289 const node_datas = tree.nodes.items(.data);1351 const node_datas = tree.nodes.items(.data);
1290 const main_tokens = tree.nodes.items(.main_token);1352 const main_tokens = tree.nodes.items(.main_token);
...@@ -1299,7 +1361,7 @@ fn assignOp(...@@ -1299,7 +1361,7 @@ fn assignOp(
1299 _ = try addZIRBinOp(mod, scope, src, .store, lhs_ptr, result);1361 _ = try addZIRBinOp(mod, scope, src, .store, lhs_ptr, result);
1300}1362}
13011363
1302fn boolNot(mod: *Module, scope: *Scope, node: ast.Node.Index) InnerError!*zir.Inst {1364fn boolNot(mod: *Module, scope: *Scope, node: ast.Node.Index) InnerError!zir.Inst.Ref {
1303 const tree = scope.tree();1365 const tree = scope.tree();
1304 const node_datas = tree.nodes.items(.data);1366 const node_datas = tree.nodes.items(.data);
1305 const main_tokens = tree.nodes.items(.main_token);1367 const main_tokens = tree.nodes.items(.main_token);
...@@ -1314,7 +1376,7 @@ fn boolNot(mod: *Module, scope: *Scope, node: ast.Node.Index) InnerError!*zir.In...@@ -1314,7 +1376,7 @@ fn boolNot(mod: *Module, scope: *Scope, node: ast.Node.Index) InnerError!*zir.In
1314 return addZIRUnOp(mod, scope, src, .bool_not, operand);1376 return addZIRUnOp(mod, scope, src, .bool_not, operand);
1315}1377}
13161378
1317fn bitNot(mod: *Module, scope: *Scope, node: ast.Node.Index) InnerError!*zir.Inst {1379fn bitNot(mod: *Module, scope: *Scope, node: ast.Node.Index) InnerError!zir.Inst.Ref {
1318 const tree = scope.tree();1380 const tree = scope.tree();
1319 const node_datas = tree.nodes.items(.data);1381 const node_datas = tree.nodes.items(.data);
1320 const main_tokens = tree.nodes.items(.main_token);1382 const main_tokens = tree.nodes.items(.main_token);
...@@ -1330,7 +1392,7 @@ fn negation(...@@ -1330,7 +1392,7 @@ fn negation(
1330 scope: *Scope,1392 scope: *Scope,
1331 node: ast.Node.Index,1393 node: ast.Node.Index,
1332 op_inst_tag: zir.Inst.Tag,1394 op_inst_tag: zir.Inst.Tag,
1333) InnerError!*zir.Inst {1395) InnerError!zir.Inst.Ref {
1334 const tree = scope.tree();1396 const tree = scope.tree();
1335 const node_datas = tree.nodes.items(.data);1397 const node_datas = tree.nodes.items(.data);
1336 const main_tokens = tree.nodes.items(.main_token);1398 const main_tokens = tree.nodes.items(.main_token);
...@@ -1350,7 +1412,8 @@ fn ptrType(...@@ -1350,7 +1412,8 @@ fn ptrType(
1350 scope: *Scope,1412 scope: *Scope,
1351 rl: ResultLoc,1413 rl: ResultLoc,
1352 ptr_info: ast.full.PtrType,1414 ptr_info: ast.full.PtrType,
1353) InnerError!*zir.Inst {1415) InnerError!zir.Inst.Ref {
1416 if (true) @panic("TODO update for zir-memory-layout");
1354 const tree = scope.tree();1417 const tree = scope.tree();
1355 const token_starts = tree.tokens.items(.start);1418 const token_starts = tree.tokens.items(.start);
13561419
...@@ -1394,7 +1457,8 @@ fn ptrType(...@@ -1394,7 +1457,8 @@ fn ptrType(
1394 return rvalue(mod, scope, rl, result);1457 return rvalue(mod, scope, rl, result);
1395}1458}
13961459
1397fn arrayType(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) !*zir.Inst {1460fn arrayType(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) !zir.Inst.Ref {
1461 if (true) @panic("TODO update for zir-memory-layout");
1398 const tree = scope.tree();1462 const tree = scope.tree();
1399 const main_tokens = tree.nodes.items(.main_token);1463 const main_tokens = tree.nodes.items(.main_token);
1400 const node_datas = tree.nodes.items(.data);1464 const node_datas = tree.nodes.items(.data);
...@@ -1421,7 +1485,8 @@ fn arrayType(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) !...@@ -1421,7 +1485,8 @@ fn arrayType(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) !
1421 }1485 }
1422}1486}
14231487
1424fn arrayTypeSentinel(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) !*zir.Inst {1488fn arrayTypeSentinel(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) !zir.Inst.Ref {
1489 if (true) @panic("TODO update for zir-memory-layout");
1425 const tree = scope.tree();1490 const tree = scope.tree();
1426 const main_tokens = tree.nodes.items(.main_token);1491 const main_tokens = tree.nodes.items(.main_token);
1427 const token_starts = tree.tokens.items(.start);1492 const token_starts = tree.tokens.items(.start);
...@@ -1454,7 +1519,8 @@ fn containerDecl(...@@ -1454,7 +1519,8 @@ fn containerDecl(
1454 scope: *Scope,1519 scope: *Scope,
1455 rl: ResultLoc,1520 rl: ResultLoc,
1456 container_decl: ast.full.ContainerDecl,1521 container_decl: ast.full.ContainerDecl,
1457) InnerError!*zir.Inst {1522) InnerError!zir.Inst.Ref {
1523 if (true) @panic("TODO update for zir-memory-layout");
1458 return mod.failTok(scope, container_decl.ast.main_token, "TODO implement container decls", .{});1524 return mod.failTok(scope, container_decl.ast.main_token, "TODO implement container decls", .{});
1459}1525}
14601526
...@@ -1463,7 +1529,8 @@ fn errorSetDecl(...@@ -1463,7 +1529,8 @@ fn errorSetDecl(
1463 scope: *Scope,1529 scope: *Scope,
1464 rl: ResultLoc,1530 rl: ResultLoc,
1465 node: ast.Node.Index,1531 node: ast.Node.Index,
1466) InnerError!*zir.Inst {1532) InnerError!zir.Inst.Ref {
1533 if (true) @panic("TODO update for zir-memory-layout");
1467 const tree = scope.tree();1534 const tree = scope.tree();
1468 const main_tokens = tree.nodes.items(.main_token);1535 const main_tokens = tree.nodes.items(.main_token);
1469 const token_tags = tree.tokens.items(.tag);1536 const token_tags = tree.tokens.items(.tag);
...@@ -1516,7 +1583,9 @@ fn orelseCatchExpr(...@@ -1516,7 +1583,9 @@ fn orelseCatchExpr(
1516 unwrap_code_op: zir.Inst.Tag,1583 unwrap_code_op: zir.Inst.Tag,
1517 rhs: ast.Node.Index,1584 rhs: ast.Node.Index,
1518 payload_token: ?ast.TokenIndex,1585 payload_token: ?ast.TokenIndex,
1519) InnerError!*zir.Inst {1586) InnerError!zir.Inst.Ref {
1587 if (true) @panic("TODO update for zir-memory-layout");
1588
1520 const tree = scope.tree();1589 const tree = scope.tree();
1521 const token_starts = tree.tokens.items(.start);1590 const token_starts = tree.tokens.items(.start);
15221591
...@@ -1548,7 +1617,7 @@ fn orelseCatchExpr(...@@ -1548,7 +1617,7 @@ fn orelseCatchExpr(
1548 }, .{});1617 }, .{});
15491618
1550 const block = try addZIRInstBlock(mod, scope, src, .block, .{1619 const block = try addZIRInstBlock(mod, scope, src, .block, .{
1551 .instructions = try block_scope.arena.dupe(*zir.Inst, block_scope.instructions.items),1620 .instructions = try block_scope.arena.dupe(zir.Inst.Ref, block_scope.instructions.items),
1552 });1621 });
15531622
1554 var then_scope: Scope.GenZir = .{1623 var then_scope: Scope.GenZir = .{
...@@ -1624,11 +1693,11 @@ fn finishThenElseBlock(...@@ -1624,11 +1693,11 @@ fn finishThenElseBlock(
1624 else_body: *zir.Body,1693 else_body: *zir.Body,
1625 then_src: usize,1694 then_src: usize,
1626 else_src: usize,1695 else_src: usize,
1627 then_result: *zir.Inst,1696 then_result: zir.Inst.Ref,
1628 else_result: ?*zir.Inst,1697 else_result: ?*zir.Inst,
1629 main_block: *zir.Inst.Block,1698 main_block: zir.Inst.Ref.Block,
1630 then_break_block: *zir.Inst.Block,1699 then_break_block: zir.Inst.Ref.Block,
1631) InnerError!*zir.Inst {1700) InnerError!zir.Inst.Ref {
1632 // We now have enough information to decide whether the result instruction should1701 // We now have enough information to decide whether the result instruction should
1633 // be communicated via result location pointer or break instructions.1702 // be communicated via result location pointer or break instructions.
1634 const strat = rlStrategy(rl, block_scope);1703 const strat = rlStrategy(rl, block_scope);
...@@ -1699,7 +1768,8 @@ fn tokenIdentEql(mod: *Module, scope: *Scope, token1: ast.TokenIndex, token2: as...@@ -1699,7 +1768,8 @@ fn tokenIdentEql(mod: *Module, scope: *Scope, token1: ast.TokenIndex, token2: as
1699 return mem.eql(u8, ident_name_1, ident_name_2);1768 return mem.eql(u8, ident_name_1, ident_name_2);
1700}1769}
17011770
1702pub fn fieldAccess(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) InnerError!*zir.Inst {1771pub fn fieldAccess(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) InnerError!zir.Inst.Ref {
1772 if (true) @panic("TODO update for zir-memory-layout");
1703 const tree = scope.tree();1773 const tree = scope.tree();
1704 const token_starts = tree.tokens.items(.start);1774 const token_starts = tree.tokens.items(.start);
1705 const main_tokens = tree.nodes.items(.main_token);1775 const main_tokens = tree.nodes.items(.main_token);
...@@ -1727,7 +1797,8 @@ fn arrayAccess(...@@ -1727,7 +1797,8 @@ fn arrayAccess(
1727 scope: *Scope,1797 scope: *Scope,
1728 rl: ResultLoc,1798 rl: ResultLoc,
1729 node: ast.Node.Index,1799 node: ast.Node.Index,
1730) InnerError!*zir.Inst {1800) InnerError!zir.Inst.Ref {
1801 if (true) @panic("TODO update for zir-memory-layout");
1731 const tree = scope.tree();1802 const tree = scope.tree();
1732 const main_tokens = tree.nodes.items(.main_token);1803 const main_tokens = tree.nodes.items(.main_token);
1733 const token_starts = tree.tokens.items(.start);1804 const token_starts = tree.tokens.items(.start);
...@@ -1756,7 +1827,8 @@ fn sliceExpr(...@@ -1756,7 +1827,8 @@ fn sliceExpr(
1756 scope: *Scope,1827 scope: *Scope,
1757 rl: ResultLoc,1828 rl: ResultLoc,
1758 slice: ast.full.Slice,1829 slice: ast.full.Slice,
1759) InnerError!*zir.Inst {1830) InnerError!zir.Inst.Ref {
1831 if (true) @panic("TODO update for zir-memory-layout");
1760 const tree = scope.tree();1832 const tree = scope.tree();
1761 const token_starts = tree.tokens.items(.start);1833 const token_starts = tree.tokens.items(.start);
17621834
...@@ -1805,7 +1877,8 @@ fn simpleBinOp(...@@ -1805,7 +1877,8 @@ fn simpleBinOp(
1805 rl: ResultLoc,1877 rl: ResultLoc,
1806 infix_node: ast.Node.Index,1878 infix_node: ast.Node.Index,
1807 op_inst_tag: zir.Inst.Tag,1879 op_inst_tag: zir.Inst.Tag,
1808) InnerError!*zir.Inst {1880) InnerError!zir.Inst.Ref {
1881 if (true) @panic("TODO update for zir-memory-layout");
1809 const tree = scope.tree();1882 const tree = scope.tree();
1810 const node_datas = tree.nodes.items(.data);1883 const node_datas = tree.nodes.items(.data);
1811 const main_tokens = tree.nodes.items(.main_token);1884 const main_tokens = tree.nodes.items(.main_token);
...@@ -1824,7 +1897,8 @@ fn boolBinOp(...@@ -1824,7 +1897,8 @@ fn boolBinOp(
1824 rl: ResultLoc,1897 rl: ResultLoc,
1825 infix_node: ast.Node.Index,1898 infix_node: ast.Node.Index,
1826 is_bool_and: bool,1899 is_bool_and: bool,
1827) InnerError!*zir.Inst {1900) InnerError!zir.Inst.Ref {
1901 if (true) @panic("TODO update for zir-memory-layout");
1828 const tree = scope.tree();1902 const tree = scope.tree();
1829 const node_datas = tree.nodes.items(.data);1903 const node_datas = tree.nodes.items(.data);
1830 const main_tokens = tree.nodes.items(.main_token);1904 const main_tokens = tree.nodes.items(.main_token);
...@@ -1853,7 +1927,7 @@ fn boolBinOp(...@@ -1853,7 +1927,7 @@ fn boolBinOp(
1853 }, .{});1927 }, .{});
18541928
1855 const block = try addZIRInstBlock(mod, scope, src, .block, .{1929 const block = try addZIRInstBlock(mod, scope, src, .block, .{
1856 .instructions = try block_scope.arena.dupe(*zir.Inst, block_scope.instructions.items),1930 .instructions = try block_scope.arena.dupe(zir.Inst.Ref, block_scope.instructions.items),
1857 });1931 });
18581932
1859 var rhs_scope: Scope.GenZir = .{1933 var rhs_scope: Scope.GenZir = .{
...@@ -1893,15 +1967,15 @@ fn boolBinOp(...@@ -1893,15 +1967,15 @@ fn boolBinOp(
1893 // break rhs1967 // break rhs
1894 // else1968 // else
1895 // break false1969 // break false
1896 condbr.positionals.then_body = .{ .instructions = try rhs_scope.arena.dupe(*zir.Inst, rhs_scope.instructions.items) };1970 condbr.positionals.then_body = .{ .instructions = try rhs_scope.arena.dupe(zir.Inst.Ref, rhs_scope.instructions.items) };
1897 condbr.positionals.else_body = .{ .instructions = try const_scope.arena.dupe(*zir.Inst, const_scope.instructions.items) };1971 condbr.positionals.else_body = .{ .instructions = try const_scope.arena.dupe(zir.Inst.Ref, const_scope.instructions.items) };
1898 } else {1972 } else {
1899 // if lhs // OR1973 // if lhs // OR
1900 // break true1974 // break true
1901 // else1975 // else
1902 // break rhs1976 // break rhs
1903 condbr.positionals.then_body = .{ .instructions = try const_scope.arena.dupe(*zir.Inst, const_scope.instructions.items) };1977 condbr.positionals.then_body = .{ .instructions = try const_scope.arena.dupe(zir.Inst.Ref, const_scope.instructions.items) };
1904 condbr.positionals.else_body = .{ .instructions = try rhs_scope.arena.dupe(*zir.Inst, rhs_scope.instructions.items) };1978 condbr.positionals.else_body = .{ .instructions = try rhs_scope.arena.dupe(zir.Inst.Ref, rhs_scope.instructions.items) };
1905 }1979 }
19061980
1907 return rvalue(mod, scope, rl, &block.base);1981 return rvalue(mod, scope, rl, &block.base);
...@@ -1912,7 +1986,8 @@ fn ifExpr(...@@ -1912,7 +1986,8 @@ fn ifExpr(
1912 scope: *Scope,1986 scope: *Scope,
1913 rl: ResultLoc,1987 rl: ResultLoc,
1914 if_full: ast.full.If,1988 if_full: ast.full.If,
1915) InnerError!*zir.Inst {1989) InnerError!zir.Inst.Ref {
1990 if (true) @panic("TODO update for zir-memory-layout");
1916 var block_scope: Scope.GenZir = .{1991 var block_scope: Scope.GenZir = .{
1917 .parent = scope,1992 .parent = scope,
1918 .decl = scope.ownerDecl().?,1993 .decl = scope.ownerDecl().?,
...@@ -1951,7 +2026,7 @@ fn ifExpr(...@@ -1951,7 +2026,7 @@ fn ifExpr(
1951 }, .{});2026 }, .{});
19522027
1953 const block = try addZIRInstBlock(mod, scope, if_src, .block, .{2028 const block = try addZIRInstBlock(mod, scope, if_src, .block, .{
1954 .instructions = try block_scope.arena.dupe(*zir.Inst, block_scope.instructions.items),2029 .instructions = try block_scope.arena.dupe(zir.Inst.Ref, block_scope.instructions.items),
1955 });2030 });
19562031
1957 const then_src = token_starts[tree.lastToken(if_full.ast.then_expr)];2032 const then_src = token_starts[tree.lastToken(if_full.ast.then_expr)];
...@@ -2016,7 +2091,7 @@ fn ifExpr(...@@ -2016,7 +2091,7 @@ fn ifExpr(
2016/// Expects to find exactly 1 .store_to_block_ptr instruction.2091/// Expects to find exactly 1 .store_to_block_ptr instruction.
2017fn copyBodyWithElidedStoreBlockPtr(body: *zir.Body, scope: Module.Scope.GenZir) !void {2092fn copyBodyWithElidedStoreBlockPtr(body: *zir.Body, scope: Module.Scope.GenZir) !void {
2018 body.* = .{2093 body.* = .{
2019 .instructions = try scope.arena.alloc(*zir.Inst, scope.instructions.items.len - 1),2094 .instructions = try scope.arena.alloc(zir.Inst.Ref, scope.instructions.items.len - 1),
2020 };2095 };
2021 var dst_index: usize = 0;2096 var dst_index: usize = 0;
2022 for (scope.instructions.items) |src_inst| {2097 for (scope.instructions.items) |src_inst| {
...@@ -2030,7 +2105,7 @@ fn copyBodyWithElidedStoreBlockPtr(body: *zir.Body, scope: Module.Scope.GenZir)...@@ -2030,7 +2105,7 @@ fn copyBodyWithElidedStoreBlockPtr(body: *zir.Body, scope: Module.Scope.GenZir)
20302105
2031fn copyBodyNoEliding(body: *zir.Body, scope: Module.Scope.GenZir) !void {2106fn copyBodyNoEliding(body: *zir.Body, scope: Module.Scope.GenZir) !void {
2032 body.* = .{2107 body.* = .{
2033 .instructions = try scope.arena.dupe(*zir.Inst, scope.instructions.items),2108 .instructions = try scope.arena.dupe(zir.Inst.Ref, scope.instructions.items),
2034 };2109 };
2035}2110}
20362111
...@@ -2039,7 +2114,8 @@ fn whileExpr(...@@ -2039,7 +2114,8 @@ fn whileExpr(
2039 scope: *Scope,2114 scope: *Scope,
2040 rl: ResultLoc,2115 rl: ResultLoc,
2041 while_full: ast.full.While,2116 while_full: ast.full.While,
2042) InnerError!*zir.Inst {2117) InnerError!zir.Inst.Ref {
2118 if (true) @panic("TODO update for zir-memory-layout");
2043 if (while_full.label_token) |label_token| {2119 if (while_full.label_token) |label_token| {
2044 try checkLabelRedefinition(mod, scope, label_token);2120 try checkLabelRedefinition(mod, scope, label_token);
2045 }2121 }
...@@ -2096,7 +2172,7 @@ fn whileExpr(...@@ -2096,7 +2172,7 @@ fn whileExpr(
2096 .else_body = undefined, // populated below2172 .else_body = undefined, // populated below
2097 }, .{});2173 }, .{});
2098 const cond_block = try addZIRInstBlock(mod, &loop_scope.base, while_src, .block, .{2174 const cond_block = try addZIRInstBlock(mod, &loop_scope.base, while_src, .block, .{
2099 .instructions = try loop_scope.arena.dupe(*zir.Inst, continue_scope.instructions.items),2175 .instructions = try loop_scope.arena.dupe(zir.Inst.Ref, continue_scope.instructions.items),
2100 });2176 });
2101 // TODO avoid emitting the continue expr when there2177 // TODO avoid emitting the continue expr when there
2102 // are no jumps to it. This happens when the last statement of a while body is noreturn2178 // are no jumps to it. This happens when the last statement of a while body is noreturn
...@@ -2113,13 +2189,13 @@ fn whileExpr(...@@ -2113,13 +2189,13 @@ fn whileExpr(
2113 },2189 },
2114 .positionals = .{2190 .positionals = .{
2115 .body = .{2191 .body = .{
2116 .instructions = try scope.arena().dupe(*zir.Inst, loop_scope.instructions.items),2192 .instructions = try scope.arena().dupe(zir.Inst.Ref, loop_scope.instructions.items),
2117 },2193 },
2118 },2194 },
2119 .kw_args = .{},2195 .kw_args = .{},
2120 };2196 };
2121 const while_block = try addZIRInstBlock(mod, scope, while_src, .block, .{2197 const while_block = try addZIRInstBlock(mod, scope, while_src, .block, .{
2122 .instructions = try scope.arena().dupe(*zir.Inst, &[1]*zir.Inst{&loop.base}),2198 .instructions = try scope.arena().dupe(zir.Inst.Ref, &[1]zir.Inst.Ref{&loop.base}),
2123 });2199 });
2124 loop_scope.break_block = while_block;2200 loop_scope.break_block = while_block;
2125 loop_scope.continue_block = cond_block;2201 loop_scope.continue_block = cond_block;
...@@ -2195,7 +2271,8 @@ fn forExpr(...@@ -2195,7 +2271,8 @@ fn forExpr(
2195 scope: *Scope,2271 scope: *Scope,
2196 rl: ResultLoc,2272 rl: ResultLoc,
2197 for_full: ast.full.While,2273 for_full: ast.full.While,
2198) InnerError!*zir.Inst {2274) InnerError!zir.Inst.Ref {
2275 if (true) @panic("TODO update for zir-memory-layout");
2199 if (for_full.label_token) |label_token| {2276 if (for_full.label_token) |label_token| {
2200 try checkLabelRedefinition(mod, scope, label_token);2277 try checkLabelRedefinition(mod, scope, label_token);
2201 }2278 }
...@@ -2258,7 +2335,7 @@ fn forExpr(...@@ -2258,7 +2335,7 @@ fn forExpr(
2258 .else_body = undefined, // populated below2335 .else_body = undefined, // populated below
2259 }, .{});2336 }, .{});
2260 const cond_block = try addZIRInstBlock(mod, &loop_scope.base, for_src, .block, .{2337 const cond_block = try addZIRInstBlock(mod, &loop_scope.base, for_src, .block, .{
2261 .instructions = try loop_scope.arena.dupe(*zir.Inst, cond_scope.instructions.items),2338 .instructions = try loop_scope.arena.dupe(zir.Inst.Ref, cond_scope.instructions.items),
2262 });2339 });
22632340
2264 // increment index variable2341 // increment index variable
...@@ -2278,13 +2355,13 @@ fn forExpr(...@@ -2278,13 +2355,13 @@ fn forExpr(
2278 },2355 },
2279 .positionals = .{2356 .positionals = .{
2280 .body = .{2357 .body = .{
2281 .instructions = try scope.arena().dupe(*zir.Inst, loop_scope.instructions.items),2358 .instructions = try scope.arena().dupe(zir.Inst.Ref, loop_scope.instructions.items),
2282 },2359 },
2283 },2360 },
2284 .kw_args = .{},2361 .kw_args = .{},
2285 };2362 };
2286 const for_block = try addZIRInstBlock(mod, scope, for_src, .block, .{2363 const for_block = try addZIRInstBlock(mod, scope, for_src, .block, .{
2287 .instructions = try scope.arena().dupe(*zir.Inst, &[1]*zir.Inst{&loop.base}),2364 .instructions = try scope.arena().dupe(zir.Inst.Ref, &[1]zir.Inst.Ref{&loop.base}),
2288 });2365 });
2289 loop_scope.break_block = for_block;2366 loop_scope.break_block = for_block;
2290 loop_scope.continue_block = cond_block;2367 loop_scope.continue_block = cond_block;
...@@ -2407,7 +2484,8 @@ fn switchExpr(...@@ -2407,7 +2484,8 @@ fn switchExpr(
2407 scope: *Scope,2484 scope: *Scope,
2408 rl: ResultLoc,2485 rl: ResultLoc,
2409 switch_node: ast.Node.Index,2486 switch_node: ast.Node.Index,
2410) InnerError!*zir.Inst {2487) InnerError!zir.Inst.Ref {
2488 if (true) @panic("TODO update for zir-memory-layout");
2411 const tree = scope.tree();2489 const tree = scope.tree();
2412 const node_datas = tree.nodes.items(.data);2490 const node_datas = tree.nodes.items(.data);
2413 const main_tokens = tree.nodes.items(.main_token);2491 const main_tokens = tree.nodes.items(.main_token);
...@@ -2432,7 +2510,7 @@ fn switchExpr(...@@ -2432,7 +2510,7 @@ fn switchExpr(
2432 setBlockResultLoc(&block_scope, rl);2510 setBlockResultLoc(&block_scope, rl);
2433 defer block_scope.instructions.deinit(mod.gpa);2511 defer block_scope.instructions.deinit(mod.gpa);
24342512
2435 var items = std.ArrayList(*zir.Inst).init(mod.gpa);2513 var items = std.ArrayList(zir.Inst.Ref).init(mod.gpa);
2436 defer items.deinit();2514 defer items.deinit();
24372515
2438 // First we gather all the switch items and check else/'_' prongs.2516 // First we gather all the switch items and check else/'_' prongs.
...@@ -2549,13 +2627,13 @@ fn switchExpr(...@@ -2549,13 +2627,13 @@ fn switchExpr(
2549 const switch_inst = try addZirInstT(mod, &block_scope.base, switch_src, zir.Inst.SwitchBr, rl_and_tag.tag, .{2627 const switch_inst = try addZirInstT(mod, &block_scope.base, switch_src, zir.Inst.SwitchBr, rl_and_tag.tag, .{
2550 .target = target,2628 .target = target,
2551 .cases = cases,2629 .cases = cases,
2552 .items = try block_scope.arena.dupe(*zir.Inst, items.items),2630 .items = try block_scope.arena.dupe(zir.Inst.Ref, items.items),
2553 .else_body = undefined, // populated below2631 .else_body = undefined, // populated below
2554 .range = first_range,2632 .range = first_range,
2555 .special_prong = special_prong,2633 .special_prong = special_prong,
2556 });2634 });
2557 const block = try addZIRInstBlock(mod, scope, switch_src, .block, .{2635 const block = try addZIRInstBlock(mod, scope, switch_src, .block, .{
2558 .instructions = try block_scope.arena.dupe(*zir.Inst, block_scope.instructions.items),2636 .instructions = try block_scope.arena.dupe(zir.Inst.Ref, block_scope.instructions.items),
2559 });2637 });
25602638
2561 var case_scope: Scope.GenZir = .{2639 var case_scope: Scope.GenZir = .{
...@@ -2611,7 +2689,7 @@ fn switchExpr(...@@ -2611,7 +2689,7 @@ fn switchExpr(
26112689
2612 cases[case_index] = .{2690 cases[case_index] = .{
2613 .item = item,2691 .item = item,
2614 .body = .{ .instructions = try scope.arena().dupe(*zir.Inst, case_scope.instructions.items) },2692 .body = .{ .instructions = try scope.arena().dupe(zir.Inst.Ref, case_scope.instructions.items) },
2615 };2693 };
2616 case_index += 1;2694 case_index += 1;
2617 continue;2695 continue;
...@@ -2658,14 +2736,14 @@ fn switchExpr(...@@ -2658,14 +2736,14 @@ fn switchExpr(
2658 .else_body = undefined, // populated below2736 .else_body = undefined, // populated below
2659 }, .{});2737 }, .{});
2660 const cond_block = try addZIRInstBlock(mod, &else_scope.base, case_src, .block, .{2738 const cond_block = try addZIRInstBlock(mod, &else_scope.base, case_src, .block, .{
2661 .instructions = try scope.arena().dupe(*zir.Inst, case_scope.instructions.items),2739 .instructions = try scope.arena().dupe(zir.Inst.Ref, case_scope.instructions.items),
2662 });2740 });
26632741
2664 // reset cond_scope for then_body2742 // reset cond_scope for then_body
2665 case_scope.instructions.items.len = 0;2743 case_scope.instructions.items.len = 0;
2666 try switchCaseExpr(mod, &case_scope.base, block_scope.break_result_loc, block, case, target);2744 try switchCaseExpr(mod, &case_scope.base, block_scope.break_result_loc, block, case, target);
2667 condbr.positionals.then_body = .{2745 condbr.positionals.then_body = .{
2668 .instructions = try scope.arena().dupe(*zir.Inst, case_scope.instructions.items),2746 .instructions = try scope.arena().dupe(zir.Inst.Ref, case_scope.instructions.items),
2669 };2747 };
26702748
2671 // reset cond_scope for else_body2749 // reset cond_scope for else_body
...@@ -2674,7 +2752,7 @@ fn switchExpr(...@@ -2674,7 +2752,7 @@ fn switchExpr(
2674 .block = cond_block,2752 .block = cond_block,
2675 }, .{});2753 }, .{});
2676 condbr.positionals.else_body = .{2754 condbr.positionals.else_body = .{
2677 .instructions = try scope.arena().dupe(*zir.Inst, case_scope.instructions.items),2755 .instructions = try scope.arena().dupe(zir.Inst.Ref, case_scope.instructions.items),
2678 };2756 };
2679 }2757 }
26802758
...@@ -2686,7 +2764,7 @@ fn switchExpr(...@@ -2686,7 +2764,7 @@ fn switchExpr(
2686 _ = try addZIRNoOp(mod, &else_scope.base, switch_src, .unreachable_unsafe);2764 _ = try addZIRNoOp(mod, &else_scope.base, switch_src, .unreachable_unsafe);
2687 }2765 }
2688 switch_inst.positionals.else_body = .{2766 switch_inst.positionals.else_body = .{
2689 .instructions = try block_scope.arena.dupe(*zir.Inst, else_scope.instructions.items),2767 .instructions = try block_scope.arena.dupe(zir.Inst.Ref, else_scope.instructions.items),
2690 };2768 };
26912769
2692 return &block.base;2770 return &block.base;
...@@ -2698,7 +2776,7 @@ fn switchCaseExpr(...@@ -2698,7 +2776,7 @@ fn switchCaseExpr(
2698 rl: ResultLoc,2776 rl: ResultLoc,
2699 block: *zir.Inst.Block,2777 block: *zir.Inst.Block,
2700 case: ast.full.SwitchCase,2778 case: ast.full.SwitchCase,
2701 target: *zir.Inst,2779 target: zir.Inst.Ref,
2702) !void {2780) !void {
2703 const tree = scope.tree();2781 const tree = scope.tree();
2704 const node_datas = tree.nodes.items(.data);2782 const node_datas = tree.nodes.items(.data);
...@@ -2733,27 +2811,22 @@ fn switchCaseExpr(...@@ -2733,27 +2811,22 @@ fn switchCaseExpr(
2733 }2811 }
2734}2812}
27352813
2736fn ret(mod: *Module, scope: *Scope, node: ast.Node.Index) InnerError!*zir.Inst {2814fn ret(mod: *Module, scope: *Scope, node: ast.Node.Index) InnerError!zir.Inst.Ref {
2737 const tree = scope.tree();2815 const tree = scope.tree();
2738 const node_datas = tree.nodes.items(.data);2816 const node_datas = tree.nodes.items(.data);
2739 const main_tokens = tree.nodes.items(.main_token);2817 const main_tokens = tree.nodes.items(.main_token);
2740 const token_starts = tree.tokens.items(.start);
27412818
2742 const src = token_starts[main_tokens[node]];2819 const operand_node = node_datas[node].lhs;
2743 const rhs_node = node_datas[node].lhs;2820 const gz = scope.getGenZir();
2744 if (rhs_node != 0) {2821 const operand: zir.Inst.Ref = if (operand_node != 0) operand: {
2745 if (nodeMayNeedMemoryLocation(scope, rhs_node)) {2822 const rl: ResultLoc = if (nodeMayNeedMemoryLocation(scope, operand_node)) .{
2746 const ret_ptr = try addZIRNoOp(mod, scope, src, .ret_ptr);2823 .ptr = try gz.addNode(.ret_ptr, node),
2747 const operand = try expr(mod, scope, .{ .ptr = ret_ptr }, rhs_node);2824 } else .{
2748 return addZIRUnOp(mod, scope, src, .@"return", operand);2825 .ty = try gz.addNode(.ret_type, node),
2749 } else {2826 };
2750 const fn_ret_ty = try addZIRNoOp(mod, scope, src, .ret_type);2827 break :operand try expr(mod, scope, rl, operand_node);
2751 const operand = try expr(mod, scope, .{ .ty = fn_ret_ty }, rhs_node);2828 } else void_inst;
2752 return addZIRUnOp(mod, scope, src, .@"return", operand);2829 return gz.addUnNode(.ret_node, operand, node);
2753 }
2754 } else {
2755 return addZIRNoOp(mod, scope, src, .return_void);
2756 }
2757}2830}
27582831
2759fn identifier(2832fn identifier(
...@@ -2761,7 +2834,8 @@ fn identifier(...@@ -2761,7 +2834,8 @@ fn identifier(
2761 scope: *Scope,2834 scope: *Scope,
2762 rl: ResultLoc,2835 rl: ResultLoc,
2763 ident: ast.Node.Index,2836 ident: ast.Node.Index,
2764) InnerError!*zir.Inst {2837) InnerError!zir.Inst.Ref {
2838 if (true) @panic("TODO update for zir-memory-layout");
2765 const tracy = trace(@src());2839 const tracy = trace(@src());
2766 defer tracy.end();2840 defer tracy.end();
27672841
...@@ -2882,7 +2956,8 @@ fn stringLiteral(...@@ -2882,7 +2956,8 @@ fn stringLiteral(
2882 scope: *Scope,2956 scope: *Scope,
2883 rl: ResultLoc,2957 rl: ResultLoc,
2884 str_lit: ast.Node.Index,2958 str_lit: ast.Node.Index,
2885) InnerError!*zir.Inst {2959) InnerError!zir.Inst.Ref {
2960 if (true) @panic("TODO update for zir-memory-layout");
2886 const tree = scope.tree();2961 const tree = scope.tree();
2887 const main_tokens = tree.nodes.items(.main_token);2962 const main_tokens = tree.nodes.items(.main_token);
2888 const token_starts = tree.tokens.items(.start);2963 const token_starts = tree.tokens.items(.start);
...@@ -2899,7 +2974,8 @@ fn multilineStringLiteral(...@@ -2899,7 +2974,8 @@ fn multilineStringLiteral(
2899 scope: *Scope,2974 scope: *Scope,
2900 rl: ResultLoc,2975 rl: ResultLoc,
2901 str_lit: ast.Node.Index,2976 str_lit: ast.Node.Index,
2902) InnerError!*zir.Inst {2977) InnerError!zir.Inst.Ref {
2978 if (true) @panic("TODO update for zir-memory-layout");
2903 const tree = scope.tree();2979 const tree = scope.tree();
2904 const node_datas = tree.nodes.items(.data);2980 const node_datas = tree.nodes.items(.data);
2905 const main_tokens = tree.nodes.items(.main_token);2981 const main_tokens = tree.nodes.items(.main_token);
...@@ -2943,7 +3019,8 @@ fn multilineStringLiteral(...@@ -2943,7 +3019,8 @@ fn multilineStringLiteral(
2943 return rvalue(mod, scope, rl, str_inst);3019 return rvalue(mod, scope, rl, str_inst);
2944}3020}
29453021
2946fn charLiteral(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) !*zir.Inst {3022fn charLiteral(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) !zir.Inst.Ref {
3023 if (true) @panic("TODO update for zir-memory-layout");
2947 const tree = scope.tree();3024 const tree = scope.tree();
2948 const main_tokens = tree.nodes.items(.main_token);3025 const main_tokens = tree.nodes.items(.main_token);
2949 const main_token = main_tokens[node];3026 const main_token = main_tokens[node];
...@@ -2970,11 +3047,11 @@ fn integerLiteral(...@@ -2970,11 +3047,11 @@ fn integerLiteral(
2970 mod: *Module,3047 mod: *Module,
2971 scope: *Scope,3048 scope: *Scope,
2972 rl: ResultLoc,3049 rl: ResultLoc,
2973 int_lit: ast.Node.Index,3050 node: ast.Node.Index,
2974) InnerError!*zir.Inst {3051) InnerError!zir.Inst.Ref {
2975 const tree = scope.tree();3052 const tree = scope.tree();
2976 const main_tokens = tree.nodes.items(.main_token);3053 const main_tokens = tree.nodes.items(.main_token);
2977 const int_token = main_tokens[int_lit];3054 const int_token = main_tokens[node];
2978 const prefixed_bytes = tree.tokenSlice(int_token);3055 const prefixed_bytes = tree.tokenSlice(int_token);
2979 const gz = scope.getGenZir();3056 const gz = scope.getGenZir();
2980 if (std.fmt.parseInt(u64, prefixed_bytes, 0)) |small_int| {3057 if (std.fmt.parseInt(u64, prefixed_bytes, 0)) |small_int| {
...@@ -2983,9 +3060,9 @@ fn integerLiteral(...@@ -2983,9 +3060,9 @@ fn integerLiteral(
2983 1 => @enumToInt(zir.Const.one),3060 1 => @enumToInt(zir.Const.one),
2984 else => try gz.addInt(small_int),3061 else => try gz.addInt(small_int),
2985 };3062 };
2986 return rvalue(mod, scope, rl, result);3063 return rvalue(mod, scope, rl, result, node);
2987 } else |err| {3064 } else |err| {
2988 return mod.failTok(scope, int_token, "TODO implement int literals that don't fit in a u64", .{});3065 return mod.failNode(scope, node, "TODO implement int literals that don't fit in a u64", .{});
2989 }3066 }
2990}3067}
29913068
...@@ -2994,7 +3071,8 @@ fn floatLiteral(...@@ -2994,7 +3071,8 @@ fn floatLiteral(
2994 scope: *Scope,3071 scope: *Scope,
2995 rl: ResultLoc,3072 rl: ResultLoc,
2996 float_lit: ast.Node.Index,3073 float_lit: ast.Node.Index,
2997) InnerError!*zir.Inst {3074) InnerError!zir.Inst.Ref {
3075 if (true) @panic("TODO update for zir-memory-layout");
2998 const arena = scope.arena();3076 const arena = scope.arena();
2999 const tree = scope.tree();3077 const tree = scope.tree();
3000 const main_tokens = tree.nodes.items(.main_token);3078 const main_tokens = tree.nodes.items(.main_token);
...@@ -3016,7 +3094,8 @@ fn floatLiteral(...@@ -3016,7 +3094,8 @@ fn floatLiteral(
3016 return rvalue(mod, scope, rl, result);3094 return rvalue(mod, scope, rl, result);
3017}3095}
30183096
3019fn asmExpr(mod: *Module, scope: *Scope, rl: ResultLoc, full: ast.full.Asm) InnerError!*zir.Inst {3097fn asmExpr(mod: *Module, scope: *Scope, rl: ResultLoc, full: ast.full.Asm) InnerError!zir.Inst.Ref {
3098 if (true) @panic("TODO update for zir-memory-layout");
3020 const arena = scope.arena();3099 const arena = scope.arena();
3021 const tree = scope.tree();3100 const tree = scope.tree();
3022 const main_tokens = tree.nodes.items(.main_token);3101 const main_tokens = tree.nodes.items(.main_token);
...@@ -3028,7 +3107,7 @@ fn asmExpr(mod: *Module, scope: *Scope, rl: ResultLoc, full: ast.full.Asm) Inner...@@ -3028,7 +3107,7 @@ fn asmExpr(mod: *Module, scope: *Scope, rl: ResultLoc, full: ast.full.Asm) Inner
3028 }3107 }
30293108
3030 const inputs = try arena.alloc([]const u8, full.inputs.len);3109 const inputs = try arena.alloc([]const u8, full.inputs.len);
3031 const args = try arena.alloc(*zir.Inst, full.inputs.len);3110 const args = try arena.alloc(zir.Inst.Ref, full.inputs.len);
30323111
3033 const src = token_starts[full.ast.asm_token];3112 const src = token_starts[full.ast.asm_token];
3034 const str_type = try addZIRInstConst(mod, scope, src, .{3113 const str_type = try addZIRInstConst(mod, scope, src, .{
...@@ -3068,7 +3147,7 @@ fn as(...@@ -3068,7 +3147,7 @@ fn as(
3068 src: usize,3147 src: usize,
3069 lhs: ast.Node.Index,3148 lhs: ast.Node.Index,
3070 rhs: ast.Node.Index,3149 rhs: ast.Node.Index,
3071) InnerError!*zir.Inst {3150) InnerError!zir.Inst.Ref {
3072 const dest_type = try typeExpr(mod, scope, lhs);3151 const dest_type = try typeExpr(mod, scope, lhs);
3073 switch (rl) {3152 switch (rl) {
3074 .none, .discard, .ref, .ty => {3153 .none, .discard, .ref, .ty => {
...@@ -3099,10 +3178,10 @@ fn asRlPtr(...@@ -3099,10 +3178,10 @@ fn asRlPtr(
3099 scope: *Scope,3178 scope: *Scope,
3100 rl: ResultLoc,3179 rl: ResultLoc,
3101 src: usize,3180 src: usize,
3102 result_ptr: *zir.Inst,3181 result_ptr: zir.Inst.Ref,
3103 operand_node: ast.Node.Index,3182 operand_node: ast.Node.Index,
3104 dest_type: *zir.Inst,3183 dest_type: zir.Inst.Ref,
3105) InnerError!*zir.Inst {3184) InnerError!zir.Inst.Ref {
3106 // Detect whether this expr() call goes into rvalue() to store the result into the3185 // Detect whether this expr() call goes into rvalue() to store the result into the
3107 // result location. If it does, elide the coerce_result_ptr instruction3186 // result location. If it does, elide the coerce_result_ptr instruction
3108 // as well as the store instruction, instead passing the result as an rvalue.3187 // as well as the store instruction, instead passing the result as an rvalue.
...@@ -3146,7 +3225,7 @@ fn bitCast(...@@ -3146,7 +3225,7 @@ fn bitCast(
3146 src: usize,3225 src: usize,
3147 lhs: ast.Node.Index,3226 lhs: ast.Node.Index,
3148 rhs: ast.Node.Index,3227 rhs: ast.Node.Index,
3149) InnerError!*zir.Inst {3228) InnerError!zir.Inst.Ref {
3150 const dest_type = try typeExpr(mod, scope, lhs);3229 const dest_type = try typeExpr(mod, scope, lhs);
3151 switch (rl) {3230 switch (rl) {
3152 .none => {3231 .none => {
...@@ -3193,7 +3272,7 @@ fn typeOf(...@@ -3193,7 +3272,7 @@ fn typeOf(
3193 builtin_token: ast.TokenIndex,3272 builtin_token: ast.TokenIndex,
3194 src: usize,3273 src: usize,
3195 params: []const ast.Node.Index,3274 params: []const ast.Node.Index,
3196) InnerError!*zir.Inst {3275) InnerError!zir.Inst.Ref {
3197 if (params.len < 1) {3276 if (params.len < 1) {
3198 return mod.failTok(scope, builtin_token, "expected at least 1 argument, found 0", .{});3277 return mod.failTok(scope, builtin_token, "expected at least 1 argument, found 0", .{});
3199 }3278 }
...@@ -3201,7 +3280,7 @@ fn typeOf(...@@ -3201,7 +3280,7 @@ fn typeOf(
3201 return rvalue(mod, scope, rl, try addZIRUnOp(mod, scope, src, .typeof, try expr(mod, scope, .none, params[0])));3280 return rvalue(mod, scope, rl, try addZIRUnOp(mod, scope, src, .typeof, try expr(mod, scope, .none, params[0])));
3202 }3281 }
3203 const arena = scope.arena();3282 const arena = scope.arena();
3204 var items = try arena.alloc(*zir.Inst, params.len);3283 var items = try arena.alloc(zir.Inst.Ref, params.len);
3205 for (params) |param, param_i|3284 for (params) |param, param_i|
3206 items[param_i] = try expr(mod, scope, .none, param);3285 items[param_i] = try expr(mod, scope, .none, param);
3207 return rvalue(mod, scope, rl, try addZIRInst(mod, scope, src, zir.Inst.TypeOfPeer, .{ .items = items }, .{}));3286 return rvalue(mod, scope, rl, try addZIRInst(mod, scope, src, zir.Inst.TypeOfPeer, .{ .items = items }, .{}));
...@@ -3213,7 +3292,8 @@ fn builtinCall(...@@ -3213,7 +3292,8 @@ fn builtinCall(
3213 rl: ResultLoc,3292 rl: ResultLoc,
3214 call: ast.Node.Index,3293 call: ast.Node.Index,
3215 params: []const ast.Node.Index,3294 params: []const ast.Node.Index,
3216) InnerError!*zir.Inst {3295) InnerError!zir.Inst.Ref {
3296 if (true) @panic("TODO update for zir-memory-layout");
3217 const tree = scope.tree();3297 const tree = scope.tree();
3218 const main_tokens = tree.nodes.items(.main_token);3298 const main_tokens = tree.nodes.items(.main_token);
3219 const token_starts = tree.tokens.items(.start);3299 const token_starts = tree.tokens.items(.start);
...@@ -3284,7 +3364,7 @@ fn builtinCall(...@@ -3284,7 +3364,7 @@ fn builtinCall(
3284 },3364 },
3285 .compile_log => {3365 .compile_log => {
3286 const arena = scope.arena();3366 const arena = scope.arena();
3287 var targets = try arena.alloc(*zir.Inst, params.len);3367 var targets = try arena.alloc(zir.Inst.Ref, params.len);
3288 for (params) |param, param_i|3368 for (params) |param, param_i|
3289 targets[param_i] = try expr(mod, scope, .none, param);3369 targets[param_i] = try expr(mod, scope, .none, param);
3290 const result = try addZIRInst(mod, scope, src, zir.Inst.CompileLog, .{ .to_log = targets }, .{});3370 const result = try addZIRInst(mod, scope, src, zir.Inst.CompileLog, .{ .to_log = targets }, .{});
...@@ -3414,7 +3494,7 @@ fn callExpr(...@@ -3414,7 +3494,7 @@ fn callExpr(
3414 rl: ResultLoc,3494 rl: ResultLoc,
3415 node: ast.Node.Index,3495 node: ast.Node.Index,
3416 call: ast.full.Call,3496 call: ast.full.Call,
3417) InnerError!*zir.Inst {3497) InnerError!zir.Inst.Ref {
3418 if (true) {3498 if (true) {
3419 @panic("TODO update for zir-memory-layout branch");3499 @panic("TODO update for zir-memory-layout branch");
3420 }3500 }
...@@ -3459,7 +3539,7 @@ fn callExpr(...@@ -3459,7 +3539,7 @@ fn callExpr(
3459 return rvalue(mod, scope, rl, result); // TODO function call with result location3539 return rvalue(mod, scope, rl, result); // TODO function call with result location
3460}3540}
34613541
3462fn suspendExpr(mod: *Module, scope: *Scope, node: ast.Node.Index) InnerError!*zir.Inst {3542fn suspendExpr(mod: *Module, scope: *Scope, node: ast.Node.Index) InnerError!zir.Inst.Ref {
3463 const tree = scope.tree();3543 const tree = scope.tree();
3464 const src = tree.tokens.items(.start)[tree.nodes.items(.main_token)[node]];3544 const src = tree.tokens.items(.start)[tree.nodes.items(.main_token)[node]];
34653545
...@@ -3504,12 +3584,13 @@ fn suspendExpr(mod: *Module, scope: *Scope, node: ast.Node.Index) InnerError!*zi...@@ -3504,12 +3584,13 @@ fn suspendExpr(mod: *Module, scope: *Scope, node: ast.Node.Index) InnerError!*zi
3504 }3584 }
35053585
3506 const block = try addZIRInstBlock(mod, scope, src, .suspend_block, .{3586 const block = try addZIRInstBlock(mod, scope, src, .suspend_block, .{
3507 .instructions = try scope.arena().dupe(*zir.Inst, suspend_scope.instructions.items),3587 .instructions = try scope.arena().dupe(zir.Inst.Ref, suspend_scope.instructions.items),
3508 });3588 });
3509 return &block.base;3589 return &block.base;
3510}3590}
35113591
3512fn nosuspendExpr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) InnerError!*zir.Inst {3592fn nosuspendExpr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) InnerError!zir.Inst.Ref {
3593 if (true) @panic("TODO update for zir-memory-layout");
3513 const tree = scope.tree();3594 const tree = scope.tree();
3514 var child_scope = Scope.Nosuspend{3595 var child_scope = Scope.Nosuspend{
3515 .parent = scope,3596 .parent = scope,
...@@ -3520,7 +3601,8 @@ fn nosuspendExpr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Inde...@@ -3520,7 +3601,8 @@ fn nosuspendExpr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Inde
3520 return expr(mod, &child_scope.base, rl, tree.nodes.items(.data)[node].lhs);3601 return expr(mod, &child_scope.base, rl, tree.nodes.items(.data)[node].lhs);
3521}3602}
35223603
3523fn awaitExpr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) InnerError!*zir.Inst {3604fn awaitExpr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) InnerError!zir.Inst.Ref {
3605 if (true) @panic("TODO update for zir-memory-layout");
3524 const tree = scope.tree();3606 const tree = scope.tree();
3525 const src = tree.tokens.items(.start)[tree.nodes.items(.main_token)[node]];3607 const src = tree.tokens.items(.start)[tree.nodes.items(.main_token)[node]];
3526 const is_nosuspend = scope.getNosuspend() != null;3608 const is_nosuspend = scope.getNosuspend() != null;
...@@ -3542,7 +3624,7 @@ fn awaitExpr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) I...@@ -3542,7 +3624,7 @@ fn awaitExpr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) I
3542 return addZIRUnOp(mod, scope, src, if (is_nosuspend) .nosuspend_await else .@"await", operand);3624 return addZIRUnOp(mod, scope, src, if (is_nosuspend) .nosuspend_await else .@"await", operand);
3543}3625}
35443626
3545fn resumeExpr(mod: *Module, scope: *Scope, node: ast.Node.Index) InnerError!*zir.Inst {3627fn resumeExpr(mod: *Module, scope: *Scope, node: ast.Node.Index) InnerError!zir.Inst.Ref {
3546 const tree = scope.tree();3628 const tree = scope.tree();
3547 const src = tree.tokens.items(.start)[tree.nodes.items(.main_token)[node]];3629 const src = tree.tokens.items(.start)[tree.nodes.items(.main_token)[node]];
35483630
...@@ -3828,7 +3910,7 @@ fn rvalue(...@@ -3828,7 +3910,7 @@ fn rvalue(
3828 // We need a pointer but we have a value.3910 // We need a pointer but we have a value.
3829 const tree = scope.tree();3911 const tree = scope.tree();
3830 const src_token = tree.firstToken(src_node);3912 const src_token = tree.firstToken(src_node);
3831 return gz.addUnTok(.ref, result, src_tok);3913 return gz.addUnTok(.ref, result, src_token);
3832 },3914 },
3833 .ty => |ty_inst| return gz.addBin(.as, ty_inst, result),3915 .ty => |ty_inst| return gz.addBin(.as, ty_inst, result),
3834 .ptr => |ptr_inst| {3916 .ptr => |ptr_inst| {
...@@ -3844,31 +3926,12 @@ fn rvalue(...@@ -3844,31 +3926,12 @@ fn rvalue(
3844 },3926 },
3845 .block_ptr => |block_scope| {3927 .block_ptr => |block_scope| {
3846 block_scope.rvalue_rl_count += 1;3928 block_scope.rvalue_rl_count += 1;
3847 _ = try gz.addBin(.store_to_block_ptr, block_scope.rl_ptr.?, result);3929 _ = try gz.addBin(.store_to_block_ptr, block_scope.rl_ptr, result);
3848 return result;3930 return result;
3849 },3931 },
3850 }3932 }
3851}3933}
38523934
3853/// TODO when reworking ZIR memory layout, make the void value correspond to a hard coded
3854/// index; that way this does not actually need to allocate anything.
3855fn rvalueVoid(
3856 mod: *Module,
3857 scope: *Scope,
3858 rl: ResultLoc,
3859 node: ast.Node.Index,
3860 result: void,
3861) InnerError!*zir.Inst {
3862 const tree = scope.tree();
3863 const main_tokens = tree.nodes.items(.main_token);
3864 const src = tree.tokens.items(.start)[tree.firstToken(node)];
3865 const void_inst = try addZIRInstConst(mod, scope, src, .{
3866 .ty = Type.initTag(.void),
3867 .val = Value.initTag(.void_value),
3868 });
3869 return rvalue(mod, scope, rl, void_inst);
3870}
3871
3872fn rlStrategy(rl: ResultLoc, block_scope: *Scope.GenZir) ResultLoc.Strategy {3935fn rlStrategy(rl: ResultLoc, block_scope: *Scope.GenZir) ResultLoc.Strategy {
3873 var elide_store_to_block_ptr_instructions = false;3936 var elide_store_to_block_ptr_instructions = false;
3874 switch (rl) {3937 switch (rl) {
...@@ -3953,190 +4016,3 @@ fn setBlockResultLoc(block_scope: *Scope.GenZir, parent_rl: ResultLoc) void {...@@ -3953,190 +4016,3 @@ fn setBlockResultLoc(block_scope: *Scope.GenZir, parent_rl: ResultLoc) void {
3953 },4016 },
3954 }4017 }
3955}4018}
3956
3957pub fn addZirInstTag(
3958 mod: *Module,
3959 scope: *Scope,
3960 src: usize,
3961 comptime tag: zir.Inst.Tag,
3962 positionals: std.meta.fieldInfo(tag.Type(), .positionals).field_type,
3963) !*zir.Inst {
3964 const gen_zir = scope.getGenZir();
3965 try gen_zir.instructions.ensureCapacity(mod.gpa, gen_zir.instructions.items.len + 1);
3966 const inst = try gen_zir.arena.create(tag.Type());
3967 inst.* = .{
3968 .base = .{
3969 .tag = tag,
3970 .src = src,
3971 },
3972 .positionals = positionals,
3973 .kw_args = .{},
3974 };
3975 gen_zir.instructions.appendAssumeCapacity(&inst.base);
3976 return &inst.base;
3977}
3978
3979pub fn addZirInstT(
3980 mod: *Module,
3981 scope: *Scope,
3982 src: usize,
3983 comptime T: type,
3984 tag: zir.Inst.Tag,
3985 positionals: std.meta.fieldInfo(T, .positionals).field_type,
3986) !*T {
3987 const gen_zir = scope.getGenZir();
3988 try gen_zir.instructions.ensureCapacity(mod.gpa, gen_zir.instructions.items.len + 1);
3989 const inst = try gen_zir.arena.create(T);
3990 inst.* = .{
3991 .base = .{
3992 .tag = tag,
3993 .src = src,
3994 },
3995 .positionals = positionals,
3996 .kw_args = .{},
3997 };
3998 gen_zir.instructions.appendAssumeCapacity(&inst.base);
3999 return inst;
4000}
4001
4002pub fn addZIRInstSpecial(
4003 mod: *Module,
4004 scope: *Scope,
4005 src: usize,
4006 comptime T: type,
4007 positionals: std.meta.fieldInfo(T, .positionals).field_type,
4008 kw_args: std.meta.fieldInfo(T, .kw_args).field_type,
4009) !*T {
4010 const gen_zir = scope.getGenZir();
4011 try gen_zir.instructions.ensureCapacity(mod.gpa, gen_zir.instructions.items.len + 1);
4012 const inst = try gen_zir.arena.create(T);
4013 inst.* = .{
4014 .base = .{
4015 .tag = T.base_tag,
4016 .src = src,
4017 },
4018 .positionals = positionals,
4019 .kw_args = kw_args,
4020 };
4021 gen_zir.instructions.appendAssumeCapacity(&inst.base);
4022 return inst;
4023}
4024
4025pub fn addZIRNoOpT(mod: *Module, scope: *Scope, src: usize, tag: zir.Inst.Tag) !*zir.Inst.NoOp {
4026 const gen_zir = scope.getGenZir();
4027 try gen_zir.instructions.ensureCapacity(mod.gpa, gen_zir.instructions.items.len + 1);
4028 const inst = try gen_zir.arena.create(zir.Inst.NoOp);
4029 inst.* = .{
4030 .base = .{
4031 .tag = tag,
4032 .src = src,
4033 },
4034 .positionals = .{},
4035 .kw_args = .{},
4036 };
4037 gen_zir.instructions.appendAssumeCapacity(&inst.base);
4038 return inst;
4039}
4040
4041pub fn addZIRNoOp(mod: *Module, scope: *Scope, src: usize, tag: zir.Inst.Tag) !*zir.Inst {
4042 const inst = try addZIRNoOpT(mod, scope, src, tag);
4043 return &inst.base;
4044}
4045
4046pub fn addZIRUnOp(
4047 mod: *Module,
4048 scope: *Scope,
4049 src: usize,
4050 tag: zir.Inst.Tag,
4051 operand: *zir.Inst,
4052) !*zir.Inst {
4053 const gen_zir = scope.getGenZir();
4054 try gen_zir.instructions.ensureCapacity(mod.gpa, gen_zir.instructions.items.len + 1);
4055 const inst = try gen_zir.arena.create(zir.Inst.UnOp);
4056 inst.* = .{
4057 .base = .{
4058 .tag = tag,
4059 .src = src,
4060 },
4061 .positionals = .{
4062 .operand = operand,
4063 },
4064 .kw_args = .{},
4065 };
4066 gen_zir.instructions.appendAssumeCapacity(&inst.base);
4067 return &inst.base;
4068}
4069
4070pub fn addZIRBinOp(
4071 mod: *Module,
4072 scope: *Scope,
4073 src: usize,
4074 tag: zir.Inst.Tag,
4075 lhs: *zir.Inst,
4076 rhs: *zir.Inst,
4077) !*zir.Inst {
4078 const gen_zir = scope.getGenZir();
4079 try gen_zir.instructions.ensureCapacity(mod.gpa, gen_zir.instructions.items.len + 1);
4080 const inst = try gen_zir.arena.create(zir.Inst.BinOp);
4081 inst.* = .{
4082 .base = .{
4083 .tag = tag,
4084 .src = src,
4085 },
4086 .positionals = .{
4087 .lhs = lhs,
4088 .rhs = rhs,
4089 },
4090 .kw_args = .{},
4091 };
4092 gen_zir.instructions.appendAssumeCapacity(&inst.base);
4093 return &inst.base;
4094}
4095
4096pub fn addZIRInstBlock(
4097 mod: *Module,
4098 scope: *Scope,
4099 src: usize,
4100 tag: zir.Inst.Tag,
4101 body: zir.Body,
4102) !*zir.Inst.Block {
4103 const gen_zir = scope.getGenZir();
4104 try gen_zir.instructions.ensureCapacity(mod.gpa, gen_zir.instructions.items.len + 1);
4105 const inst = try gen_zir.arena.create(zir.Inst.Block);
4106 inst.* = .{
4107 .base = .{
4108 .tag = tag,
4109 .src = src,
4110 },
4111 .positionals = .{
4112 .body = body,
4113 },
4114 .kw_args = .{},
4115 };
4116 gen_zir.instructions.appendAssumeCapacity(&inst.base);
4117 return inst;
4118}
4119
4120pub fn addZIRInst(
4121 mod: *Module,
4122 scope: *Scope,
4123 src: usize,
4124 comptime T: type,
4125 positionals: std.meta.fieldInfo(T, .positionals).field_type,
4126 kw_args: std.meta.fieldInfo(T, .kw_args).field_type,
4127) !*zir.Inst {
4128 const inst_special = try addZIRInstSpecial(mod, scope, src, T, positionals, kw_args);
4129 return &inst_special.base;
4130}
4131
4132/// TODO The existence of this function is a workaround for a bug in stage1.
4133pub fn addZIRInstConst(mod: *Module, scope: *Scope, src: usize, typed_value: TypedValue) !*zir.Inst {
4134 const P = std.meta.fieldInfo(zir.Inst.Const, .positionals).field_type;
4135 return addZIRInst(mod, scope, src, zir.Inst.Const, P{ .typed_value = typed_value }, .{});
4136}
4137
4138/// TODO The existence of this function is a workaround for a bug in stage1.
4139pub fn addZIRInstLoop(mod: *Module, scope: *Scope, src: usize, body: zir.Body) !*zir.Inst.Loop {
4140 const P = std.meta.fieldInfo(zir.Inst.Loop, .positionals).field_type;
4141 return addZIRInstSpecial(mod, scope, src, zir.Inst.Loop, P{ .body = body }, .{});
4142}
src/codegen.zig+2-2
...@@ -499,7 +499,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {...@@ -499,7 +499,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
499 defer function.stack.deinit(bin_file.allocator);499 defer function.stack.deinit(bin_file.allocator);
500 defer function.exitlude_jump_relocs.deinit(bin_file.allocator);500 defer function.exitlude_jump_relocs.deinit(bin_file.allocator);
501501
502 var call_info = function.resolveCallingConventionValues(src_loc.byte_offset, fn_type) catch |err| switch (err) {502 var call_info = function.resolveCallingConventionValues(src_loc.lazy, fn_type) catch |err| switch (err) {
503 error.CodegenFail => return Result{ .fail = function.err_msg.? },503 error.CodegenFail => return Result{ .fail = function.err_msg.? },
504 else => |e| return e,504 else => |e| return e,
505 };505 };
...@@ -2850,7 +2850,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {...@@ -2850,7 +2850,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
2850 return self.fail(inst.base.src, "TODO implement support for more x86 assembly instructions", .{});2850 return self.fail(inst.base.src, "TODO implement support for more x86 assembly instructions", .{});
2851 }2851 }
28522852
2853 if (inst.output) |output| {2853 if (inst.output_name) |output| {
2854 if (output.len < 4 or output[0] != '=' or output[1] != '{' or output[output.len - 1] != '}') {2854 if (output.len < 4 or output[0] != '=' or output[1] != '{' or output[output.len - 1] != '}') {
2855 return self.fail(inst.base.src, "unrecognized asm output constraint: '{s}'", .{output});2855 return self.fail(inst.base.src, "unrecognized asm output constraint: '{s}'", .{output});
2856 }2856 }
src/codegen/c.zig+16-16
...@@ -14,6 +14,7 @@ const TypedValue = @import("../TypedValue.zig");...@@ -14,6 +14,7 @@ const TypedValue = @import("../TypedValue.zig");
14const C = link.File.C;14const C = link.File.C;
15const Decl = Module.Decl;15const Decl = Module.Decl;
16const trace = @import("../tracy.zig").trace;16const trace = @import("../tracy.zig").trace;
17const LazySrcLoc = Module.LazySrcLoc;
1718
18const Mutability = enum { Const, Mut };19const Mutability = enum { Const, Mut };
1920
...@@ -145,11 +146,10 @@ pub const DeclGen = struct {...@@ -145,11 +146,10 @@ pub const DeclGen = struct {
145 error_msg: ?*Module.ErrorMsg,146 error_msg: ?*Module.ErrorMsg,
146 typedefs: TypedefMap,147 typedefs: TypedefMap,
147148
148 fn fail(dg: *DeclGen, src: usize, comptime format: []const u8, args: anytype) error{ AnalysisFail, OutOfMemory } {149 fn fail(dg: *DeclGen, src: LazySrcLoc, comptime format: []const u8, args: anytype) error{ AnalysisFail, OutOfMemory } {
149 dg.error_msg = try Module.ErrorMsg.create(dg.module.gpa, .{150 @setCold(true);
150 .file_scope = dg.decl.getFileScope(),151 const src_loc = src.toSrcLocWithDecl(dg.decl);
151 .byte_offset = src,152 dg.error_msg = try Module.ErrorMsg.create(dg.module.gpa, src_loc, format, args);
152 }, format, args);
153 return error.AnalysisFail;153 return error.AnalysisFail;
154 }154 }
155155
...@@ -160,7 +160,7 @@ pub const DeclGen = struct {...@@ -160,7 +160,7 @@ pub const DeclGen = struct {
160 val: Value,160 val: Value,
161 ) error{ OutOfMemory, AnalysisFail }!void {161 ) error{ OutOfMemory, AnalysisFail }!void {
162 if (val.isUndef()) {162 if (val.isUndef()) {
163 return dg.fail(dg.decl.src(), "TODO: C backend: properly handle undefined in all cases (with debug safety?)", .{});163 return dg.fail(.{ .node_offset = 0 }, "TODO: C backend: properly handle undefined in all cases (with debug safety?)", .{});
164 }164 }
165 switch (t.zigTypeTag()) {165 switch (t.zigTypeTag()) {
166 .Int => {166 .Int => {
...@@ -193,7 +193,7 @@ pub const DeclGen = struct {...@@ -193,7 +193,7 @@ pub const DeclGen = struct {
193 try writer.print("{s}", .{decl.name});193 try writer.print("{s}", .{decl.name});
194 },194 },
195 else => |e| return dg.fail(195 else => |e| return dg.fail(
196 dg.decl.src(),196 .{ .node_offset = 0 },
197 "TODO: C backend: implement Pointer value {s}",197 "TODO: C backend: implement Pointer value {s}",
198 .{@tagName(e)},198 .{@tagName(e)},
199 ),199 ),
...@@ -276,7 +276,7 @@ pub const DeclGen = struct {...@@ -276,7 +276,7 @@ pub const DeclGen = struct {
276 try writer.writeAll(", .error = 0 }");276 try writer.writeAll(", .error = 0 }");
277 }277 }
278 },278 },
279 else => |e| return dg.fail(dg.decl.src(), "TODO: C backend: implement value {s}", .{279 else => |e| return dg.fail(.{ .node_offset = 0 }, "TODO: C backend: implement value {s}", .{
280 @tagName(e),280 @tagName(e),
281 }),281 }),
282 }282 }
...@@ -350,7 +350,7 @@ pub const DeclGen = struct {...@@ -350,7 +350,7 @@ pub const DeclGen = struct {
350 break;350 break;
351 }351 }
352 } else {352 } else {
353 return dg.fail(dg.decl.src(), "TODO: C backend: implement integer types larger than 128 bits", .{});353 return dg.fail(.{ .node_offset = 0 }, "TODO: C backend: implement integer types larger than 128 bits", .{});
354 }354 }
355 },355 },
356 else => unreachable,356 else => unreachable,
...@@ -358,7 +358,7 @@ pub const DeclGen = struct {...@@ -358,7 +358,7 @@ pub const DeclGen = struct {
358 },358 },
359 .Pointer => {359 .Pointer => {
360 if (t.isSlice()) {360 if (t.isSlice()) {
361 return dg.fail(dg.decl.src(), "TODO: C backend: implement slices", .{});361 return dg.fail(.{ .node_offset = 0 }, "TODO: C backend: implement slices", .{});
362 } else {362 } else {
363 try dg.renderType(w, t.elemType());363 try dg.renderType(w, t.elemType());
364 try w.writeAll(" *");364 try w.writeAll(" *");
...@@ -431,7 +431,7 @@ pub const DeclGen = struct {...@@ -431,7 +431,7 @@ pub const DeclGen = struct {
431 dg.typedefs.putAssumeCapacityNoClobber(t, .{ .name = name, .rendered = rendered });431 dg.typedefs.putAssumeCapacityNoClobber(t, .{ .name = name, .rendered = rendered });
432 },432 },
433 .Null, .Undefined => unreachable, // must be const or comptime433 .Null, .Undefined => unreachable, // must be const or comptime
434 else => |e| return dg.fail(dg.decl.src(), "TODO: C backend: implement type {s}", .{434 else => |e| return dg.fail(.{ .node_offset = 0 }, "TODO: C backend: implement type {s}", .{
435 @tagName(e),435 @tagName(e),
436 }),436 }),
437 }437 }
...@@ -575,7 +575,7 @@ pub fn genBody(o: *Object, body: ir.Body) error{ AnalysisFail, OutOfMemory }!voi...@@ -575,7 +575,7 @@ pub fn genBody(o: *Object, body: ir.Body) error{ AnalysisFail, OutOfMemory }!voi
575 .unwrap_errunion_err_ptr => try genUnwrapErrUnionErr(o, inst.castTag(.unwrap_errunion_err_ptr).?),575 .unwrap_errunion_err_ptr => try genUnwrapErrUnionErr(o, inst.castTag(.unwrap_errunion_err_ptr).?),
576 .wrap_errunion_payload => try genWrapErrUnionPay(o, inst.castTag(.wrap_errunion_payload).?),576 .wrap_errunion_payload => try genWrapErrUnionPay(o, inst.castTag(.wrap_errunion_payload).?),
577 .wrap_errunion_err => try genWrapErrUnionErr(o, inst.castTag(.wrap_errunion_err).?),577 .wrap_errunion_err => try genWrapErrUnionErr(o, inst.castTag(.wrap_errunion_err).?),
578 else => |e| return o.dg.fail(o.dg.decl.src(), "TODO: C backend: implement codegen for {}", .{e}),578 else => |e| return o.dg.fail(.{ .node_offset = 0 }, "TODO: C backend: implement codegen for {}", .{e}),
579 };579 };
580 switch (result_value) {580 switch (result_value) {
581 .none => {},581 .none => {},
...@@ -756,7 +756,7 @@ fn genCall(o: *Object, inst: *Inst.Call) !CValue {...@@ -756,7 +756,7 @@ fn genCall(o: *Object, inst: *Inst.Call) !CValue {
756 try writer.writeAll(");\n");756 try writer.writeAll(");\n");
757 return result_local;757 return result_local;
758 } else {758 } else {
759 return o.dg.fail(o.dg.decl.src(), "TODO: C backend: implement function pointers", .{});759 return o.dg.fail(.{ .node_offset = 0 }, "TODO: C backend: implement function pointers", .{});
760 }760 }
761}761}
762762
...@@ -913,13 +913,13 @@ fn genAsm(o: *Object, as: *Inst.Assembly) !CValue {...@@ -913,13 +913,13 @@ fn genAsm(o: *Object, as: *Inst.Assembly) !CValue {
913 try o.writeCValue(writer, arg_c_value);913 try o.writeCValue(writer, arg_c_value);
914 try writer.writeAll(";\n");914 try writer.writeAll(";\n");
915 } else {915 } else {
916 return o.dg.fail(o.dg.decl.src(), "TODO non-explicit inline asm regs", .{});916 return o.dg.fail(.{ .node_offset = 0 }, "TODO non-explicit inline asm regs", .{});
917 }917 }
918 }918 }
919 const volatile_string: []const u8 = if (as.is_volatile) "volatile " else "";919 const volatile_string: []const u8 = if (as.is_volatile) "volatile " else "";
920 try writer.print("__asm {s}(\"{s}\"", .{ volatile_string, as.asm_source });920 try writer.print("__asm {s}(\"{s}\"", .{ volatile_string, as.asm_source });
921 if (as.output) |_| {921 if (as.output) |_| {
922 return o.dg.fail(o.dg.decl.src(), "TODO inline asm output", .{});922 return o.dg.fail(.{ .node_offset = 0 }, "TODO inline asm output", .{});
923 }923 }
924 if (as.inputs.len > 0) {924 if (as.inputs.len > 0) {
925 if (as.output == null) {925 if (as.output == null) {
...@@ -945,7 +945,7 @@ fn genAsm(o: *Object, as: *Inst.Assembly) !CValue {...@@ -945,7 +945,7 @@ fn genAsm(o: *Object, as: *Inst.Assembly) !CValue {
945 if (as.base.isUnused())945 if (as.base.isUnused())
946 return CValue.none;946 return CValue.none;
947947
948 return o.dg.fail(o.dg.decl.src(), "TODO: C backend: inline asm expression result used", .{});948 return o.dg.fail(.{ .node_offset = 0 }, "TODO: C backend: inline asm expression result used", .{});
949}949}
950950
951fn genIsNull(o: *Object, inst: *Inst.UnOp) !CValue {951fn genIsNull(o: *Object, inst: *Inst.UnOp) !CValue {
src/codegen/wasm.zig+9-10
...@@ -14,6 +14,7 @@ const Type = @import("../type.zig").Type;...@@ -14,6 +14,7 @@ const Type = @import("../type.zig").Type;
14const Value = @import("../value.zig").Value;14const Value = @import("../value.zig").Value;
15const Compilation = @import("../Compilation.zig");15const Compilation = @import("../Compilation.zig");
16const AnyMCValue = @import("../codegen.zig").AnyMCValue;16const AnyMCValue = @import("../codegen.zig").AnyMCValue;
17const LazySrcLoc = Module.LazySrcLoc;
1718
18/// Wasm Value, created when generating an instruction19/// Wasm Value, created when generating an instruction
19const WValue = union(enum) {20const WValue = union(enum) {
...@@ -70,11 +71,9 @@ pub const Context = struct {...@@ -70,11 +71,9 @@ pub const Context = struct {
70 }71 }
7172
72 /// Sets `err_msg` on `Context` and returns `error.CodegemFail` which is caught in link/Wasm.zig73 /// Sets `err_msg` on `Context` and returns `error.CodegemFail` which is caught in link/Wasm.zig
73 fn fail(self: *Context, src: usize, comptime fmt: []const u8, args: anytype) InnerError {74 fn fail(self: *Context, src: LazySrcLoc, comptime fmt: []const u8, args: anytype) InnerError {
74 self.err_msg = try Module.ErrorMsg.create(self.gpa, .{75 const src_loc = src.toSrcLocWithDecl(self.decl);
75 .file_scope = self.decl.getFileScope(),76 self.err_msg = try Module.ErrorMsg.create(self.gpa, src_loc, fmt, args);
76 .byte_offset = src,
77 }, fmt, args);
78 return error.CodegenFail;77 return error.CodegenFail;
79 }78 }
8079
...@@ -91,7 +90,7 @@ pub const Context = struct {...@@ -91,7 +90,7 @@ pub const Context = struct {
91 }90 }
9291
93 /// Using a given `Type`, returns the corresponding wasm value type92 /// Using a given `Type`, returns the corresponding wasm value type
94 fn genValtype(self: *Context, src: usize, ty: Type) InnerError!u8 {93 fn genValtype(self: *Context, src: LazySrcLoc, ty: Type) InnerError!u8 {
95 return switch (ty.tag()) {94 return switch (ty.tag()) {
96 .f32 => wasm.valtype(.f32),95 .f32 => wasm.valtype(.f32),
97 .f64 => wasm.valtype(.f64),96 .f64 => wasm.valtype(.f64),
...@@ -104,7 +103,7 @@ pub const Context = struct {...@@ -104,7 +103,7 @@ pub const Context = struct {
104 /// Using a given `Type`, returns the corresponding wasm value type103 /// Using a given `Type`, returns the corresponding wasm value type
105 /// Differently from `genValtype` this also allows `void` to create a block104 /// Differently from `genValtype` this also allows `void` to create a block
106 /// with no return type105 /// with no return type
107 fn genBlockType(self: *Context, src: usize, ty: Type) InnerError!u8 {106 fn genBlockType(self: *Context, src: LazySrcLoc, ty: Type) InnerError!u8 {
108 return switch (ty.tag()) {107 return switch (ty.tag()) {
109 .void, .noreturn => wasm.block_empty,108 .void, .noreturn => wasm.block_empty,
110 else => self.genValtype(src, ty),109 else => self.genValtype(src, ty),
...@@ -139,7 +138,7 @@ pub const Context = struct {...@@ -139,7 +138,7 @@ pub const Context = struct {
139 ty.fnParamTypes(params);138 ty.fnParamTypes(params);
140 for (params) |param_type| {139 for (params) |param_type| {
141 // Can we maybe get the source index of each param?140 // Can we maybe get the source index of each param?
142 const val_type = try self.genValtype(self.decl.src(), param_type);141 const val_type = try self.genValtype(.{ .node_offset = 0 }, param_type);
143 try writer.writeByte(val_type);142 try writer.writeByte(val_type);
144 }143 }
145 }144 }
...@@ -151,7 +150,7 @@ pub const Context = struct {...@@ -151,7 +150,7 @@ pub const Context = struct {
151 else => |ret_type| {150 else => |ret_type| {
152 try leb.writeULEB128(writer, @as(u32, 1));151 try leb.writeULEB128(writer, @as(u32, 1));
153 // Can we maybe get the source index of the return type?152 // Can we maybe get the source index of the return type?
154 const val_type = try self.genValtype(self.decl.src(), return_type);153 const val_type = try self.genValtype(.{ .node_offset = 0 }, return_type);
155 try writer.writeByte(val_type);154 try writer.writeByte(val_type);
156 },155 },
157 }156 }
...@@ -168,7 +167,7 @@ pub const Context = struct {...@@ -168,7 +167,7 @@ pub const Context = struct {
168 const mod_fn = blk: {167 const mod_fn = blk: {
169 if (tv.val.castTag(.function)) |func| break :blk func.data;168 if (tv.val.castTag(.function)) |func| break :blk func.data;
170 if (tv.val.castTag(.extern_fn)) |ext_fn| return; // don't need codegen for extern functions169 if (tv.val.castTag(.extern_fn)) |ext_fn| return; // don't need codegen for extern functions
171 return self.fail(self.decl.src(), "TODO: Wasm codegen for decl type '{s}'", .{tv.ty.tag()});170 return self.fail(.{ .node_offset = 0 }, "TODO: Wasm codegen for decl type '{s}'", .{tv.ty.tag()});
172 };171 };
173172
174 // Reserve space to write the size after generating the code as well as space for locals count173 // Reserve space to write the size after generating the code as well as space for locals count
src/type.zig+1-1
...@@ -3150,7 +3150,7 @@ pub const Type = extern union {...@@ -3150,7 +3150,7 @@ pub const Type = extern union {
3150 => unreachable,3150 => unreachable,
31513151
3152 .empty_struct => self.castTag(.empty_struct).?.data,3152 .empty_struct => self.castTag(.empty_struct).?.data,
3153 .@"opaque" => &self.castTag(.@"opaque").?.scope,3153 .@"opaque" => &self.castTag(.@"opaque").?.data,
3154 };3154 };
3155 }3155 }
31563156
src/zir.zig+127-121
...@@ -35,7 +35,7 @@ pub const Code = struct {...@@ -35,7 +35,7 @@ pub const Code = struct {
35 extra: []u32,35 extra: []u32,
36 /// First ZIR instruction in this `Code`.36 /// First ZIR instruction in this `Code`.
37 /// `extra` at this index contains a `Ref` for every root member.37 /// `extra` at this index contains a `Ref` for every root member.
38 root_start: Inst.Index,38 root_start: u32,
39 /// Number of ZIR instructions in the implicit root block of the `Code`.39 /// Number of ZIR instructions in the implicit root block of the `Code`.
40 root_len: u32,40 root_len: u32,
4141
...@@ -138,204 +138,205 @@ pub const Const = enum {...@@ -138,204 +138,205 @@ pub const Const = enum {
138 bool_false,138 bool_false,
139};139};
140140
141pub const const_inst_list = enumArray(Const, .{141pub const const_inst_list = std.enums.directEnumArray(Const, TypedValue, 0, .{
142 .u8_type = @as(TypedValue, .{142 .unused = undefined,
143 .u8_type = .{
143 .ty = Type.initTag(.type),144 .ty = Type.initTag(.type),
144 .val = Value.initTag(.u8_type),145 .val = Value.initTag(.u8_type),
145 }),146 },
146 .i8_type = @as(TypedValue, .{147 .i8_type = .{
147 .ty = Type.initTag(.type),148 .ty = Type.initTag(.type),
148 .val = Value.initTag(.i8_type),149 .val = Value.initTag(.i8_type),
149 }),150 },
150 .u16_type = @as(TypedValue, .{151 .u16_type = .{
151 .ty = Type.initTag(.type),152 .ty = Type.initTag(.type),
152 .val = Value.initTag(.u16_type),153 .val = Value.initTag(.u16_type),
153 }),154 },
154 .i16_type = @as(TypedValue, .{155 .i16_type = .{
155 .ty = Type.initTag(.type),156 .ty = Type.initTag(.type),
156 .val = Value.initTag(.i16_type),157 .val = Value.initTag(.i16_type),
157 }),158 },
158 .u32_type = @as(TypedValue, .{159 .u32_type = .{
159 .ty = Type.initTag(.type),160 .ty = Type.initTag(.type),
160 .val = Value.initTag(.u32_type),161 .val = Value.initTag(.u32_type),
161 }),162 },
162 .i32_type = @as(TypedValue, .{163 .i32_type = .{
163 .ty = Type.initTag(.type),164 .ty = Type.initTag(.type),
164 .val = Value.initTag(.i32_type),165 .val = Value.initTag(.i32_type),
165 }),166 },
166 .u64_type = @as(TypedValue, .{167 .u64_type = .{
167 .ty = Type.initTag(.type),168 .ty = Type.initTag(.type),
168 .val = Value.initTag(.u64_type),169 .val = Value.initTag(.u64_type),
169 }),170 },
170 .i64_type = @as(TypedValue, .{171 .i64_type = .{
171 .ty = Type.initTag(.type),172 .ty = Type.initTag(.type),
172 .val = Value.initTag(.i64_type),173 .val = Value.initTag(.i64_type),
173 }),174 },
174 .usize_type = @as(TypedValue, .{175 .usize_type = .{
175 .ty = Type.initTag(.type),176 .ty = Type.initTag(.type),
176 .val = Value.initTag(.usize_type),177 .val = Value.initTag(.usize_type),
177 }),178 },
178 .isize_type = @as(TypedValue, .{179 .isize_type = .{
179 .ty = Type.initTag(.type),180 .ty = Type.initTag(.type),
180 .val = Value.initTag(.isize_type),181 .val = Value.initTag(.isize_type),
181 }),182 },
182 .c_short_type = @as(TypedValue, .{183 .c_short_type = .{
183 .ty = Type.initTag(.type),184 .ty = Type.initTag(.type),
184 .val = Value.initTag(.c_short_type),185 .val = Value.initTag(.c_short_type),
185 }),186 },
186 .c_ushort_type = @as(TypedValue, .{187 .c_ushort_type = .{
187 .ty = Type.initTag(.type),188 .ty = Type.initTag(.type),
188 .val = Value.initTag(.c_ushort_type),189 .val = Value.initTag(.c_ushort_type),
189 }),190 },
190 .c_int_type = @as(TypedValue, .{191 .c_int_type = .{
191 .ty = Type.initTag(.type),192 .ty = Type.initTag(.type),
192 .val = Value.initTag(.c_int_type),193 .val = Value.initTag(.c_int_type),
193 }),194 },
194 .c_uint_type = @as(TypedValue, .{195 .c_uint_type = .{
195 .ty = Type.initTag(.type),196 .ty = Type.initTag(.type),
196 .val = Value.initTag(.c_uint_type),197 .val = Value.initTag(.c_uint_type),
197 }),198 },
198 .c_long_type = @as(TypedValue, .{199 .c_long_type = .{
199 .ty = Type.initTag(.type),200 .ty = Type.initTag(.type),
200 .val = Value.initTag(.c_long_type),201 .val = Value.initTag(.c_long_type),
201 }),202 },
202 .c_ulong_type = @as(TypedValue, .{203 .c_ulong_type = .{
203 .ty = Type.initTag(.type),204 .ty = Type.initTag(.type),
204 .val = Value.initTag(.c_ulong_type),205 .val = Value.initTag(.c_ulong_type),
205 }),206 },
206 .c_longlong_type = @as(TypedValue, .{207 .c_longlong_type = .{
207 .ty = Type.initTag(.type),208 .ty = Type.initTag(.type),
208 .val = Value.initTag(.c_longlong_type),209 .val = Value.initTag(.c_longlong_type),
209 }),210 },
210 .c_ulonglong_type = @as(TypedValue, .{211 .c_ulonglong_type = .{
211 .ty = Type.initTag(.type),212 .ty = Type.initTag(.type),
212 .val = Value.initTag(.c_ulonglong_type),213 .val = Value.initTag(.c_ulonglong_type),
213 }),214 },
214 .c_longdouble_type = @as(TypedValue, .{215 .c_longdouble_type = .{
215 .ty = Type.initTag(.type),216 .ty = Type.initTag(.type),
216 .val = Value.initTag(.c_longdouble_type),217 .val = Value.initTag(.c_longdouble_type),
217 }),218 },
218 .f16_type = @as(TypedValue, .{219 .f16_type = .{
219 .ty = Type.initTag(.type),220 .ty = Type.initTag(.type),
220 .val = Value.initTag(.f16_type),221 .val = Value.initTag(.f16_type),
221 }),222 },
222 .f32_type = @as(TypedValue, .{223 .f32_type = .{
223 .ty = Type.initTag(.type),224 .ty = Type.initTag(.type),
224 .val = Value.initTag(.f32_type),225 .val = Value.initTag(.f32_type),
225 }),226 },
226 .f64_type = @as(TypedValue, .{227 .f64_type = .{
227 .ty = Type.initTag(.type),228 .ty = Type.initTag(.type),
228 .val = Value.initTag(.f64_type),229 .val = Value.initTag(.f64_type),
229 }),230 },
230 .f128_type = @as(TypedValue, .{231 .f128_type = .{
231 .ty = Type.initTag(.type),232 .ty = Type.initTag(.type),
232 .val = Value.initTag(.f128_type),233 .val = Value.initTag(.f128_type),
233 }),234 },
234 .c_void_type = @as(TypedValue, .{235 .c_void_type = .{
235 .ty = Type.initTag(.type),236 .ty = Type.initTag(.type),
236 .val = Value.initTag(.c_void_type),237 .val = Value.initTag(.c_void_type),
237 }),238 },
238 .bool_type = @as(TypedValue, .{239 .bool_type = .{
239 .ty = Type.initTag(.type),240 .ty = Type.initTag(.type),
240 .val = Value.initTag(.bool_type),241 .val = Value.initTag(.bool_type),
241 }),242 },
242 .void_type = @as(TypedValue, .{243 .void_type = .{
243 .ty = Type.initTag(.type),244 .ty = Type.initTag(.type),
244 .val = Value.initTag(.void_type),245 .val = Value.initTag(.void_type),
245 }),246 },
246 .type_type = @as(TypedValue, .{247 .type_type = .{
247 .ty = Type.initTag(.type),248 .ty = Type.initTag(.type),
248 .val = Value.initTag(.type_type),249 .val = Value.initTag(.type_type),
249 }),250 },
250 .anyerror_type = @as(TypedValue, .{251 .anyerror_type = .{
251 .ty = Type.initTag(.type),252 .ty = Type.initTag(.type),
252 .val = Value.initTag(.anyerror_type),253 .val = Value.initTag(.anyerror_type),
253 }),254 },
254 .comptime_int_type = @as(TypedValue, .{255 .comptime_int_type = .{
255 .ty = Type.initTag(.type),256 .ty = Type.initTag(.type),
256 .val = Value.initTag(.comptime_int_type),257 .val = Value.initTag(.comptime_int_type),
257 }),258 },
258 .comptime_float_type = @as(TypedValue, .{259 .comptime_float_type = .{
259 .ty = Type.initTag(.type),260 .ty = Type.initTag(.type),
260 .val = Value.initTag(.comptime_float_type),261 .val = Value.initTag(.comptime_float_type),
261 }),262 },
262 .noreturn_type = @as(TypedValue, .{263 .noreturn_type = .{
263 .ty = Type.initTag(.type),264 .ty = Type.initTag(.type),
264 .val = Value.initTag(.noreturn_type),265 .val = Value.initTag(.noreturn_type),
265 }),266 },
266 .null_type = @as(TypedValue, .{267 .null_type = .{
267 .ty = Type.initTag(.type),268 .ty = Type.initTag(.type),
268 .val = Value.initTag(.null_type),269 .val = Value.initTag(.null_type),
269 }),270 },
270 .undefined_type = @as(TypedValue, .{271 .undefined_type = .{
271 .ty = Type.initTag(.type),272 .ty = Type.initTag(.type),
272 .val = Value.initTag(.undefined_type),273 .val = Value.initTag(.undefined_type),
273 }),274 },
274 .fn_noreturn_no_args_type = @as(TypedValue, .{275 .fn_noreturn_no_args_type = .{
275 .ty = Type.initTag(.type),276 .ty = Type.initTag(.type),
276 .val = Value.initTag(.fn_noreturn_no_args_type),277 .val = Value.initTag(.fn_noreturn_no_args_type),
277 }),278 },
278 .fn_void_no_args_type = @as(TypedValue, .{279 .fn_void_no_args_type = .{
279 .ty = Type.initTag(.type),280 .ty = Type.initTag(.type),
280 .val = Value.initTag(.fn_void_no_args_type),281 .val = Value.initTag(.fn_void_no_args_type),
281 }),282 },
282 .fn_naked_noreturn_no_args_type = @as(TypedValue, .{283 .fn_naked_noreturn_no_args_type = .{
283 .ty = Type.initTag(.type),284 .ty = Type.initTag(.type),
284 .val = Value.initTag(.fn_naked_noreturn_no_args_type),285 .val = Value.initTag(.fn_naked_noreturn_no_args_type),
285 }),286 },
286 .fn_ccc_void_no_args_type = @as(TypedValue, .{287 .fn_ccc_void_no_args_type = .{
287 .ty = Type.initTag(.type),288 .ty = Type.initTag(.type),
288 .val = Value.initTag(.fn_ccc_void_no_args_type),289 .val = Value.initTag(.fn_ccc_void_no_args_type),
289 }),290 },
290 .single_const_pointer_to_comptime_int_type = @as(TypedValue, .{291 .single_const_pointer_to_comptime_int_type = .{
291 .ty = Type.initTag(.type),292 .ty = Type.initTag(.type),
292 .val = Value.initTag(.single_const_pointer_to_comptime_int_type),293 .val = Value.initTag(.single_const_pointer_to_comptime_int_type),
293 }),294 },
294 .const_slice_u8_type = @as(TypedValue, .{295 .const_slice_u8_type = .{
295 .ty = Type.initTag(.type),296 .ty = Type.initTag(.type),
296 .val = Value.initTag(.const_slice_u8_type),297 .val = Value.initTag(.const_slice_u8_type),
297 }),298 },
298 .enum_literal_type = @as(TypedValue, .{299 .enum_literal_type = .{
299 .ty = Type.initTag(.type),300 .ty = Type.initTag(.type),
300 .val = Value.initTag(.enum_literal_type),301 .val = Value.initTag(.enum_literal_type),
301 }),302 },
302 .anyframe_type = @as(TypedValue, .{303 .anyframe_type = .{
303 .ty = Type.initTag(.type),304 .ty = Type.initTag(.type),
304 .val = Value.initTag(.anyframe_type),305 .val = Value.initTag(.anyframe_type),
305 }),306 },
306307
307 .undef = @as(TypedValue, .{308 .undef = .{
308 .ty = Type.initTag(.@"undefined"),309 .ty = Type.initTag(.@"undefined"),
309 .val = Value.initTag(.undef),310 .val = Value.initTag(.undef),
310 }),311 },
311 .zero = @as(TypedValue, .{312 .zero = .{
312 .ty = Type.initTag(.comptime_int),313 .ty = Type.initTag(.comptime_int),
313 .val = Value.initTag(.zero),314 .val = Value.initTag(.zero),
314 }),315 },
315 .one = @as(TypedValue, .{316 .one = .{
316 .ty = Type.initTag(.comptime_int),317 .ty = Type.initTag(.comptime_int),
317 .val = Value.initTag(.one),318 .val = Value.initTag(.one),
318 }),319 },
319 .void_value = @as(TypedValue, .{320 .void_value = .{
320 .ty = Type.initTag(.void),321 .ty = Type.initTag(.void),
321 .val = Value.initTag(.void_value),322 .val = Value.initTag(.void_value),
322 }),323 },
323 .unreachable_value = @as(TypedValue, .{324 .unreachable_value = .{
324 .ty = Type.initTag(.noreturn),325 .ty = Type.initTag(.noreturn),
325 .val = Value.initTag(.unreachable_value),326 .val = Value.initTag(.unreachable_value),
326 }),327 },
327 .null_value = @as(TypedValue, .{328 .null_value = .{
328 .ty = Type.initTag(.@"null"),329 .ty = Type.initTag(.@"null"),
329 .val = Value.initTag(.null_value),330 .val = Value.initTag(.null_value),
330 }),331 },
331 .bool_true = @as(TypedValue, .{332 .bool_true = .{
332 .ty = Type.initTag(.bool),333 .ty = Type.initTag(.bool),
333 .val = Value.initTag(.bool_true),334 .val = Value.initTag(.bool_true),
334 }),335 },
335 .bool_false = @as(TypedValue, .{336 .bool_false = .{
336 .ty = Type.initTag(.bool),337 .ty = Type.initTag(.bool),
337 .val = Value.initTag(.bool_false),338 .val = Value.initTag(.bool_false),
338 }),339 },
339});340});
340341
341/// These are untyped instructions generated from an Abstract Syntax Tree.342/// These are untyped instructions generated from an Abstract Syntax Tree.
...@@ -633,7 +634,7 @@ pub const Inst = struct {...@@ -633,7 +634,7 @@ pub const Inst = struct {
633 /// Sends control flow back to the function's callee.634 /// Sends control flow back to the function's callee.
634 /// Includes an operand as the return value.635 /// Includes an operand as the return value.
635 /// Includes a token source location.636 /// Includes a token source location.
636 /// Uses the un_tok union field.637 /// Uses the `un_tok` union field.
637 ret_tok,638 ret_tok,
638 /// Changes the maximum number of backwards branches that compile-time639 /// Changes the maximum number of backwards branches that compile-time
639 /// code execution can use before giving up and making a compile error.640 /// code execution can use before giving up and making a compile error.
...@@ -755,6 +756,9 @@ pub const Inst = struct {...@@ -755,6 +756,9 @@ pub const Inst = struct {
755 ensure_err_payload_void,756 ensure_err_payload_void,
756 /// An enum literal. Uses the `str_tok` union field.757 /// An enum literal. Uses the `str_tok` union field.
757 enum_literal,758 enum_literal,
759 /// An enum literal 8 or fewer bytes. No source location.
760 /// Uses the `small_str` field.
761 enum_literal_small,
758 /// Suspend an async function. The suspend block has 0 or 1 statements in it.762 /// Suspend an async function. The suspend block has 0 or 1 statements in it.
759 /// Uses the `un_node` union field.763 /// Uses the `un_node` union field.
760 suspend_block_one,764 suspend_block_one,
...@@ -816,6 +820,7 @@ pub const Inst = struct {...@@ -816,6 +820,7 @@ pub const Inst = struct {
816 .indexable_ptr_len,820 .indexable_ptr_len,
817 .as,821 .as,
818 .@"asm",822 .@"asm",
823 .asm_volatile,
819 .bit_and,824 .bit_and,
820 .bitcast,825 .bitcast,
821 .bitcast_ref,826 .bitcast_ref,
...@@ -831,12 +836,9 @@ pub const Inst = struct {...@@ -831,12 +836,9 @@ pub const Inst = struct {
831 .breakpoint,836 .breakpoint,
832 .call,837 .call,
833 .call_async_kw,838 .call_async_kw,
834 .call_never_tail,
835 .call_never_inline,
836 .call_no_async,839 .call_no_async,
837 .call_always_tail,
838 .call_always_inline,
839 .call_compile_time,840 .call_compile_time,
841 .call_none,
840 .cmp_lt,842 .cmp_lt,
841 .cmp_lte,843 .cmp_lte,
842 .cmp_eq,844 .cmp_eq,
...@@ -845,13 +847,15 @@ pub const Inst = struct {...@@ -845,13 +847,15 @@ pub const Inst = struct {
845 .cmp_neq,847 .cmp_neq,
846 .coerce_result_ptr,848 .coerce_result_ptr,
847 .@"const",849 .@"const",
848 .dbg_stmt,850 .dbg_stmt_node,
849 .decl_ref,851 .decl_ref,
850 .decl_val,852 .decl_val,
851 .deref_node,853 .deref_node,
852 .div,854 .div,
853 .elem_ptr,855 .elem_ptr,
854 .elem_val,856 .elem_val,
857 .elem_ptr_node,
858 .elem_val_node,
855 .ensure_result_used,859 .ensure_result_used,
856 .ensure_result_non_error,860 .ensure_result_non_error,
857 .floatcast,861 .floatcast,
...@@ -882,14 +886,6 @@ pub const Inst = struct {...@@ -882,14 +886,6 @@ pub const Inst = struct {
882 .ret_type,886 .ret_type,
883 .shl,887 .shl,
884 .shr,888 .shr,
885 .single_const_ptr_type,
886 .single_mut_ptr_type,
887 .many_const_ptr_type,
888 .many_mut_ptr_type,
889 .c_const_ptr_type,
890 .c_mut_ptr_type,
891 .mut_slice_type,
892 .const_slice_type,
893 .store,889 .store,
894 .store_to_block_ptr,890 .store_to_block_ptr,
895 .store_to_inferred_ptr,891 .store_to_inferred_ptr,
...@@ -914,20 +910,21 @@ pub const Inst = struct {...@@ -914,20 +910,21 @@ pub const Inst = struct {
914 .ptr_type_simple,910 .ptr_type_simple,
915 .ensure_err_payload_void,911 .ensure_err_payload_void,
916 .enum_literal,912 .enum_literal,
913 .enum_literal_small,
917 .merge_error_sets,914 .merge_error_sets,
918 .anyframe_type,915 .anyframe_type,
919 .error_union_type,916 .error_union_type,
920 .bit_not,917 .bit_not,
921 .error_set,918 .error_set,
922 .error_value,919 .error_value,
923 .slice,
924 .slice_start,920 .slice_start,
921 .slice_end,
922 .slice_sentinel,
925 .import,923 .import,
926 .typeof_peer,924 .typeof_peer,
927 .resolve_inferred_alloc,925 .resolve_inferred_alloc,
928 .set_eval_branch_quota,926 .set_eval_branch_quota,
929 .compile_log,927 .compile_log,
930 .switch_range,
931 .@"resume",928 .@"resume",
932 .@"await",929 .@"await",
933 .nosuspend_await,930 .nosuspend_await,
...@@ -942,11 +939,8 @@ pub const Inst = struct {...@@ -942,11 +939,8 @@ pub const Inst = struct {
942 .unreachable_unsafe,939 .unreachable_unsafe,
943 .unreachable_safe,940 .unreachable_safe,
944 .loop,941 .loop,
945 .container_field_named,
946 .container_field_typed,
947 .container_field,
948 .@"suspend",
949 .suspend_block,942 .suspend_block,
943 .suspend_block_one,
950 => true,944 => true,
951 };945 };
952 }946 }
...@@ -1017,6 +1011,17 @@ pub const Inst = struct {...@@ -1017,6 +1011,17 @@ pub const Inst = struct {
1017 return code.string_bytes[self.start..][0..self.len];1011 return code.string_bytes[self.start..][0..self.len];
1018 }1012 }
1019 },1013 },
1014 /// Strings 8 or fewer bytes which may not contain null bytes.
1015 small_str: struct {
1016 bytes: [8]u8,
1017
1018 pub fn get(self: @This()) []const u8 {
1019 const end = for (self.bytes) |byte, i| {
1020 if (byte == 0) break i;
1021 } else self.bytes.len;
1022 return self.bytes[0..end];
1023 }
1024 },
1020 str_tok: struct {1025 str_tok: struct {
1021 /// Offset into `string_bytes`. Null-terminated.1026 /// Offset into `string_bytes`. Null-terminated.
1022 start: u32,1027 start: u32,
...@@ -1205,7 +1210,8 @@ pub const Inst = struct {...@@ -1205,7 +1210,8 @@ pub const Inst = struct {
1205};1210};
12061211
1207/// For debugging purposes, like dumpFn but for unanalyzed zir blocks1212/// For debugging purposes, like dumpFn but for unanalyzed zir blocks
1208pub fn dumpZir(gpa: *Allocator, kind: []const u8, decl_name: [*:0]const u8, instructions: []*Inst) !void {1213pub fn dumpZir(gpa: *Allocator, kind: []const u8, decl_name: [*:0]const u8, code: Code) !void {
1214 if (true) @panic("TODO fix this function for zir-memory-layout branch");
1209 var fib = std.heap.FixedBufferAllocator.init(&[_]u8{});1215 var fib = std.heap.FixedBufferAllocator.init(&[_]u8{});
1210 var module = Module{1216 var module = Module{
1211 .decls = &[_]*Module.Decl{},1217 .decls = &[_]*Module.Decl{},