| ... | @@ -42,7 +42,7 @@ const InnerError = error{ OutOfMemory, AnalysisFail }; | ... | @@ -42,7 +42,7 @@ const InnerError = error{ OutOfMemory, AnalysisFail }; |
| 42 | | 42 | |
| 43 | fn addExtra(astgen: *AstGen, extra: anytype) Allocator.Error!u32 { | 43 | fn addExtra(astgen: *AstGen, extra: anytype) Allocator.Error!u32 { |
| 44 | const fields = std.meta.fields(@TypeOf(extra)); | 44 | const fields = std.meta.fields(@TypeOf(extra)); |
| 45 | try astgen.extra.ensureCapacity(astgen.gpa, astgen.extra.items.len + fields.len); | 45 | try astgen.extra.ensureUnusedCapacity(astgen.gpa, fields.len); |
| 46 | return addExtraAssumeCapacity(astgen, extra); | 46 | return addExtraAssumeCapacity(astgen, extra); |
| 47 | } | 47 | } |
| 48 | | 48 | |
| ... | @@ -259,6 +259,7 @@ pub const ResultLoc = union(enum) { | ... | @@ -259,6 +259,7 @@ pub const ResultLoc = union(enum) { |
| 259 | | 259 | |
| 260 | pub const align_rl: ResultLoc = .{ .ty = .u16_type }; | 260 | pub const align_rl: ResultLoc = .{ .ty = .u16_type }; |
| 261 | pub const bool_rl: ResultLoc = .{ .ty = .bool_type }; | 261 | pub const bool_rl: ResultLoc = .{ .ty = .bool_type }; |
| | 262 | pub const type_rl: ResultLoc = .{ .ty = .type_type }; |
| 262 | | 263 | |
| 263 | fn typeExpr(gz: *GenZir, scope: *Scope, type_node: ast.Node.Index) InnerError!Zir.Inst.Ref { | 264 | fn typeExpr(gz: *GenZir, scope: *Scope, type_node: ast.Node.Index) InnerError!Zir.Inst.Ref { |
| 264 | const prev_force_comptime = gz.force_comptime; | 265 | const prev_force_comptime = gz.force_comptime; |
| ... | @@ -1036,7 +1037,6 @@ fn fnProtoExpr( | ... | @@ -1036,7 +1037,6 @@ fn fnProtoExpr( |
| 1036 | fn_proto: ast.full.FnProto, | 1037 | fn_proto: ast.full.FnProto, |
| 1037 | ) InnerError!Zir.Inst.Ref { | 1038 | ) InnerError!Zir.Inst.Ref { |
| 1038 | const astgen = gz.astgen; | 1039 | const astgen = gz.astgen; |
| 1039 | const gpa = astgen.gpa; | | |
| 1040 | const tree = astgen.tree; | 1040 | const tree = astgen.tree; |
| 1041 | const token_tags = tree.tokens.items(.tag); | 1041 | const token_tags = tree.tokens.items(.tag); |
| 1042 | | 1042 | |
| ... | @@ -1046,71 +1046,53 @@ fn fnProtoExpr( | ... | @@ -1046,71 +1046,53 @@ fn fnProtoExpr( |
| 1046 | }; | 1046 | }; |
| 1047 | assert(!is_extern); | 1047 | assert(!is_extern); |
| 1048 | | 1048 | |
| 1049 | // The AST params array does not contain anytype and ... parameters. | 1049 | const is_var_args = is_var_args: { |
| 1050 | // We must iterate to count how many param types to allocate. | | |
| 1051 | const param_count = blk: { | | |
| 1052 | var count: usize = 0; | | |
| 1053 | var it = fn_proto.iterate(tree.*); | | |
| 1054 | while (it.next()) |param| { | | |
| 1055 | if (param.anytype_ellipsis3) |token| switch (token_tags[token]) { | | |
| 1056 | .ellipsis3 => break, | | |
| 1057 | .keyword_anytype => {}, | | |
| 1058 | else => unreachable, | | |
| 1059 | }; | | |
| 1060 | count += 1; | | |
| 1061 | } | | |
| 1062 | break :blk count; | | |
| 1063 | }; | | |
| 1064 | const param_types = try gpa.alloc(Zir.Inst.Ref, param_count); | | |
| 1065 | defer gpa.free(param_types); | | |
| 1066 | | | |
| 1067 | const bits_per_param = 1; | | |
| 1068 | const params_per_u32 = 32 / bits_per_param; | | |
| 1069 | // We only need this if there are greater than params_per_u32 fields. | | |
| 1070 | var bit_bag = ArrayListUnmanaged(u32){}; | | |
| 1071 | defer bit_bag.deinit(gpa); | | |
| 1072 | var cur_bit_bag: u32 = 0; | | |
| 1073 | var is_var_args = false; | | |
| 1074 | { | | |
| 1075 | var param_type_i: usize = 0; | 1050 | var param_type_i: usize = 0; |
| 1076 | var it = fn_proto.iterate(tree.*); | 1051 | var it = fn_proto.iterate(tree.*); |
| 1077 | while (it.next()) |param| : (param_type_i += 1) { | 1052 | while (it.next()) |param| : (param_type_i += 1) { |
| 1078 | if (param_type_i % params_per_u32 == 0 and param_type_i != 0) { | | |
| 1079 | try bit_bag.append(gpa, cur_bit_bag); | | |
| 1080 | cur_bit_bag = 0; | | |
| 1081 | } | | |
| 1082 | const is_comptime = if (param.comptime_noalias) |token| | 1053 | const is_comptime = if (param.comptime_noalias) |token| |
| 1083 | token_tags[token] == .keyword_comptime | 1054 | token_tags[token] == .keyword_comptime |
| 1084 | else | 1055 | else |
| 1085 | false; | 1056 | false; |
| 1086 | cur_bit_bag = (cur_bit_bag >> bits_per_param) | | | |
| 1087 | (@as(u32, @boolToInt(is_comptime)) << 31); | | |
| 1088 | | 1057 | |
| 1089 | if (param.anytype_ellipsis3) |token| { | 1058 | const is_anytype = if (param.anytype_ellipsis3) |token| blk: { |
| 1090 | switch (token_tags[token]) { | 1059 | switch (token_tags[token]) { |
| 1091 | .keyword_anytype => { | 1060 | .keyword_anytype => break :blk true, |
| 1092 | param_types[param_type_i] = .none; | 1061 | .ellipsis3 => break :is_var_args true, |
| 1093 | continue; | | |
| 1094 | }, | | |
| 1095 | .ellipsis3 => { | | |
| 1096 | is_var_args = true; | | |
| 1097 | break; | | |
| 1098 | }, | | |
| 1099 | else => unreachable, | 1062 | else => unreachable, |
| 1100 | } | 1063 | } |
| 1101 | } | 1064 | } else false; |
| 1102 | const param_type_node = param.type_expr; | 1065 | |
| 1103 | assert(param_type_node != 0); | 1066 | const param_name: u32 = if (param.name_token) |name_token| blk: { |
| 1104 | param_types[param_type_i] = | 1067 | if (mem.eql(u8, "_", tree.tokenSlice(name_token))) |
| 1105 | try expr(gz, scope, .{ .ty = .type_type }, param_type_node); | 1068 | break :blk 0; |
| 1106 | } | 1069 | |
| 1107 | assert(param_type_i == param_count); | 1070 | break :blk try astgen.identAsString(name_token); |
| | 1071 | } else 0; |
| 1108 | | 1072 | |
| 1109 | const empty_slot_count = params_per_u32 - (param_type_i % params_per_u32); | 1073 | if (is_anytype) { |
| 1110 | if (empty_slot_count < params_per_u32) { | 1074 | const name_token = param.name_token orelse param.anytype_ellipsis3.?; |
| 1111 | cur_bit_bag >>= @intCast(u5, empty_slot_count * bits_per_param); | 1075 | |
| | 1076 | const tag: Zir.Inst.Tag = if (is_comptime) |
| | 1077 | .param_anytype_comptime |
| | 1078 | else |
| | 1079 | .param_anytype; |
| | 1080 | _ = try gz.addStrTok(tag, param_name, name_token); |
| | 1081 | } else { |
| | 1082 | const param_type_node = param.type_expr; |
| | 1083 | assert(param_type_node != 0); |
| | 1084 | const param_type = try expr(gz, scope, type_rl, param_type_node); |
| | 1085 | const main_tokens = tree.nodes.items(.main_token); |
| | 1086 | const name_token = param.name_token orelse main_tokens[param_type_node]; |
| | 1087 | const tag: Zir.Inst.Tag = if (is_comptime) .param_comptime else .param; |
| | 1088 | _ = try gz.addPlTok(tag, name_token, Zir.Inst.Param{ |
| | 1089 | .name = param_name, |
| | 1090 | .ty = param_type, |
| | 1091 | }); |
| | 1092 | } |
| 1112 | } | 1093 | } |
| 1113 | } | 1094 | break :is_var_args false; |
| | 1095 | }; |
| 1114 | | 1096 | |
| 1115 | const align_inst: Zir.Inst.Ref = if (fn_proto.ast.align_expr == 0) .none else inst: { | 1097 | const align_inst: Zir.Inst.Ref = if (fn_proto.ast.align_expr == 0) .none else inst: { |
| 1116 | break :inst try expr(gz, scope, align_rl, fn_proto.ast.align_expr); | 1098 | break :inst try expr(gz, scope, align_rl, fn_proto.ast.align_expr); |
| ... | @@ -1144,7 +1126,6 @@ fn fnProtoExpr( | ... | @@ -1144,7 +1126,6 @@ fn fnProtoExpr( |
| 1144 | const result = try gz.addFunc(.{ | 1126 | const result = try gz.addFunc(.{ |
| 1145 | .src_node = fn_proto.ast.proto_node, | 1127 | .src_node = fn_proto.ast.proto_node, |
| 1146 | .ret_ty = return_type_inst, | 1128 | .ret_ty = return_type_inst, |
| 1147 | .param_types = param_types, | | |
| 1148 | .body = &[0]Zir.Inst.Index{}, | 1129 | .body = &[0]Zir.Inst.Index{}, |
| 1149 | .cc = cc, | 1130 | .cc = cc, |
| 1150 | .align_inst = align_inst, | 1131 | .align_inst = align_inst, |
| ... | @@ -1153,8 +1134,6 @@ fn fnProtoExpr( | ... | @@ -1153,8 +1134,6 @@ fn fnProtoExpr( |
| 1153 | .is_inferred_error = false, | 1134 | .is_inferred_error = false, |
| 1154 | .is_test = false, | 1135 | .is_test = false, |
| 1155 | .is_extern = false, | 1136 | .is_extern = false, |
| 1156 | .cur_bit_bag = cur_bit_bag, | | |
| 1157 | .bit_bag = bit_bag.items, | | |
| 1158 | }); | 1137 | }); |
| 1159 | return rvalue(gz, rl, result, fn_proto.ast.proto_node); | 1138 | return rvalue(gz, rl, result, fn_proto.ast.proto_node); |
| 1160 | } | 1139 | } |
| ... | @@ -1447,8 +1426,8 @@ fn structInitExprRlNone( | ... | @@ -1447,8 +1426,8 @@ fn structInitExprRlNone( |
| 1447 | const init_inst = try gz.addPlNode(tag, node, Zir.Inst.StructInitAnon{ | 1426 | const init_inst = try gz.addPlNode(tag, node, Zir.Inst.StructInitAnon{ |
| 1448 | .fields_len = @intCast(u32, fields_list.len), | 1427 | .fields_len = @intCast(u32, fields_list.len), |
| 1449 | }); | 1428 | }); |
| 1450 | try astgen.extra.ensureCapacity(gpa, astgen.extra.items.len + | 1429 | try astgen.extra.ensureUnusedCapacity(gpa, fields_list.len * |
| 1451 | fields_list.len * @typeInfo(Zir.Inst.StructInitAnon.Item).Struct.fields.len); | 1430 | @typeInfo(Zir.Inst.StructInitAnon.Item).Struct.fields.len); |
| 1452 | for (fields_list) |field| { | 1431 | for (fields_list) |field| { |
| 1453 | _ = gz.astgen.addExtraAssumeCapacity(field); | 1432 | _ = gz.astgen.addExtraAssumeCapacity(field); |
| 1454 | } | 1433 | } |
| ... | @@ -1520,8 +1499,8 @@ fn structInitExprRlTy( | ... | @@ -1520,8 +1499,8 @@ fn structInitExprRlTy( |
| 1520 | const init_inst = try gz.addPlNode(tag, node, Zir.Inst.StructInit{ | 1499 | const init_inst = try gz.addPlNode(tag, node, Zir.Inst.StructInit{ |
| 1521 | .fields_len = @intCast(u32, fields_list.len), | 1500 | .fields_len = @intCast(u32, fields_list.len), |
| 1522 | }); | 1501 | }); |
| 1523 | try astgen.extra.ensureCapacity(gpa, astgen.extra.items.len + | 1502 | try astgen.extra.ensureUnusedCapacity(gpa, fields_list.len * |
| 1524 | fields_list.len * @typeInfo(Zir.Inst.StructInit.Item).Struct.fields.len); | 1503 | @typeInfo(Zir.Inst.StructInit.Item).Struct.fields.len); |
| 1525 | for (fields_list) |field| { | 1504 | for (fields_list) |field| { |
| 1526 | _ = gz.astgen.addExtraAssumeCapacity(field); | 1505 | _ = gz.astgen.addExtraAssumeCapacity(field); |
| 1527 | } | 1506 | } |
| ... | @@ -1918,7 +1897,10 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: ast.Node.Index) Inner | ... | @@ -1918,7 +1897,10 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: ast.Node.Index) Inner |
| 1918 | // ZIR instructions that might be a type other than `noreturn` or `void`. | 1897 | // ZIR instructions that might be a type other than `noreturn` or `void`. |
| 1919 | .add, | 1898 | .add, |
| 1920 | .addwrap, | 1899 | .addwrap, |
| 1921 | .arg, | 1900 | .param, |
| | 1901 | .param_comptime, |
| | 1902 | .param_anytype, |
| | 1903 | .param_anytype_comptime, |
| 1922 | .alloc, | 1904 | .alloc, |
| 1923 | .alloc_mut, | 1905 | .alloc_mut, |
| 1924 | .alloc_comptime, | 1906 | .alloc_comptime, |
| ... | @@ -2488,7 +2470,7 @@ fn varDecl( | ... | @@ -2488,7 +2470,7 @@ fn varDecl( |
| 2488 | // Move the init_scope instructions into the parent scope, swapping | 2470 | // Move the init_scope instructions into the parent scope, swapping |
| 2489 | // store_to_block_ptr for store_to_inferred_ptr. | 2471 | // store_to_block_ptr for store_to_inferred_ptr. |
| 2490 | const expected_len = parent_zir.items.len + init_scope.instructions.items.len; | 2472 | const expected_len = parent_zir.items.len + init_scope.instructions.items.len; |
| 2491 | try parent_zir.ensureCapacity(gpa, expected_len); | 2473 | try parent_zir.ensureTotalCapacity(gpa, expected_len); |
| 2492 | for (init_scope.instructions.items) |src_inst| { | 2474 | for (init_scope.instructions.items) |src_inst| { |
| 2493 | if (zir_tags[src_inst] == .store_to_block_ptr) { | 2475 | if (zir_tags[src_inst] == .store_to_block_ptr) { |
| 2494 | if (zir_datas[src_inst].bin.lhs == init_scope.rl_ptr) { | 2476 | if (zir_datas[src_inst].bin.lhs == init_scope.rl_ptr) { |
| ... | @@ -2750,10 +2732,10 @@ fn ptrType( | ... | @@ -2750,10 +2732,10 @@ fn ptrType( |
| 2750 | } | 2732 | } |
| 2751 | | 2733 | |
| 2752 | const gpa = gz.astgen.gpa; | 2734 | const gpa = gz.astgen.gpa; |
| 2753 | try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1); | 2735 | try gz.instructions.ensureUnusedCapacity(gpa, 1); |
| 2754 | try gz.astgen.instructions.ensureCapacity(gpa, gz.astgen.instructions.len + 1); | 2736 | try gz.astgen.instructions.ensureUnusedCapacity(gpa, 1); |
| 2755 | try gz.astgen.extra.ensureCapacity(gpa, gz.astgen.extra.items.len + | 2737 | try gz.astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.PtrType).Struct.fields.len + |
| 2756 | @typeInfo(Zir.Inst.PtrType).Struct.fields.len + trailing_count); | 2738 | trailing_count); |
| 2757 | | 2739 | |
| 2758 | const payload_index = gz.astgen.addExtraAssumeCapacity(Zir.Inst.PtrType{ .elem_type = elem_type }); | 2740 | const payload_index = gz.astgen.addExtraAssumeCapacity(Zir.Inst.PtrType{ .elem_type = elem_type }); |
| 2759 | if (sentinel_ref != .none) { | 2741 | if (sentinel_ref != .none) { |
| ... | @@ -2899,6 +2881,16 @@ fn fnDecl( | ... | @@ -2899,6 +2881,16 @@ fn fnDecl( |
| 2899 | }; | 2881 | }; |
| 2900 | defer decl_gz.instructions.deinit(gpa); | 2882 | defer decl_gz.instructions.deinit(gpa); |
| 2901 | | 2883 | |
| | 2884 | var fn_gz: GenZir = .{ |
| | 2885 | .force_comptime = false, |
| | 2886 | .in_defer = false, |
| | 2887 | .decl_node_index = fn_proto.ast.proto_node, |
| | 2888 | .decl_line = decl_gz.decl_line, |
| | 2889 | .parent = &decl_gz.base, |
| | 2890 | .astgen = astgen, |
| | 2891 | }; |
| | 2892 | defer fn_gz.instructions.deinit(gpa); |
| | 2893 | |
| 2902 | // TODO: support noinline | 2894 | // TODO: support noinline |
| 2903 | const is_pub = fn_proto.visib_token != null; | 2895 | const is_pub = fn_proto.visib_token != null; |
| 2904 | const is_export = blk: { | 2896 | const is_export = blk: { |
| ... | @@ -2922,71 +2914,76 @@ fn fnDecl( | ... | @@ -2922,71 +2914,76 @@ fn fnDecl( |
| 2922 | | 2914 | |
| 2923 | try wip_decls.next(gpa, is_pub, is_export, align_inst != .none, section_inst != .none); | 2915 | try wip_decls.next(gpa, is_pub, is_export, align_inst != .none, section_inst != .none); |
| 2924 | | 2916 | |
| 2925 | // The AST params array does not contain anytype and ... parameters. | 2917 | var params_scope = &fn_gz.base; |
| 2926 | // We must iterate to count how many param types to allocate. | 2918 | const is_var_args = is_var_args: { |
| 2927 | const param_count = blk: { | | |
| 2928 | var count: usize = 0; | | |
| 2929 | var it = fn_proto.iterate(tree.*); | | |
| 2930 | while (it.next()) |param| { | | |
| 2931 | if (param.anytype_ellipsis3) |token| switch (token_tags[token]) { | | |
| 2932 | .ellipsis3 => break, | | |
| 2933 | .keyword_anytype => {}, | | |
| 2934 | else => unreachable, | | |
| 2935 | }; | | |
| 2936 | count += 1; | | |
| 2937 | } | | |
| 2938 | break :blk count; | | |
| 2939 | }; | | |
| 2940 | const param_types = try gpa.alloc(Zir.Inst.Ref, param_count); | | |
| 2941 | defer gpa.free(param_types); | | |
| 2942 | | | |
| 2943 | const bits_per_param = 1; | | |
| 2944 | const params_per_u32 = 32 / bits_per_param; | | |
| 2945 | // We only need this if there are greater than params_per_u32 fields. | | |
| 2946 | var bit_bag = ArrayListUnmanaged(u32){}; | | |
| 2947 | defer bit_bag.deinit(gpa); | | |
| 2948 | var cur_bit_bag: u32 = 0; | | |
| 2949 | var is_var_args = false; | | |
| 2950 | { | | |
| 2951 | var param_type_i: usize = 0; | 2919 | var param_type_i: usize = 0; |
| 2952 | var it = fn_proto.iterate(tree.*); | 2920 | var it = fn_proto.iterate(tree.*); |
| 2953 | while (it.next()) |param| : (param_type_i += 1) { | 2921 | while (it.next()) |param| : (param_type_i += 1) { |
| 2954 | if (param_type_i % params_per_u32 == 0 and param_type_i != 0) { | | |
| 2955 | try bit_bag.append(gpa, cur_bit_bag); | | |
| 2956 | cur_bit_bag = 0; | | |
| 2957 | } | | |
| 2958 | const is_comptime = if (param.comptime_noalias) |token| | 2922 | const is_comptime = if (param.comptime_noalias) |token| |
| 2959 | token_tags[token] == .keyword_comptime | 2923 | token_tags[token] == .keyword_comptime |
| 2960 | else | 2924 | else |
| 2961 | false; | 2925 | false; |
| 2962 | cur_bit_bag = (cur_bit_bag >> bits_per_param) | | | |
| 2963 | (@as(u32, @boolToInt(is_comptime)) << 31); | | |
| 2964 | | 2926 | |
| 2965 | if (param.anytype_ellipsis3) |token| { | 2927 | const is_anytype = if (param.anytype_ellipsis3) |token| blk: { |
| 2966 | switch (token_tags[token]) { | 2928 | switch (token_tags[token]) { |
| 2967 | .keyword_anytype => { | 2929 | .keyword_anytype => break :blk true, |
| 2968 | param_types[param_type_i] = .none; | 2930 | .ellipsis3 => break :is_var_args true, |
| 2969 | continue; | | |
| 2970 | }, | | |
| 2971 | .ellipsis3 => { | | |
| 2972 | is_var_args = true; | | |
| 2973 | break; | | |
| 2974 | }, | | |
| 2975 | else => unreachable, | 2931 | else => unreachable, |
| 2976 | } | 2932 | } |
| 2977 | } | 2933 | } else false; |
| 2978 | const param_type_node = param.type_expr; | 2934 | |
| 2979 | assert(param_type_node != 0); | 2935 | const param_name: u32 = if (param.name_token) |name_token| blk: { |
| 2980 | param_types[param_type_i] = | 2936 | if (mem.eql(u8, "_", tree.tokenSlice(name_token))) |
| 2981 | try expr(&decl_gz, &decl_gz.base, .{ .ty = .type_type }, param_type_node); | 2937 | break :blk 0; |
| 2982 | } | 2938 | |
| 2983 | assert(param_type_i == param_count); | 2939 | const param_name = try astgen.identAsString(name_token); |
| | 2940 | if (!is_extern) { |
| | 2941 | try astgen.detectLocalShadowing(params_scope, param_name, name_token); |
| | 2942 | } |
| | 2943 | break :blk param_name; |
| | 2944 | } else if (!is_extern) { |
| | 2945 | if (param.anytype_ellipsis3) |tok| { |
| | 2946 | return astgen.failTok(tok, "missing parameter name", .{}); |
| | 2947 | } else { |
| | 2948 | return astgen.failNode(param.type_expr, "missing parameter name", .{}); |
| | 2949 | } |
| | 2950 | } else 0; |
| | 2951 | |
| | 2952 | const param_inst = if (is_anytype) param: { |
| | 2953 | const name_token = param.name_token orelse param.anytype_ellipsis3.?; |
| | 2954 | const tag: Zir.Inst.Tag = if (is_comptime) |
| | 2955 | .param_anytype_comptime |
| | 2956 | else |
| | 2957 | .param_anytype; |
| | 2958 | break :param try decl_gz.addStrTok(tag, param_name, name_token); |
| | 2959 | } else param: { |
| | 2960 | const param_type_node = param.type_expr; |
| | 2961 | assert(param_type_node != 0); |
| | 2962 | const param_type = try expr(&decl_gz, params_scope, type_rl, param_type_node); |
| | 2963 | const main_tokens = tree.nodes.items(.main_token); |
| | 2964 | const name_token = param.name_token orelse main_tokens[param_type_node]; |
| | 2965 | const tag: Zir.Inst.Tag = if (is_comptime) .param_comptime else .param; |
| | 2966 | break :param try decl_gz.addPlTok(tag, name_token, Zir.Inst.Param{ |
| | 2967 | .name = param_name, |
| | 2968 | .ty = param_type, |
| | 2969 | }); |
| | 2970 | }; |
| | 2971 | |
| | 2972 | if (param_name == 0) continue; |
| 2984 | | 2973 | |
| 2985 | const empty_slot_count = params_per_u32 - (param_type_i % params_per_u32); | 2974 | const sub_scope = try astgen.arena.create(Scope.LocalVal); |
| 2986 | if (empty_slot_count < params_per_u32) { | 2975 | sub_scope.* = .{ |
| 2987 | cur_bit_bag >>= @intCast(u5, empty_slot_count * bits_per_param); | 2976 | .parent = params_scope, |
| | 2977 | .gen_zir = &decl_gz, |
| | 2978 | .name = param_name, |
| | 2979 | .inst = param_inst, |
| | 2980 | .token_src = param.name_token.?, |
| | 2981 | .id_cat = .@"function parameter", |
| | 2982 | }; |
| | 2983 | params_scope = &sub_scope.base; |
| 2988 | } | 2984 | } |
| 2989 | } | 2985 | break :is_var_args false; |
| | 2986 | }; |
| 2990 | | 2987 | |
| 2991 | const lib_name: u32 = if (fn_proto.lib_name) |lib_name_token| blk: { | 2988 | const lib_name: u32 = if (fn_proto.lib_name) |lib_name_token| blk: { |
| 2992 | const lib_name_str = try astgen.strLitAsString(lib_name_token); | 2989 | const lib_name_str = try astgen.strLitAsString(lib_name_token); |
| ... | @@ -2998,7 +2995,7 @@ fn fnDecl( | ... | @@ -2998,7 +2995,7 @@ fn fnDecl( |
| 2998 | | 2995 | |
| 2999 | const return_type_inst = try AstGen.expr( | 2996 | const return_type_inst = try AstGen.expr( |
| 3000 | &decl_gz, | 2997 | &decl_gz, |
| 3001 | &decl_gz.base, | 2998 | params_scope, |
| 3002 | .{ .ty = .type_type }, | 2999 | .{ .ty = .type_type }, |
| 3003 | fn_proto.ast.return_type, | 3000 | fn_proto.ast.return_type, |
| 3004 | ); | 3001 | ); |
| ... | @@ -3014,7 +3011,7 @@ fn fnDecl( | ... | @@ -3014,7 +3011,7 @@ fn fnDecl( |
| 3014 | } | 3011 | } |
| 3015 | break :blk try AstGen.expr( | 3012 | break :blk try AstGen.expr( |
| 3016 | &decl_gz, | 3013 | &decl_gz, |
| 3017 | &decl_gz.base, | 3014 | params_scope, |
| 3018 | .{ .ty = .calling_convention_type }, | 3015 | .{ .ty = .calling_convention_type }, |
| 3019 | fn_proto.ast.callconv_expr, | 3016 | fn_proto.ast.callconv_expr, |
| 3020 | ); | 3017 | ); |
| ... | @@ -3038,7 +3035,6 @@ fn fnDecl( | ... | @@ -3038,7 +3035,6 @@ fn fnDecl( |
| 3038 | break :func try decl_gz.addFunc(.{ | 3035 | break :func try decl_gz.addFunc(.{ |
| 3039 | .src_node = decl_node, | 3036 | .src_node = decl_node, |
| 3040 | .ret_ty = return_type_inst, | 3037 | .ret_ty = return_type_inst, |
| 3041 | .param_types = param_types, | | |
| 3042 | .body = &[0]Zir.Inst.Index{}, | 3038 | .body = &[0]Zir.Inst.Index{}, |
| 3043 | .cc = cc, | 3039 | .cc = cc, |
| 3044 | .align_inst = .none, // passed in the per-decl data | 3040 | .align_inst = .none, // passed in the per-decl data |
| ... | @@ -3047,75 +3043,18 @@ fn fnDecl( | ... | @@ -3047,75 +3043,18 @@ fn fnDecl( |
| 3047 | .is_inferred_error = false, | 3043 | .is_inferred_error = false, |
| 3048 | .is_test = false, | 3044 | .is_test = false, |
| 3049 | .is_extern = true, | 3045 | .is_extern = true, |
| 3050 | .cur_bit_bag = cur_bit_bag, | | |
| 3051 | .bit_bag = bit_bag.items, | | |
| 3052 | }); | 3046 | }); |
| 3053 | } else func: { | 3047 | } else func: { |
| 3054 | if (is_var_args) { | 3048 | if (is_var_args) { |
| 3055 | return astgen.failTok(fn_proto.ast.fn_token, "non-extern function is variadic", .{}); | 3049 | return astgen.failTok(fn_proto.ast.fn_token, "non-extern function is variadic", .{}); |
| 3056 | } | 3050 | } |
| 3057 | | 3051 | |
| 3058 | var fn_gz: GenZir = .{ | | |
| 3059 | .force_comptime = false, | | |
| 3060 | .in_defer = false, | | |
| 3061 | .decl_node_index = fn_proto.ast.proto_node, | | |
| 3062 | .decl_line = decl_gz.decl_line, | | |
| 3063 | .parent = &decl_gz.base, | | |
| 3064 | .astgen = astgen, | | |
| 3065 | }; | | |
| 3066 | defer fn_gz.instructions.deinit(gpa); | | |
| 3067 | | | |
| 3068 | const prev_fn_block = astgen.fn_block; | 3052 | const prev_fn_block = astgen.fn_block; |
| 3069 | astgen.fn_block = &fn_gz; | 3053 | astgen.fn_block = &fn_gz; |
| 3070 | defer astgen.fn_block = prev_fn_block; | 3054 | defer astgen.fn_block = prev_fn_block; |
| 3071 | | 3055 | |
| 3072 | // Iterate over the parameters. We put the param names as the first N | 3056 | _ = try expr(&fn_gz, params_scope, .none, body_node); |
| 3073 | // items inside `extra` so that debug info later can refer to the parameter names | 3057 | try checkUsed(gz, &fn_gz.base, params_scope); |
| 3074 | // even while the respective source code is unloaded. | | |
| 3075 | try astgen.extra.ensureUnusedCapacity(gpa, param_count); | | |
| 3076 | | | |
| 3077 | { | | |
| 3078 | var params_scope = &fn_gz.base; | | |
| 3079 | var i: usize = 0; | | |
| 3080 | var it = fn_proto.iterate(tree.*); | | |
| 3081 | while (it.next()) |param| : (i += 1) { | | |
| 3082 | const name_token = param.name_token orelse { | | |
| 3083 | if (param.anytype_ellipsis3) |tok| { | | |
| 3084 | return astgen.failTok(tok, "missing parameter name", .{}); | | |
| 3085 | } else { | | |
| 3086 | return astgen.failNode(param.type_expr, "missing parameter name", .{}); | | |
| 3087 | } | | |
| 3088 | }; | | |
| 3089 | if (param.type_expr != 0) | | |
| 3090 | _ = try typeExpr(&fn_gz, params_scope, param.type_expr); | | |
| 3091 | if (mem.eql(u8, "_", tree.tokenSlice(name_token))) | | |
| 3092 | continue; | | |
| 3093 | const param_name = try astgen.identAsString(name_token); | | |
| 3094 | // Create an arg instruction. This is needed to emit a semantic analysis | | |
| 3095 | // error for shadowing decls. | | |
| 3096 | try astgen.detectLocalShadowing(params_scope, param_name, name_token); | | |
| 3097 | const arg_inst = try fn_gz.addStrTok(.arg, param_name, name_token); | | |
| 3098 | const sub_scope = try astgen.arena.create(Scope.LocalVal); | | |
| 3099 | sub_scope.* = .{ | | |
| 3100 | .parent = params_scope, | | |
| 3101 | .gen_zir = &fn_gz, | | |
| 3102 | .name = param_name, | | |
| 3103 | .inst = arg_inst, | | |
| 3104 | .token_src = name_token, | | |
| 3105 | .id_cat = .@"function parameter", | | |
| 3106 | }; | | |
| 3107 | params_scope = &sub_scope.base; | | |
| 3108 | | | |
| 3109 | // Additionally put the param name into `string_bytes` and reference it with | | |
| 3110 | // `extra` so that we have access to the data in codegen, for debug info. | | |
| 3111 | const str_index = try astgen.identAsString(name_token); | | |
| 3112 | try astgen.extra.append(astgen.gpa, str_index); | | |
| 3113 | } | | |
| 3114 | _ = try typeExpr(&fn_gz, params_scope, fn_proto.ast.return_type); | | |
| 3115 | | | |
| 3116 | _ = try expr(&fn_gz, params_scope, .none, body_node); | | |
| 3117 | try checkUsed(gz, &fn_gz.base, params_scope); | | |
| 3118 | } | | |
| 3119 | | 3058 | |
| 3120 | const need_implicit_ret = blk: { | 3059 | const need_implicit_ret = blk: { |
| 3121 | if (fn_gz.instructions.items.len == 0) | 3060 | if (fn_gz.instructions.items.len == 0) |
| ... | @@ -3133,7 +3072,6 @@ fn fnDecl( | ... | @@ -3133,7 +3072,6 @@ fn fnDecl( |
| 3133 | break :func try decl_gz.addFunc(.{ | 3072 | break :func try decl_gz.addFunc(.{ |
| 3134 | .src_node = decl_node, | 3073 | .src_node = decl_node, |
| 3135 | .ret_ty = return_type_inst, | 3074 | .ret_ty = return_type_inst, |
| 3136 | .param_types = param_types, | | |
| 3137 | .body = fn_gz.instructions.items, | 3075 | .body = fn_gz.instructions.items, |
| 3138 | .cc = cc, | 3076 | .cc = cc, |
| 3139 | .align_inst = .none, // passed in the per-decl data | 3077 | .align_inst = .none, // passed in the per-decl data |
| ... | @@ -3142,8 +3080,6 @@ fn fnDecl( | ... | @@ -3142,8 +3080,6 @@ fn fnDecl( |
| 3142 | .is_inferred_error = is_inferred_error, | 3080 | .is_inferred_error = is_inferred_error, |
| 3143 | .is_test = false, | 3081 | .is_test = false, |
| 3144 | .is_extern = false, | 3082 | .is_extern = false, |
| 3145 | .cur_bit_bag = cur_bit_bag, | | |
| 3146 | .bit_bag = bit_bag.items, | | |
| 3147 | }); | 3083 | }); |
| 3148 | }; | 3084 | }; |
| 3149 | | 3085 | |
| ... | @@ -3480,7 +3416,6 @@ fn testDecl( | ... | @@ -3480,7 +3416,6 @@ fn testDecl( |
| 3480 | const func_inst = try decl_block.addFunc(.{ | 3416 | const func_inst = try decl_block.addFunc(.{ |
| 3481 | .src_node = node, | 3417 | .src_node = node, |
| 3482 | .ret_ty = .void_type, | 3418 | .ret_ty = .void_type, |
| 3483 | .param_types = &[0]Zir.Inst.Ref{}, | | |
| 3484 | .body = fn_block.instructions.items, | 3419 | .body = fn_block.instructions.items, |
| 3485 | .cc = .none, | 3420 | .cc = .none, |
| 3486 | .align_inst = .none, | 3421 | .align_inst = .none, |
| ... | @@ -3489,8 +3424,6 @@ fn testDecl( | ... | @@ -3489,8 +3424,6 @@ fn testDecl( |
| 3489 | .is_inferred_error = true, | 3424 | .is_inferred_error = true, |
| 3490 | .is_test = true, | 3425 | .is_test = true, |
| 3491 | .is_extern = false, | 3426 | .is_extern = false, |
| 3492 | .cur_bit_bag = 0, | | |
| 3493 | .bit_bag = &.{}, | | |
| 3494 | }); | 3427 | }); |
| 3495 | | 3428 | |
| 3496 | _ = try decl_block.addBreak(.break_inline, block_inst, func_inst); | 3429 | _ = try decl_block.addBreak(.break_inline, block_inst, func_inst); |
| ... | @@ -4238,7 +4171,7 @@ fn containerDecl( | ... | @@ -4238,7 +4171,7 @@ fn containerDecl( |
| 4238 | var fields_data = ArrayListUnmanaged(u32){}; | 4171 | var fields_data = ArrayListUnmanaged(u32){}; |
| 4239 | defer fields_data.deinit(gpa); | 4172 | defer fields_data.deinit(gpa); |
| 4240 | | 4173 | |
| 4241 | try fields_data.ensureCapacity(gpa, counts.total_fields + counts.values); | 4174 | try fields_data.ensureTotalCapacity(gpa, counts.total_fields + counts.values); |
| 4242 | | 4175 | |
| 4243 | // We only need this if there are greater than 32 fields. | 4176 | // We only need this if there are greater than 32 fields. |
| 4244 | var bit_bag = ArrayListUnmanaged(u32){}; | 4177 | var bit_bag = ArrayListUnmanaged(u32){}; |
| ... | @@ -5184,8 +5117,7 @@ fn setCondBrPayload( | ... | @@ -5184,8 +5117,7 @@ fn setCondBrPayload( |
| 5184 | ) !void { | 5117 | ) !void { |
| 5185 | const astgen = then_scope.astgen; | 5118 | const astgen = then_scope.astgen; |
| 5186 | | 5119 | |
| 5187 | try astgen.extra.ensureCapacity(astgen.gpa, astgen.extra.items.len + | 5120 | try astgen.extra.ensureUnusedCapacity(astgen.gpa, @typeInfo(Zir.Inst.CondBr).Struct.fields.len + |
| 5188 | @typeInfo(Zir.Inst.CondBr).Struct.fields.len + | | |
| 5189 | then_scope.instructions.items.len + else_scope.instructions.items.len); | 5121 | then_scope.instructions.items.len + else_scope.instructions.items.len); |
| 5190 | | 5122 | |
| 5191 | const zir_datas = astgen.instructions.items(.data); | 5123 | const zir_datas = astgen.instructions.items(.data); |
| ... | @@ -5839,10 +5771,9 @@ fn switchExpr( | ... | @@ -5839,10 +5771,9 @@ fn switchExpr( |
| 5839 | _ = try case_scope.addBreak(.@"break", switch_block, case_result); | 5771 | _ = try case_scope.addBreak(.@"break", switch_block, case_result); |
| 5840 | } | 5772 | } |
| 5841 | // Documentation for this: `Zir.Inst.SwitchBlock` and `Zir.Inst.SwitchBlockMulti`. | 5773 | // Documentation for this: `Zir.Inst.SwitchBlock` and `Zir.Inst.SwitchBlockMulti`. |
| 5842 | try scalar_cases_payload.ensureCapacity(gpa, scalar_cases_payload.items.len + | 5774 | try scalar_cases_payload.ensureUnusedCapacity(gpa, case_scope.instructions.items.len + |
| 5843 | 3 + // operand, scalar_cases_len, else body len | 5775 | 3 + // operand, scalar_cases_len, else body len |
| 5844 | @boolToInt(multi_cases_len != 0) + | 5776 | @boolToInt(multi_cases_len != 0)); |
| 5845 | case_scope.instructions.items.len); | | |
| 5846 | scalar_cases_payload.appendAssumeCapacity(@enumToInt(operand)); | 5777 | scalar_cases_payload.appendAssumeCapacity(@enumToInt(operand)); |
| 5847 | scalar_cases_payload.appendAssumeCapacity(scalar_cases_len); | 5778 | scalar_cases_payload.appendAssumeCapacity(scalar_cases_len); |
| 5848 | if (multi_cases_len != 0) { | 5779 | if (multi_cases_len != 0) { |
| ... | @@ -5852,9 +5783,11 @@ fn switchExpr( | ... | @@ -5852,9 +5783,11 @@ fn switchExpr( |
| 5852 | scalar_cases_payload.appendSliceAssumeCapacity(case_scope.instructions.items); | 5783 | scalar_cases_payload.appendSliceAssumeCapacity(case_scope.instructions.items); |
| 5853 | } else { | 5784 | } else { |
| 5854 | // Documentation for this: `Zir.Inst.SwitchBlock` and `Zir.Inst.SwitchBlockMulti`. | 5785 | // Documentation for this: `Zir.Inst.SwitchBlock` and `Zir.Inst.SwitchBlockMulti`. |
| 5855 | try scalar_cases_payload.ensureCapacity(gpa, scalar_cases_payload.items.len + | 5786 | try scalar_cases_payload.ensureUnusedCapacity( |
| 5856 | 2 + // operand, scalar_cases_len | 5787 | gpa, |
| 5857 | @boolToInt(multi_cases_len != 0)); | 5788 | @as(usize, 2) + // operand, scalar_cases_len |
| | 5789 | @boolToInt(multi_cases_len != 0), |
| | 5790 | ); |
| 5858 | scalar_cases_payload.appendAssumeCapacity(@enumToInt(operand)); | 5791 | scalar_cases_payload.appendAssumeCapacity(@enumToInt(operand)); |
| 5859 | scalar_cases_payload.appendAssumeCapacity(scalar_cases_len); | 5792 | scalar_cases_payload.appendAssumeCapacity(scalar_cases_len); |
| 5860 | if (multi_cases_len != 0) { | 5793 | if (multi_cases_len != 0) { |
| ... | @@ -5975,8 +5908,8 @@ fn switchExpr( | ... | @@ -5975,8 +5908,8 @@ fn switchExpr( |
| 5975 | block_scope.break_count += 1; | 5908 | block_scope.break_count += 1; |
| 5976 | _ = try case_scope.addBreak(.@"break", switch_block, case_result); | 5909 | _ = try case_scope.addBreak(.@"break", switch_block, case_result); |
| 5977 | } | 5910 | } |
| 5978 | try scalar_cases_payload.ensureCapacity(gpa, scalar_cases_payload.items.len + | 5911 | try scalar_cases_payload.ensureUnusedCapacity(gpa, 2 + |
| 5979 | 2 + case_scope.instructions.items.len); | 5912 | case_scope.instructions.items.len); |
| 5980 | scalar_cases_payload.appendAssumeCapacity(@enumToInt(item_inst)); | 5913 | scalar_cases_payload.appendAssumeCapacity(@enumToInt(item_inst)); |
| 5981 | scalar_cases_payload.appendAssumeCapacity(@intCast(u32, case_scope.instructions.items.len)); | 5914 | scalar_cases_payload.appendAssumeCapacity(@intCast(u32, case_scope.instructions.items.len)); |
| 5982 | scalar_cases_payload.appendSliceAssumeCapacity(case_scope.instructions.items); | 5915 | scalar_cases_payload.appendSliceAssumeCapacity(case_scope.instructions.items); |
| ... | @@ -6012,8 +5945,8 @@ fn switchExpr( | ... | @@ -6012,8 +5945,8 @@ fn switchExpr( |
| 6012 | const payload_index = astgen.extra.items.len; | 5945 | const payload_index = astgen.extra.items.len; |
| 6013 | const zir_datas = astgen.instructions.items(.data); | 5946 | const zir_datas = astgen.instructions.items(.data); |
| 6014 | zir_datas[switch_block].pl_node.payload_index = @intCast(u32, payload_index); | 5947 | zir_datas[switch_block].pl_node.payload_index = @intCast(u32, payload_index); |
| 6015 | try astgen.extra.ensureCapacity(gpa, astgen.extra.items.len + | 5948 | try astgen.extra.ensureUnusedCapacity(gpa, scalar_cases_payload.items.len + |
| 6016 | scalar_cases_payload.items.len + multi_cases_payload.items.len); | 5949 | multi_cases_payload.items.len); |
| 6017 | const strat = rl.strategy(&block_scope); | 5950 | const strat = rl.strategy(&block_scope); |
| 6018 | switch (strat.tag) { | 5951 | switch (strat.tag) { |
| 6019 | .break_operand => { | 5952 | .break_operand => { |
| ... | @@ -8659,7 +8592,7 @@ fn failNodeNotes( | ... | @@ -8659,7 +8592,7 @@ fn failNodeNotes( |
| 8659 | } | 8592 | } |
| 8660 | const notes_index: u32 = if (notes.len != 0) blk: { | 8593 | const notes_index: u32 = if (notes.len != 0) blk: { |
| 8661 | const notes_start = astgen.extra.items.len; | 8594 | const notes_start = astgen.extra.items.len; |
| 8662 | try astgen.extra.ensureCapacity(astgen.gpa, notes_start + 1 + notes.len); | 8595 | try astgen.extra.ensureTotalCapacity(astgen.gpa, notes_start + 1 + notes.len); |
| 8663 | astgen.extra.appendAssumeCapacity(@intCast(u32, notes.len)); | 8596 | astgen.extra.appendAssumeCapacity(@intCast(u32, notes.len)); |
| 8664 | astgen.extra.appendSliceAssumeCapacity(notes); | 8597 | astgen.extra.appendSliceAssumeCapacity(notes); |
| 8665 | break :blk @intCast(u32, notes_start); | 8598 | break :blk @intCast(u32, notes_start); |
| ... | @@ -8700,7 +8633,7 @@ fn failTokNotes( | ... | @@ -8700,7 +8633,7 @@ fn failTokNotes( |
| 8700 | } | 8633 | } |
| 8701 | const notes_index: u32 = if (notes.len != 0) blk: { | 8634 | const notes_index: u32 = if (notes.len != 0) blk: { |
| 8702 | const notes_start = astgen.extra.items.len; | 8635 | const notes_start = astgen.extra.items.len; |
| 8703 | try astgen.extra.ensureCapacity(astgen.gpa, notes_start + 1 + notes.len); | 8636 | try astgen.extra.ensureTotalCapacity(astgen.gpa, notes_start + 1 + notes.len); |
| 8704 | astgen.extra.appendAssumeCapacity(@intCast(u32, notes.len)); | 8637 | astgen.extra.appendAssumeCapacity(@intCast(u32, notes.len)); |
| 8705 | astgen.extra.appendSliceAssumeCapacity(notes); | 8638 | astgen.extra.appendSliceAssumeCapacity(notes); |
| 8706 | break :blk @intCast(u32, notes_start); | 8639 | break :blk @intCast(u32, notes_start); |
| ... | @@ -8864,7 +8797,7 @@ fn strLitNodeAsString(astgen: *AstGen, node: ast.Node.Index) !IndexSlice { | ... | @@ -8864,7 +8797,7 @@ fn strLitNodeAsString(astgen: *AstGen, node: ast.Node.Index) !IndexSlice { |
| 8864 | while (tok_i <= end) : (tok_i += 1) { | 8797 | while (tok_i <= end) : (tok_i += 1) { |
| 8865 | const slice = tree.tokenSlice(tok_i); | 8798 | const slice = tree.tokenSlice(tok_i); |
| 8866 | const line_bytes = slice[2 .. slice.len - 1]; | 8799 | const line_bytes = slice[2 .. slice.len - 1]; |
| 8867 | try string_bytes.ensureCapacity(gpa, string_bytes.items.len + line_bytes.len + 1); | 8800 | try string_bytes.ensureUnusedCapacity(gpa, line_bytes.len + 1); |
| 8868 | string_bytes.appendAssumeCapacity('\n'); | 8801 | string_bytes.appendAssumeCapacity('\n'); |
| 8869 | string_bytes.appendSliceAssumeCapacity(line_bytes); | 8802 | string_bytes.appendSliceAssumeCapacity(line_bytes); |
| 8870 | } | 8803 | } |
| ... | @@ -9131,8 +9064,8 @@ const GenZir = struct { | ... | @@ -9131,8 +9064,8 @@ const GenZir = struct { |
| 9131 | | 9064 | |
| 9132 | fn setBoolBrBody(gz: GenZir, inst: Zir.Inst.Index) !void { | 9065 | fn setBoolBrBody(gz: GenZir, inst: Zir.Inst.Index) !void { |
| 9133 | const gpa = gz.astgen.gpa; | 9066 | const gpa = gz.astgen.gpa; |
| 9134 | try gz.astgen.extra.ensureCapacity(gpa, gz.astgen.extra.items.len + | 9067 | try gz.astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.Block).Struct.fields.len + |
| 9135 | @typeInfo(Zir.Inst.Block).Struct.fields.len + gz.instructions.items.len); | 9068 | gz.instructions.items.len); |
| 9136 | const zir_datas = gz.astgen.instructions.items(.data); | 9069 | const zir_datas = gz.astgen.instructions.items(.data); |
| 9137 | zir_datas[inst].bool_br.payload_index = gz.astgen.addExtraAssumeCapacity( | 9070 | zir_datas[inst].bool_br.payload_index = gz.astgen.addExtraAssumeCapacity( |
| 9138 | Zir.Inst.Block{ .body_len = @intCast(u32, gz.instructions.items.len) }, | 9071 | Zir.Inst.Block{ .body_len = @intCast(u32, gz.instructions.items.len) }, |
| ... | @@ -9142,8 +9075,8 @@ const GenZir = struct { | ... | @@ -9142,8 +9075,8 @@ const GenZir = struct { |
| 9142 | | 9075 | |
| 9143 | fn setBlockBody(gz: GenZir, inst: Zir.Inst.Index) !void { | 9076 | fn setBlockBody(gz: GenZir, inst: Zir.Inst.Index) !void { |
| 9144 | const gpa = gz.astgen.gpa; | 9077 | const gpa = gz.astgen.gpa; |
| 9145 | try gz.astgen.extra.ensureCapacity(gpa, gz.astgen.extra.items.len + | 9078 | try gz.astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.Block).Struct.fields.len + |
| 9146 | @typeInfo(Zir.Inst.Block).Struct.fields.len + gz.instructions.items.len); | 9079 | gz.instructions.items.len); |
| 9147 | const zir_datas = gz.astgen.instructions.items(.data); | 9080 | const zir_datas = gz.astgen.instructions.items(.data); |
| 9148 | zir_datas[inst].pl_node.payload_index = gz.astgen.addExtraAssumeCapacity( | 9081 | zir_datas[inst].pl_node.payload_index = gz.astgen.addExtraAssumeCapacity( |
| 9149 | Zir.Inst.Block{ .body_len = @intCast(u32, gz.instructions.items.len) }, | 9082 | Zir.Inst.Block{ .body_len = @intCast(u32, gz.instructions.items.len) }, |
| ... | @@ -9155,8 +9088,8 @@ const GenZir = struct { | ... | @@ -9155,8 +9088,8 @@ const GenZir = struct { |
| 9155 | /// `store_to_block_ptr` instructions with lhs set to .none. | 9088 | /// `store_to_block_ptr` instructions with lhs set to .none. |
| 9156 | fn setBlockBodyEliding(gz: GenZir, inst: Zir.Inst.Index) !void { | 9089 | fn setBlockBodyEliding(gz: GenZir, inst: Zir.Inst.Index) !void { |
| 9157 | const gpa = gz.astgen.gpa; | 9090 | const gpa = gz.astgen.gpa; |
| 9158 | try gz.astgen.extra.ensureCapacity(gpa, gz.astgen.extra.items.len + | 9091 | try gz.astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.Block).Struct.fields.len + |
| 9159 | @typeInfo(Zir.Inst.Block).Struct.fields.len + gz.instructions.items.len); | 9092 | gz.instructions.items.len); |
| 9160 | const zir_datas = gz.astgen.instructions.items(.data); | 9093 | const zir_datas = gz.astgen.instructions.items(.data); |
| 9161 | const zir_tags = gz.astgen.instructions.items(.tag); | 9094 | const zir_tags = gz.astgen.instructions.items(.tag); |
| 9162 | const block_pl_index = gz.astgen.addExtraAssumeCapacity(Zir.Inst.Block{ | 9095 | const block_pl_index = gz.astgen.addExtraAssumeCapacity(Zir.Inst.Block{ |
| ... | @@ -9177,7 +9110,6 @@ const GenZir = struct { | ... | @@ -9177,7 +9110,6 @@ const GenZir = struct { |
| 9177 | | 9110 | |
| 9178 | fn addFunc(gz: *GenZir, args: struct { | 9111 | fn addFunc(gz: *GenZir, args: struct { |
| 9179 | src_node: ast.Node.Index, | 9112 | src_node: ast.Node.Index, |
| 9180 | param_types: []const Zir.Inst.Ref, | | |
| 9181 | body: []const Zir.Inst.Index, | 9113 | body: []const Zir.Inst.Index, |
| 9182 | ret_ty: Zir.Inst.Ref, | 9114 | ret_ty: Zir.Inst.Ref, |
| 9183 | cc: Zir.Inst.Ref, | 9115 | cc: Zir.Inst.Ref, |
| ... | @@ -9187,8 +9119,6 @@ const GenZir = struct { | ... | @@ -9187,8 +9119,6 @@ const GenZir = struct { |
| 9187 | is_inferred_error: bool, | 9119 | is_inferred_error: bool, |
| 9188 | is_test: bool, | 9120 | is_test: bool, |
| 9189 | is_extern: bool, | 9121 | is_extern: bool, |
| 9190 | cur_bit_bag: u32, | | |
| 9191 | bit_bag: []const u32, | | |
| 9192 | }) !Zir.Inst.Ref { | 9122 | }) !Zir.Inst.Ref { |
| 9193 | assert(args.src_node != 0); | 9123 | assert(args.src_node != 0); |
| 9194 | assert(args.ret_ty != .none); | 9124 | assert(args.ret_ty != .none); |
| ... | @@ -9226,19 +9156,14 @@ const GenZir = struct { | ... | @@ -9226,19 +9156,14 @@ const GenZir = struct { |
| 9226 | src_locs = &src_locs_buffer; | 9156 | src_locs = &src_locs_buffer; |
| 9227 | } | 9157 | } |
| 9228 | | 9158 | |
| 9229 | const any_are_comptime = args.cur_bit_bag != 0 or for (args.bit_bag) |x| { | | |
| 9230 | if (x != 0) break true; | | |
| 9231 | } else false; | | |
| 9232 | | | |
| 9233 | if (args.cc != .none or args.lib_name != 0 or | 9159 | if (args.cc != .none or args.lib_name != 0 or |
| 9234 | args.is_var_args or args.is_test or args.align_inst != .none or | 9160 | args.is_var_args or args.is_test or args.align_inst != .none or |
| 9235 | args.is_extern or any_are_comptime) | 9161 | args.is_extern) |
| 9236 | { | 9162 | { |
| 9237 | try astgen.extra.ensureUnusedCapacity( | 9163 | try astgen.extra.ensureUnusedCapacity( |
| 9238 | gpa, | 9164 | gpa, |
| 9239 | @typeInfo(Zir.Inst.ExtendedFunc).Struct.fields.len + | 9165 | @typeInfo(Zir.Inst.ExtendedFunc).Struct.fields.len + |
| 9240 | @boolToInt(any_are_comptime) + args.bit_bag.len + | 9166 | args.body.len + src_locs.len + |
| 9241 | args.param_types.len + args.body.len + src_locs.len + | | |
| 9242 | @boolToInt(args.lib_name != 0) + | 9167 | @boolToInt(args.lib_name != 0) + |
| 9243 | @boolToInt(args.align_inst != .none) + | 9168 | @boolToInt(args.align_inst != .none) + |
| 9244 | @boolToInt(args.cc != .none), | 9169 | @boolToInt(args.cc != .none), |
| ... | @@ -9246,7 +9171,6 @@ const GenZir = struct { | ... | @@ -9246,7 +9171,6 @@ const GenZir = struct { |
| 9246 | const payload_index = astgen.addExtraAssumeCapacity(Zir.Inst.ExtendedFunc{ | 9171 | const payload_index = astgen.addExtraAssumeCapacity(Zir.Inst.ExtendedFunc{ |
| 9247 | .src_node = gz.nodeIndexToRelative(args.src_node), | 9172 | .src_node = gz.nodeIndexToRelative(args.src_node), |
| 9248 | .return_type = args.ret_ty, | 9173 | .return_type = args.ret_ty, |
| 9249 | .param_types_len = @intCast(u32, args.param_types.len), | | |
| 9250 | .body_len = @intCast(u32, args.body.len), | 9174 | .body_len = @intCast(u32, args.body.len), |
| 9251 | }); | 9175 | }); |
| 9252 | if (args.lib_name != 0) { | 9176 | if (args.lib_name != 0) { |
| ... | @@ -9258,11 +9182,6 @@ const GenZir = struct { | ... | @@ -9258,11 +9182,6 @@ const GenZir = struct { |
| 9258 | if (args.align_inst != .none) { | 9182 | if (args.align_inst != .none) { |
| 9259 | astgen.extra.appendAssumeCapacity(@enumToInt(args.align_inst)); | 9183 | astgen.extra.appendAssumeCapacity(@enumToInt(args.align_inst)); |
| 9260 | } | 9184 | } |
| 9261 | if (any_are_comptime) { | | |
| 9262 | astgen.extra.appendSliceAssumeCapacity(args.bit_bag); // Likely empty. | | |
| 9263 | astgen.extra.appendAssumeCapacity(args.cur_bit_bag); | | |
| 9264 | } | | |
| 9265 | astgen.appendRefsAssumeCapacity(args.param_types); | | |
| 9266 | astgen.extra.appendSliceAssumeCapacity(args.body); | 9185 | astgen.extra.appendSliceAssumeCapacity(args.body); |
| 9267 | astgen.extra.appendSliceAssumeCapacity(src_locs); | 9186 | astgen.extra.appendSliceAssumeCapacity(src_locs); |
| 9268 | | 9187 | |
| ... | @@ -9279,7 +9198,6 @@ const GenZir = struct { | ... | @@ -9279,7 +9198,6 @@ const GenZir = struct { |
| 9279 | .has_align = args.align_inst != .none, | 9198 | .has_align = args.align_inst != .none, |
| 9280 | .is_test = args.is_test, | 9199 | .is_test = args.is_test, |
| 9281 | .is_extern = args.is_extern, | 9200 | .is_extern = args.is_extern, |
| 9282 | .has_comptime_bits = any_are_comptime, | | |
| 9283 | }), | 9201 | }), |
| 9284 | .operand = payload_index, | 9202 | .operand = payload_index, |
| 9285 | } }, | 9203 | } }, |
| ... | @@ -9290,15 +9208,13 @@ const GenZir = struct { | ... | @@ -9290,15 +9208,13 @@ const GenZir = struct { |
| 9290 | try gz.astgen.extra.ensureUnusedCapacity( | 9208 | try gz.astgen.extra.ensureUnusedCapacity( |
| 9291 | gpa, | 9209 | gpa, |
| 9292 | @typeInfo(Zir.Inst.Func).Struct.fields.len + | 9210 | @typeInfo(Zir.Inst.Func).Struct.fields.len + |
| 9293 | args.param_types.len + args.body.len + src_locs.len, | 9211 | args.body.len + src_locs.len, |
| 9294 | ); | 9212 | ); |
| 9295 | | 9213 | |
| 9296 | const payload_index = gz.astgen.addExtraAssumeCapacity(Zir.Inst.Func{ | 9214 | const payload_index = gz.astgen.addExtraAssumeCapacity(Zir.Inst.Func{ |
| 9297 | .return_type = args.ret_ty, | 9215 | .return_type = args.ret_ty, |
| 9298 | .param_types_len = @intCast(u32, args.param_types.len), | | |
| 9299 | .body_len = @intCast(u32, args.body.len), | 9216 | .body_len = @intCast(u32, args.body.len), |
| 9300 | }); | 9217 | }); |
| 9301 | gz.astgen.appendRefsAssumeCapacity(args.param_types); | | |
| 9302 | gz.astgen.extra.appendSliceAssumeCapacity(args.body); | 9218 | gz.astgen.extra.appendSliceAssumeCapacity(args.body); |
| 9303 | gz.astgen.extra.appendSliceAssumeCapacity(src_locs); | 9219 | gz.astgen.extra.appendSliceAssumeCapacity(src_locs); |
| 9304 | | 9220 | |
| ... | @@ -9380,10 +9296,10 @@ const GenZir = struct { | ... | @@ -9380,10 +9296,10 @@ const GenZir = struct { |
| 9380 | assert(callee != .none); | 9296 | assert(callee != .none); |
| 9381 | assert(src_node != 0); | 9297 | assert(src_node != 0); |
| 9382 | const gpa = gz.astgen.gpa; | 9298 | const gpa = gz.astgen.gpa; |
| 9383 | try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1); | 9299 | try gz.instructions.ensureUnusedCapacity(gpa, 1); |
| 9384 | try gz.astgen.instructions.ensureCapacity(gpa, gz.astgen.instructions.len + 1); | 9300 | try gz.astgen.instructions.ensureUnusedCapacity(gpa, 1); |
| 9385 | try gz.astgen.extra.ensureCapacity(gpa, gz.astgen.extra.items.len + | 9301 | try gz.astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.Call).Struct.fields.len + |
| 9386 | @typeInfo(Zir.Inst.Call).Struct.fields.len + args.len); | 9302 | args.len); |
| 9387 | | 9303 | |
| 9388 | const payload_index = gz.astgen.addExtraAssumeCapacity(Zir.Inst.Call{ | 9304 | const payload_index = gz.astgen.addExtraAssumeCapacity(Zir.Inst.Call{ |
| 9389 | .callee = callee, | 9305 | .callee = callee, |
| ... | @@ -9412,8 +9328,8 @@ const GenZir = struct { | ... | @@ -9412,8 +9328,8 @@ const GenZir = struct { |
| 9412 | ) !Zir.Inst.Index { | 9328 | ) !Zir.Inst.Index { |
| 9413 | assert(lhs != .none); | 9329 | assert(lhs != .none); |
| 9414 | const gpa = gz.astgen.gpa; | 9330 | const gpa = gz.astgen.gpa; |
| 9415 | try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1); | 9331 | try gz.instructions.ensureUnusedCapacity(gpa, 1); |
| 9416 | try gz.astgen.instructions.ensureCapacity(gpa, gz.astgen.instructions.len + 1); | 9332 | try gz.astgen.instructions.ensureUnusedCapacity(gpa, 1); |
| 9417 | | 9333 | |
| 9418 | const new_index = @intCast(Zir.Inst.Index, gz.astgen.instructions.len); | 9334 | const new_index = @intCast(Zir.Inst.Index, gz.astgen.instructions.len); |
| 9419 | gz.astgen.instructions.appendAssumeCapacity(.{ | 9335 | gz.astgen.instructions.appendAssumeCapacity(.{ |
| ... | @@ -9486,8 +9402,8 @@ const GenZir = struct { | ... | @@ -9486,8 +9402,8 @@ const GenZir = struct { |
| 9486 | extra: anytype, | 9402 | extra: anytype, |
| 9487 | ) !Zir.Inst.Ref { | 9403 | ) !Zir.Inst.Ref { |
| 9488 | const gpa = gz.astgen.gpa; | 9404 | const gpa = gz.astgen.gpa; |
| 9489 | try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1); | 9405 | try gz.instructions.ensureUnusedCapacity(gpa, 1); |
| 9490 | try gz.astgen.instructions.ensureCapacity(gpa, gz.astgen.instructions.len + 1); | 9406 | try gz.astgen.instructions.ensureUnusedCapacity(gpa, 1); |
| 9491 | | 9407 | |
| 9492 | const payload_index = try gz.astgen.addExtra(extra); | 9408 | const payload_index = try gz.astgen.addExtra(extra); |
| 9493 | const new_index = @intCast(Zir.Inst.Index, gz.astgen.instructions.len); | 9409 | const new_index = @intCast(Zir.Inst.Index, gz.astgen.instructions.len); |
| ... | @@ -9502,6 +9418,30 @@ const GenZir = struct { | ... | @@ -9502,6 +9418,30 @@ const GenZir = struct { |
| 9502 | return indexToRef(new_index); | 9418 | return indexToRef(new_index); |
| 9503 | } | 9419 | } |
| 9504 | | 9420 | |
| | 9421 | fn addPlTok( |
| | 9422 | gz: *GenZir, |
| | 9423 | tag: Zir.Inst.Tag, |
| | 9424 | /// Absolute token index. This function does the conversion to Decl offset. |
| | 9425 | abs_tok_index: ast.TokenIndex, |
| | 9426 | extra: anytype, |
| | 9427 | ) !Zir.Inst.Ref { |
| | 9428 | const gpa = gz.astgen.gpa; |
| | 9429 | try gz.instructions.ensureUnusedCapacity(gpa, 1); |
| | 9430 | try gz.astgen.instructions.ensureUnusedCapacity(gpa, 1); |
| | 9431 | |
| | 9432 | const payload_index = try gz.astgen.addExtra(extra); |
| | 9433 | const new_index = @intCast(Zir.Inst.Index, gz.astgen.instructions.len); |
| | 9434 | gz.astgen.instructions.appendAssumeCapacity(.{ |
| | 9435 | .tag = tag, |
| | 9436 | .data = .{ .pl_tok = .{ |
| | 9437 | .src_tok = gz.tokenIndexToRelative(abs_tok_index), |
| | 9438 | .payload_index = payload_index, |
| | 9439 | } }, |
| | 9440 | }); |
| | 9441 | gz.instructions.appendAssumeCapacity(new_index); |
| | 9442 | return indexToRef(new_index); |
| | 9443 | } |
| | 9444 | |
| 9505 | fn addExtendedPayload( | 9445 | fn addExtendedPayload( |
| 9506 | gz: *GenZir, | 9446 | gz: *GenZir, |
| 9507 | opcode: Zir.Inst.Extended, | 9447 | opcode: Zir.Inst.Extended, |
| ... | @@ -9509,8 +9449,8 @@ const GenZir = struct { | ... | @@ -9509,8 +9449,8 @@ const GenZir = struct { |
| 9509 | ) !Zir.Inst.Ref { | 9449 | ) !Zir.Inst.Ref { |
| 9510 | const gpa = gz.astgen.gpa; | 9450 | const gpa = gz.astgen.gpa; |
| 9511 | | 9451 | |
| 9512 | try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1); | 9452 | try gz.instructions.ensureUnusedCapacity(gpa, 1); |
| 9513 | try gz.astgen.instructions.ensureCapacity(gpa, gz.astgen.instructions.len + 1); | 9453 | try gz.astgen.instructions.ensureUnusedCapacity(gpa, 1); |
| 9514 | | 9454 | |
| 9515 | const payload_index = try gz.astgen.addExtra(extra); | 9455 | const payload_index = try gz.astgen.addExtra(extra); |
| 9516 | const new_index = @intCast(Zir.Inst.Index, gz.astgen.instructions.len); | 9456 | const new_index = @intCast(Zir.Inst.Index, gz.astgen.instructions.len); |
| ... | @@ -9566,8 +9506,8 @@ const GenZir = struct { | ... | @@ -9566,8 +9506,8 @@ const GenZir = struct { |
| 9566 | elem_type: Zir.Inst.Ref, | 9506 | elem_type: Zir.Inst.Ref, |
| 9567 | ) !Zir.Inst.Ref { | 9507 | ) !Zir.Inst.Ref { |
| 9568 | const gpa = gz.astgen.gpa; | 9508 | const gpa = gz.astgen.gpa; |
| 9569 | try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1); | 9509 | try gz.instructions.ensureUnusedCapacity(gpa, 1); |
| 9570 | try gz.astgen.instructions.ensureCapacity(gpa, gz.astgen.instructions.len + 1); | 9510 | try gz.astgen.instructions.ensureUnusedCapacity(gpa, 1); |
| 9571 | | 9511 | |
| 9572 | const payload_index = try gz.astgen.addExtra(Zir.Inst.ArrayTypeSentinel{ | 9512 | const payload_index = try gz.astgen.addExtra(Zir.Inst.ArrayTypeSentinel{ |
| 9573 | .sentinel = sentinel, | 9513 | .sentinel = sentinel, |
| ... | @@ -9822,7 +9762,7 @@ const GenZir = struct { | ... | @@ -9822,7 +9762,7 @@ const GenZir = struct { |
| 9822 | /// Leaves the `payload_index` field undefined. | 9762 | /// Leaves the `payload_index` field undefined. |
| 9823 | fn addCondBr(gz: *GenZir, tag: Zir.Inst.Tag, node: ast.Node.Index) !Zir.Inst.Index { | 9763 | fn addCondBr(gz: *GenZir, tag: Zir.Inst.Tag, node: ast.Node.Index) !Zir.Inst.Index { |
| 9824 | const gpa = gz.astgen.gpa; | 9764 | const gpa = gz.astgen.gpa; |
| 9825 | try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1); | 9765 | try gz.instructions.ensureUnusedCapacity(gpa, 1); |
| 9826 | const new_index = @intCast(Zir.Inst.Index, gz.astgen.instructions.len); | 9766 | const new_index = @intCast(Zir.Inst.Index, gz.astgen.instructions.len); |
| 9827 | try gz.astgen.instructions.append(gpa, .{ | 9767 | try gz.astgen.instructions.append(gpa, .{ |
| 9828 | .tag = tag, | 9768 | .tag = tag, |