authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-10 16:27:23-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-20 12:19:14-07:00
log9918a5fbe3dc910f90f2c60ad74edb51de53e0cf
treee46e46583051c8e9aa27347d91bafe193b2f0da2
parent3c3abaf3907e344305620fb4565e7c1acb0a9c88

AstGen: remove unneeded field ref_start_index

Previously, this field was used because the Zir.Inst.Ref encoding supported the concept of references to function parameters. However now thanks to whole-file-astgen, the implementations of indexToRef and refToIndex are trivial addition/subtraction of a comptime const integer.

1 files changed, 56 insertions(+), 61 deletions(-)

src/AstGen.zig+56-61
......@@ -989,7 +989,7 @@ fn suspendExpr(
989989 }
990990 try suspend_scope.setBlockBody(suspend_inst);
991991
992 return gz.indexToRef(suspend_inst);
992 return indexToRef(suspend_inst);
993993}
994994
995995fn awaitExpr(
......@@ -1300,7 +1300,7 @@ fn arrayInitExprRlPtr(
13001300 .lhs = result_ptr,
13011301 .rhs = index_inst,
13021302 });
1303 elem_ptr_list[i] = gz.refToIndex(elem_ptr).?;
1303 elem_ptr_list[i] = refToIndex(elem_ptr).?;
13041304 _ = try expr(gz, scope, .{ .ptr = elem_ptr }, elem_init);
13051305 }
13061306 _ = try gz.addPlNode(.validate_array_init_ptr, node, Zir.Inst.Block{
......@@ -1455,7 +1455,7 @@ fn structInitExprRlPtr(
14551455 .lhs = result_ptr,
14561456 .field_name_start = str_index,
14571457 });
1458 field_ptr_list[i] = gz.refToIndex(field_ptr).?;
1458 field_ptr_list[i] = refToIndex(field_ptr).?;
14591459 _ = try expr(gz, scope, .{ .ptr = field_ptr }, field_init);
14601460 }
14611461 _ = try gz.addPlNode(.validate_struct_init_ptr, node, Zir.Inst.Block{
......@@ -1489,7 +1489,7 @@ fn structInitExprRlTy(
14891489 .name_start = str_index,
14901490 });
14911491 fields_list[i] = .{
1492 .field_type = gz.refToIndex(field_ty_inst).?,
1492 .field_type = refToIndex(field_ty_inst).?,
14931493 .init = try expr(gz, scope, .{ .ty = field_ty_inst }, field_init),
14941494 };
14951495 }
......@@ -1786,7 +1786,7 @@ fn labeledBlockExpr(
17861786 }
17871787 try block_scope.setBlockBody(block_inst);
17881788
1789 return gz.indexToRef(block_inst);
1789 return indexToRef(block_inst);
17901790 },
17911791 .break_operand => {
17921792 // All break operands are values that did not use the result location pointer.
......@@ -1800,7 +1800,7 @@ fn labeledBlockExpr(
18001800 } else {
18011801 try block_scope.setBlockBody(block_inst);
18021802 }
1803 const block_ref = gz.indexToRef(block_inst);
1803 const block_ref = indexToRef(block_inst);
18041804 switch (rl) {
18051805 .ref => return block_ref,
18061806 else => return rvalue(gz, rl, block_ref, block_node),
......@@ -1878,7 +1878,7 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: ast.Node.Index) Inner
18781878 // we want to avoid adding the ZIR instruction if possible for performance.
18791879 const maybe_unused_result = try expr(gz, scope, .none, statement);
18801880 var noreturn_src_node: ast.Node.Index = 0;
1881 const elide_check = if (gz.refToIndex(maybe_unused_result)) |inst| b: {
1881 const elide_check = if (refToIndex(maybe_unused_result)) |inst| b: {
18821882 // Note that this array becomes invalid after appending more items to it
18831883 // in the above while loop.
18841884 const zir_tags = gz.astgen.instructions.items(.tag);
......@@ -2440,7 +2440,7 @@ fn varDecl(
24402440 // the alloc instruction and the store_to_block_ptr instruction.
24412441 try parent_zir.ensureUnusedCapacity(gpa, init_scope.instructions.items.len);
24422442 for (init_scope.instructions.items) |src_inst| {
2443 if (gz.indexToRef(src_inst) == init_scope.rl_ptr) continue;
2443 if (indexToRef(src_inst) == init_scope.rl_ptr) continue;
24442444 if (zir_tags[src_inst] == .store_to_block_ptr) {
24452445 if (zir_datas[src_inst].bin.lhs == init_scope.rl_ptr) continue;
24462446 }
......@@ -2743,7 +2743,7 @@ fn ptrType(
27432743 }
27442744
27452745 const new_index = @intCast(Zir.Inst.Index, gz.astgen.instructions.len);
2746 const result = gz.indexToRef(new_index);
2746 const result = indexToRef(new_index);
27472747 gz.astgen.instructions.appendAssumeCapacity(.{ .tag = .ptr_type, .data = .{
27482748 .ptr_type = .{
27492749 .flags = .{
......@@ -3473,7 +3473,7 @@ fn structDeclInner(
34733473 .body_len = 0,
34743474 .decls_len = 0,
34753475 });
3476 return gz.indexToRef(decl_inst);
3476 return indexToRef(decl_inst);
34773477 }
34783478
34793479 const astgen = gz.astgen;
......@@ -3492,7 +3492,6 @@ fn structDeclInner(
34923492 .astgen = astgen,
34933493 .force_comptime = true,
34943494 .in_defer = false,
3495 .ref_start_index = gz.ref_start_index,
34963495 };
34973496 defer block_scope.instructions.deinit(gpa);
34983497
......@@ -3730,7 +3729,7 @@ fn structDeclInner(
37303729 }
37313730 astgen.extra.appendSliceAssumeCapacity(fields_data.items);
37323731
3733 return gz.indexToRef(decl_inst);
3732 return indexToRef(decl_inst);
37343733}
37353734
37363735fn unionDeclInner(
......@@ -3758,7 +3757,6 @@ fn unionDeclInner(
37583757 .astgen = astgen,
37593758 .force_comptime = true,
37603759 .in_defer = false,
3761 .ref_start_index = gz.ref_start_index,
37623760 };
37633761 defer block_scope.instructions.deinit(gpa);
37643762
......@@ -4006,7 +4004,7 @@ fn unionDeclInner(
40064004 astgen.extra.appendAssumeCapacity(cur_bit_bag);
40074005 astgen.extra.appendSliceAssumeCapacity(fields_data.items);
40084006
4009 return gz.indexToRef(decl_inst);
4007 return indexToRef(decl_inst);
40104008}
40114009
40124010fn containerDecl(
......@@ -4170,7 +4168,6 @@ fn containerDecl(
41704168 .astgen = astgen,
41714169 .force_comptime = true,
41724170 .in_defer = false,
4173 .ref_start_index = gz.ref_start_index,
41744171 };
41754172 defer block_scope.instructions.deinit(gpa);
41764173
......@@ -4398,7 +4395,7 @@ fn containerDecl(
43984395 astgen.extra.appendAssumeCapacity(cur_bit_bag);
43994396 astgen.extra.appendSliceAssumeCapacity(fields_data.items);
44004397
4401 return rvalue(gz, rl, gz.indexToRef(decl_inst), node);
4398 return rvalue(gz, rl, indexToRef(decl_inst), node);
44024399 },
44034400 .keyword_opaque => {
44044401 var namespace: Scope.Namespace = .{ .parent = scope };
......@@ -4559,7 +4556,7 @@ fn containerDecl(
45594556 }
45604557 astgen.extra.appendSliceAssumeCapacity(wip_decls.payload.items);
45614558
4562 return rvalue(gz, rl, gz.indexToRef(decl_inst), node);
4559 return rvalue(gz, rl, indexToRef(decl_inst), node);
45634560 },
45644561 else => unreachable,
45654562 }
......@@ -4797,7 +4794,7 @@ fn finishThenElseBlock(
47974794 }
47984795 assert(!strat.elide_store_to_block_ptr_instructions);
47994796 try setCondBrPayload(condbr, cond, then_scope, else_scope);
4800 return parent_gz.indexToRef(main_block);
4797 return indexToRef(main_block);
48014798 },
48024799 .break_operand => {
48034800 if (!parent_gz.refIsNoReturn(then_result)) {
......@@ -4815,7 +4812,7 @@ fn finishThenElseBlock(
48154812 } else {
48164813 try setCondBrPayload(condbr, cond, then_scope, else_scope);
48174814 }
4818 const block_ref = parent_gz.indexToRef(main_block);
4815 const block_ref = indexToRef(main_block);
48194816 switch (rl) {
48204817 .ref => return block_ref,
48214818 else => return rvalue(parent_gz, rl, block_ref, node),
......@@ -4937,7 +4934,7 @@ fn boolBinOp(
49374934 }
49384935 try rhs_scope.setBoolBrBody(bool_br);
49394936
4940 const block_ref = gz.indexToRef(bool_br);
4937 const block_ref = indexToRef(bool_br);
49414938 return rvalue(gz, rl, block_ref, node);
49424939}
49434940
......@@ -5959,7 +5956,7 @@ fn switchExpr(
59595956 if (!strat.elide_store_to_block_ptr_instructions) {
59605957 astgen.extra.appendSliceAssumeCapacity(scalar_cases_payload.items);
59615958 astgen.extra.appendSliceAssumeCapacity(multi_cases_payload.items);
5962 return parent_gz.indexToRef(switch_block);
5959 return indexToRef(switch_block);
59635960 }
59645961
59655962 // There will necessarily be a store_to_block_ptr for
......@@ -6003,7 +6000,7 @@ fn switchExpr(
60036000 .lhs = block_scope.rl_ty_inst,
60046001 .rhs = zir_datas[break_inst].@"break".operand,
60056002 };
6006 zir_datas[break_inst].@"break".operand = parent_gz.indexToRef(store_inst);
6003 zir_datas[break_inst].@"break".operand = indexToRef(store_inst);
60076004 } else {
60086005 scalar_cases_payload.items[body_len_index] -= 1;
60096006 astgen.extra.appendSliceAssumeCapacity(scalar_cases_payload.items[0..extra_index]);
......@@ -6045,7 +6042,7 @@ fn switchExpr(
60456042 .lhs = block_scope.rl_ty_inst,
60466043 .rhs = zir_datas[break_inst].@"break".operand,
60476044 };
6048 zir_datas[break_inst].@"break".operand = parent_gz.indexToRef(store_inst);
6045 zir_datas[break_inst].@"break".operand = indexToRef(store_inst);
60496046 } else {
60506047 scalar_cases_payload.items[body_len_index] -= 1;
60516048 astgen.extra.appendSliceAssumeCapacity(scalar_cases_payload.items[start_index..extra_index]);
......@@ -6091,7 +6088,7 @@ fn switchExpr(
60916088 .lhs = block_scope.rl_ty_inst,
60926089 .rhs = zir_datas[break_inst].@"break".operand,
60936090 };
6094 zir_datas[break_inst].@"break".operand = parent_gz.indexToRef(store_inst);
6091 zir_datas[break_inst].@"break".operand = indexToRef(store_inst);
60956092 } else {
60966093 assert(zir_datas[store_inst].bin.lhs == block_scope.rl_ptr);
60976094 multi_cases_payload.items[body_len_index] -= 1;
......@@ -6102,7 +6099,7 @@ fn switchExpr(
61026099 }
61036100 }
61046101
6105 const block_ref = parent_gz.indexToRef(switch_block);
6102 const block_ref = indexToRef(switch_block);
61066103 switch (rl) {
61076104 .ref => return block_ref,
61086105 else => return rvalue(parent_gz, rl, block_ref, switch_node),
......@@ -6162,7 +6159,7 @@ fn switchExpr(
61626159 }
61636160 }
61646161
6165 return parent_gz.indexToRef(switch_block);
6162 return indexToRef(switch_block);
61666163 },
61676164 }
61686165}
......@@ -6861,7 +6858,7 @@ fn asRlPtr(
68616858 const zir_datas = astgen.instructions.items(.data);
68626859 try parent_zir.ensureUnusedCapacity(astgen.gpa, as_scope.instructions.items.len);
68636860 for (as_scope.instructions.items) |src_inst| {
6864 if (parent_gz.indexToRef(src_inst) == as_scope.rl_ptr) continue;
6861 if (indexToRef(src_inst) == as_scope.rl_ptr) continue;
68656862 if (zir_tags[src_inst] == .store_to_block_ptr) {
68666863 if (zir_datas[src_inst].bin.lhs == as_scope.rl_ptr) continue;
68676864 }
......@@ -6992,10 +6989,10 @@ fn builtinCall(
69926989 const str_lit_token = main_tokens[operand_node];
69936990 const str = try astgen.strLitAsString(str_lit_token);
69946991 const result = try gz.addStrTok(.import, str.index, str_lit_token);
6995 const gop = try astgen.imports.getOrPut(astgen.gpa, str.index);
6996 if (!gop.found_existing) {
6997 gop.value_ptr.* = str_lit_token;
6998 }
6992 const gop = try astgen.imports.getOrPut(astgen.gpa, str.index);
6993 if (!gop.found_existing) {
6994 gop.value_ptr.* = str_lit_token;
6995 }
69996996 return rvalue(gz, rl, result, node);
70006997 },
70016998 .compile_log => {
......@@ -8705,9 +8702,6 @@ const GenZir = struct {
87058702 in_defer: bool,
87068703 /// How decls created in this scope should be named.
87078704 anon_name_strategy: Zir.Inst.NameStrategy = .anon,
8708 /// The end of special indexes. `Zir.Inst.Ref` subtracts against this number to convert
8709 /// to `Zir.Inst.Index`. The default here is correct if there are 0 parameters.
8710 ref_start_index: u32 = Zir.Inst.Ref.typed_value_map.len,
87118705 /// The containing decl AST node.
87128706 decl_node_index: ast.Node.Index,
87138707 /// The containing decl line index, absolute.
......@@ -8751,7 +8745,6 @@ const GenZir = struct {
87518745 return .{
87528746 .force_comptime = gz.force_comptime,
87538747 .in_defer = gz.in_defer,
8754 .ref_start_index = gz.ref_start_index,
87558748 .decl_node_index = gz.decl_node_index,
87568749 .decl_line = gz.decl_line,
87578750 .parent = scope,
......@@ -8769,7 +8762,7 @@ const GenZir = struct {
87698762
87708763 fn refIsNoReturn(gz: GenZir, inst_ref: Zir.Inst.Ref) bool {
87718764 if (inst_ref == .unreachable_value) return true;
8772 if (gz.refToIndex(inst_ref)) |inst_index| {
8765 if (refToIndex(inst_ref)) |inst_index| {
87738766 return gz.astgen.instructions.items(.tag)[inst_index].isNoReturn();
87748767 }
87758768 return false;
......@@ -8807,19 +8800,6 @@ const GenZir = struct {
88078800 return gz.astgen.tree.firstToken(gz.decl_node_index);
88088801 }
88098802
8810 fn indexToRef(gz: GenZir, inst: Zir.Inst.Index) Zir.Inst.Ref {
8811 return @intToEnum(Zir.Inst.Ref, gz.ref_start_index + inst);
8812 }
8813
8814 fn refToIndex(gz: GenZir, inst: Zir.Inst.Ref) ?Zir.Inst.Index {
8815 const ref_int = @enumToInt(inst);
8816 if (ref_int >= gz.ref_start_index) {
8817 return ref_int - gz.ref_start_index;
8818 } else {
8819 return null;
8820 }
8821 }
8822
88238803 fn setBreakResultLoc(gz: *GenZir, parent_rl: AstGen.ResultLoc) void {
88248804 // Depending on whether the result location is a pointer or value, different
88258805 // ZIR needs to be generated. In the former case we rely on storing to the
......@@ -8998,7 +8978,7 @@ const GenZir = struct {
89988978 } },
89998979 });
90008980 gz.instructions.appendAssumeCapacity(new_index);
9001 return gz.indexToRef(new_index);
8981 return indexToRef(new_index);
90028982 } else {
90038983 try gz.astgen.extra.ensureUnusedCapacity(
90048984 gpa,
......@@ -9025,7 +9005,7 @@ const GenZir = struct {
90259005 } },
90269006 });
90279007 gz.instructions.appendAssumeCapacity(new_index);
9028 return gz.indexToRef(new_index);
9008 return indexToRef(new_index);
90299009 }
90309010 }
90319011
......@@ -9079,7 +9059,7 @@ const GenZir = struct {
90799059 } },
90809060 });
90819061 gz.instructions.appendAssumeCapacity(new_index);
9082 return gz.indexToRef(new_index);
9062 return indexToRef(new_index);
90839063 }
90849064
90859065 fn addCall(
......@@ -9113,7 +9093,7 @@ const GenZir = struct {
91139093 } },
91149094 });
91159095 gz.instructions.appendAssumeCapacity(new_index);
9116 return gz.indexToRef(new_index);
9096 return indexToRef(new_index);
91179097 }
91189098
91199099 /// Note that this returns a `Zir.Inst.Index` not a ref.
......@@ -9164,7 +9144,7 @@ const GenZir = struct {
91649144 });
91659145 gz.instructions.appendAssumeCapacity(new_index);
91669146 astgen.string_bytes.appendSliceAssumeCapacity(mem.sliceAsBytes(limbs));
9167 return gz.indexToRef(new_index);
9147 return indexToRef(new_index);
91689148 }
91699149
91709150 fn addFloat(gz: *GenZir, number: f32, src_node: ast.Node.Index) !Zir.Inst.Ref {
......@@ -9215,7 +9195,7 @@ const GenZir = struct {
92159195 } },
92169196 });
92179197 gz.instructions.appendAssumeCapacity(new_index);
9218 return gz.indexToRef(new_index);
9198 return indexToRef(new_index);
92199199 }
92209200
92219201 fn addExtendedPayload(
......@@ -9239,7 +9219,7 @@ const GenZir = struct {
92399219 } },
92409220 });
92419221 gz.instructions.appendAssumeCapacity(new_index);
9242 return gz.indexToRef(new_index);
9222 return indexToRef(new_index);
92439223 }
92449224
92459225 fn addExtendedMultiOp(
......@@ -9272,7 +9252,7 @@ const GenZir = struct {
92729252 });
92739253 gz.instructions.appendAssumeCapacity(new_index);
92749254 astgen.appendRefsAssumeCapacity(operands);
9275 return gz.indexToRef(new_index);
9255 return indexToRef(new_index);
92769256 }
92779257
92789258 fn addArrayTypeSentinel(
......@@ -9298,7 +9278,7 @@ const GenZir = struct {
92989278 } },
92999279 });
93009280 gz.instructions.appendAssumeCapacity(new_index);
9301 return gz.indexToRef(new_index);
9281 return indexToRef(new_index);
93029282 }
93039283
93049284 fn addUnTok(
......@@ -9457,7 +9437,7 @@ const GenZir = struct {
94579437 } },
94589438 });
94599439 gz.instructions.appendAssumeCapacity(new_index);
9460 return gz.indexToRef(new_index);
9440 return indexToRef(new_index);
94619441 }
94629442
94639443 fn addAsm(
......@@ -9515,7 +9495,7 @@ const GenZir = struct {
95159495 } },
95169496 });
95179497 gz.instructions.appendAssumeCapacity(new_index);
9518 return gz.indexToRef(new_index);
9498 return indexToRef(new_index);
95199499 }
95209500
95219501 /// Note that this returns a `Zir.Inst.Index` not a ref.
......@@ -9693,7 +9673,7 @@ const GenZir = struct {
96939673 }
96949674
96959675 fn add(gz: *GenZir, inst: Zir.Inst) !Zir.Inst.Ref {
9696 return gz.indexToRef(try gz.addAsIndex(inst));
9676 return indexToRef(try gz.addAsIndex(inst));
96979677 }
96989678
96999679 fn addAsIndex(gz: *GenZir, inst: Zir.Inst) !Zir.Inst.Index {
......@@ -9840,3 +9820,18 @@ fn advanceSourceCursor(astgen: *AstGen, source: []const u8, end: usize) void {
98409820 astgen.source_line = line;
98419821 astgen.source_column = column;
98429822}
9823
9824const ref_start_index = Zir.Inst.Ref.typed_value_map.len;
9825
9826fn indexToRef(inst: Zir.Inst.Index) Zir.Inst.Ref {
9827 return @intToEnum(Zir.Inst.Ref, ref_start_index + inst);
9828}
9829
9830fn refToIndex(inst: Zir.Inst.Ref) ?Zir.Inst.Index {
9831 const ref_int = @enumToInt(inst);
9832 if (ref_int >= ref_start_index) {
9833 return ref_int - ref_start_index;
9834 } else {
9835 return null;
9836 }
9837}