authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-02-17 13:44:35-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-02-18 19:17:20-07:00
log841add6890d001d315591dc20f7d464c264d88bb
treeb2680e04e8e16c1ff52a317830c51398cc79c0bf
parentfaa44e2e5875036b105d8b7d38ccb2e93757a3c5

AstGen: finish multi-object for loops

This strategy uses pointer arithmetic to iterate through the loop. This has a problem, however, which is tuples. AstGen does not know whether a given indexable is a tuple or can be iterated based on contiguous memory. Tuples unlike other indexables cannot be represented as a many-item pointer that is incremented as the loop counter. So, after this commit, I will modify AstGen back closer to how @vexu had it before, using a counter and array element access.

4 files changed, 103 insertions(+), 88 deletions(-)

src/AstGen.zig+68-71
......@@ -88,6 +88,7 @@ fn setExtra(astgen: *AstGen, index: usize, extra: anytype) void {
8888 Zir.Inst.BuiltinCall.Flags => @bitCast(u32, @field(extra, field.name)),
8989 Zir.Inst.SwitchBlock.Bits => @bitCast(u32, @field(extra, field.name)),
9090 Zir.Inst.FuncFancy.Bits => @bitCast(u32, @field(extra, field.name)),
91 Zir.Inst.ElemPtrImm.Bits => @bitCast(u32, @field(extra, field.name)),
9192 else => @compileError("bad field type"),
9293 };
9394 i += 1;
......@@ -1565,7 +1566,9 @@ fn arrayInitExprRlPtrInner(
15651566 for (elements) |elem_init, i| {
15661567 const elem_ptr = try gz.addPlNode(.elem_ptr_imm, elem_init, Zir.Inst.ElemPtrImm{
15671568 .ptr = result_ptr,
1568 .index = @intCast(u32, i),
1569 .bits = .{
1570 .index = @intCast(u31, i),
1571 },
15691572 });
15701573 astgen.extra.items[extra_index] = refToIndex(elem_ptr).?;
15711574 extra_index += 1;
......@@ -6308,7 +6311,7 @@ fn forExpr(
63086311 const lens = try gpa.alloc(Zir.Inst.Ref, for_full.ast.inputs.len);
63096312 defer gpa.free(lens);
63106313
6311 const counter_alloc_tag: Zir.Inst.Tag = if (is_inline) .alloc_comptime_mut else .alloc;
6314 const alloc_tag: Zir.Inst.Tag = if (is_inline) .alloc_comptime_mut else .alloc_mut;
63126315
63136316 // Tracks the index of allocs/lens that has a length to be checked and is
63146317 // used for the end value.
......@@ -6321,23 +6324,24 @@ fn forExpr(
63216324 var cond_end_val: Zir.Inst.Ref = .none;
63226325
63236326 {
6324 var payload = for_full.payload_token;
6327 var capture_token = for_full.payload_token;
63256328 for (for_full.ast.inputs) |input, i_usize| {
63266329 const i = @intCast(u32, i_usize);
6327 const payload_is_ref = token_tags[payload] == .asterisk;
6328 const ident_tok = payload + @boolToInt(payload_is_ref);
6330 const capture_is_ref = token_tags[capture_token] == .asterisk;
6331 const ident_tok = capture_token + @boolToInt(capture_is_ref);
63296332
6330 if (mem.eql(u8, tree.tokenSlice(ident_tok), "_") and payload_is_ref) {
6331 return astgen.failTok(payload, "pointer modifier invalid on discard", .{});
6333 if (mem.eql(u8, tree.tokenSlice(ident_tok), "_") and capture_is_ref) {
6334 return astgen.failTok(capture_token, "pointer modifier invalid on discard", .{});
63326335 }
6333 payload = ident_tok + @as(u32, 2);
6336 // Skip over the comma, and on to the next capture (or the ending pipe character).
6337 capture_token = ident_tok + 2;
63346338
63356339 try emitDbgNode(parent_gz, input);
63366340 if (node_tags[input] == .for_range) {
6337 if (payload_is_ref) {
6341 if (capture_is_ref) {
63386342 return astgen.failTok(ident_tok, "cannot capture reference to range", .{});
63396343 }
6340 const counter_ptr = try parent_gz.addUnNode(counter_alloc_tag, .usize_type, node);
6344 const counter_ptr = try parent_gz.addUnNode(alloc_tag, .usize_type, node);
63416345 const start_node = node_data[input].lhs;
63426346 const start_val = try expr(parent_gz, scope, .{ .rl = .none }, start_node);
63436347 _ = try parent_gz.addBin(.store, counter_ptr, start_val);
......@@ -6364,20 +6368,28 @@ fn forExpr(
63646368 allocs[i] = counter_ptr;
63656369 lens[i] = range_len;
63666370 } else {
6367 const cond_ri: ResultInfo = .{ .rl = if (payload_is_ref) .ref else .none };
6368 const indexable = try expr(parent_gz, scope, cond_ri, input);
6371 const indexable = try expr(parent_gz, scope, .{ .rl = .none }, input);
6372 // This instruction has nice compile errors so we put it before the other ones
6373 // even though it is not needed until later in the block.
6374 const ptr_len = try parent_gz.addUnNode(.indexable_ptr_len, indexable, input);
63696375 const base_ptr = try parent_gz.addPlNode(.elem_ptr_imm, input, Zir.Inst.ElemPtrImm{
63706376 .ptr = indexable,
6371 .index = 0,
6377 .bits = .{
6378 .index = 0,
6379 .manyptr = true,
6380 },
63726381 });
6382 const alloc_ty_inst = try parent_gz.addUnNode(.typeof, base_ptr, node);
6383 const alloc = try parent_gz.addUnNode(alloc_tag, alloc_ty_inst, node);
6384 _ = try parent_gz.addBin(.store, alloc, base_ptr);
63736385
63746386 if (end_input_index == null) {
63756387 end_input_index = i;
63766388 assert(cond_end_val == .none);
63776389 }
63786390
6379 allocs[i] = base_ptr;
6380 lens[i] = try parent_gz.addUnNode(.indexable_ptr_len, indexable, input);
6391 allocs[i] = alloc;
6392 lens[i] = ptr_len;
63816393 }
63826394 }
63836395 }
......@@ -6467,62 +6479,47 @@ fn forExpr(
64676479 var then_scope = parent_gz.makeSubBlock(&cond_scope.base);
64686480 defer then_scope.unstack();
64696481
6470 const then_sub_scope = &then_scope.base;
6471
6472 // try then_scope.addDbgBlockBegin();
6473 // var payload_val_scope: Scope.LocalVal = undefined;
6474 // var index_scope: Scope.LocalPtr = undefined;
6475 // const then_sub_scope = blk: {
6476 // const payload_token = for_full.payload_token.?;
6477 // const ident = if (token_tags[payload_token] == .asterisk)
6478 // payload_token + 1
6479 // else
6480 // payload_token;
6481 // const is_ptr = ident != payload_token;
6482 // const value_name = tree.tokenSlice(ident);
6483 // var payload_sub_scope: *Scope = undefined;
6484 // if (!mem.eql(u8, value_name, "_")) {
6485 // const name_str_index = try astgen.identAsString(ident);
6486 // const tag: Zir.Inst.Tag = if (is_ptr) .elem_ptr else .elem_val;
6487 // const payload_inst = try then_scope.addPlNode(tag, for_full.ast.cond_expr, Zir.Inst.Bin{
6488 // .lhs = array_ptr,
6489 // .rhs = index,
6490 // });
6491 // try astgen.detectLocalShadowing(&then_scope.base, name_str_index, ident, value_name, .capture);
6492 // payload_val_scope = .{
6493 // .parent = &then_scope.base,
6494 // .gen_zir = &then_scope,
6495 // .name = name_str_index,
6496 // .inst = payload_inst,
6497 // .token_src = ident,
6498 // .id_cat = .capture,
6499 // };
6500 // try then_scope.addDbgVar(.dbg_var_val, name_str_index, payload_inst);
6501 // payload_sub_scope = &payload_val_scope.base;
6502 // } else if (is_ptr) {
6503 // } else {
6504 // payload_sub_scope = &then_scope.base;
6505 // }
6506
6507 // const index_token = if (token_tags[ident + 1] == .comma)
6508 // ident + 2
6509 // else
6510 // break :blk payload_sub_scope;
6511 // const token_bytes = tree.tokenSlice(index_token);
6512 // const index_name = try astgen.identAsString(index_token);
6513 // try astgen.detectLocalShadowing(payload_sub_scope, index_name, index_token, token_bytes, .@"loop index capture");
6514 // index_scope = .{
6515 // .parent = payload_sub_scope,
6516 // .gen_zir = &then_scope,
6517 // .name = index_name,
6518 // .ptr = index_ptr,
6519 // .token_src = index_token,
6520 // .maybe_comptime = is_inline,
6521 // .id_cat = .@"loop index capture",
6522 // };
6523 // try then_scope.addDbgVar(.dbg_var_val, index_name, index_ptr);
6524 // break :blk &index_scope.base;
6525 // };
6482 try then_scope.addDbgBlockBegin();
6483
6484 const capture_scopes = try gpa.alloc(Scope.LocalVal, for_full.ast.inputs.len);
6485 defer gpa.free(capture_scopes);
6486
6487 const then_sub_scope = blk: {
6488 var capture_token = for_full.payload_token;
6489 var capture_sub_scope: *Scope = &then_scope.base;
6490 for (for_full.ast.inputs) |input, i_usize| {
6491 const i = @intCast(u32, i_usize);
6492 const capture_is_ref = token_tags[capture_token] == .asterisk;
6493 const ident_tok = capture_token + @boolToInt(capture_is_ref);
6494 const capture_name = tree.tokenSlice(ident_tok);
6495 // Skip over the comma, and on to the next capture (or the ending pipe character).
6496 capture_token = ident_tok + 2;
6497
6498 if (mem.eql(u8, capture_name, "_")) continue;
6499
6500 const name_str_index = try astgen.identAsString(ident_tok);
6501 try astgen.detectLocalShadowing(capture_sub_scope, name_str_index, ident_tok, capture_name, .capture);
6502
6503 const loaded = if (capture_is_ref)
6504 loaded_ptrs[i]
6505 else
6506 try then_scope.addUnNode(.load, loaded_ptrs[i], input);
6507
6508 capture_scopes[i] = .{
6509 .parent = capture_sub_scope,
6510 .gen_zir = &then_scope,
6511 .name = name_str_index,
6512 .inst = loaded,
6513 .token_src = ident_tok,
6514 .id_cat = .capture,
6515 };
6516
6517 try then_scope.addDbgVar(.dbg_var_val, name_str_index, loaded);
6518 capture_sub_scope = &capture_scopes[i].base;
6519 }
6520
6521 break :blk capture_sub_scope;
6522 };
65266523
65276524 const then_result = try expr(&then_scope, then_sub_scope, .{ .rl = .none }, for_full.ast.then_expr);
65286525 _ = try addEnsureResult(&then_scope, then_result, for_full.ast.then_expr);
src/Sema.zig+22-15
......@@ -9649,7 +9649,7 @@ fn zirElemPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
96499649 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
96509650 const array_ptr = try sema.resolveInst(extra.lhs);
96519651 const elem_index = try sema.resolveInst(extra.rhs);
9652 return sema.elemPtr(block, src, array_ptr, elem_index, src, false);
9652 return sema.elemPtr(block, src, array_ptr, elem_index, src, false, .One);
96539653}
96549654
96559655fn zirElemPtrNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
......@@ -9662,7 +9662,7 @@ fn zirElemPtrNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
96629662 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
96639663 const array_ptr = try sema.resolveInst(extra.lhs);
96649664 const elem_index = try sema.resolveInst(extra.rhs);
9665 return sema.elemPtr(block, src, array_ptr, elem_index, elem_index_src, false);
9665 return sema.elemPtr(block, src, array_ptr, elem_index, elem_index_src, false, .One);
96669666}
96679667
96689668fn zirElemPtrImm(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
......@@ -9673,8 +9673,9 @@ fn zirElemPtrImm(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
96739673 const src = inst_data.src();
96749674 const extra = sema.code.extraData(Zir.Inst.ElemPtrImm, inst_data.payload_index).data;
96759675 const array_ptr = try sema.resolveInst(extra.ptr);
9676 const elem_index = try sema.addIntUnsigned(Type.usize, extra.index);
9677 return sema.elemPtr(block, src, array_ptr, elem_index, src, true);
9676 const elem_index = try sema.addIntUnsigned(Type.usize, extra.bits.index);
9677 const size: std.builtin.Type.Pointer.Size = if (extra.bits.manyptr) .Many else .One;
9678 return sema.elemPtr(block, src, array_ptr, elem_index, src, true, size);
96789679}
96799680
96809681fn zirSliceStart(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
......@@ -22905,7 +22906,7 @@ fn panicSentinelMismatch(
2290522906 const actual_sentinel = if (ptr_ty.isSlice())
2290622907 try parent_block.addBinOp(.slice_elem_val, ptr, sentinel_index)
2290722908 else blk: {
22908 const elem_ptr_ty = try sema.elemPtrType(ptr_ty, null);
22909 const elem_ptr_ty = try sema.elemPtrType(ptr_ty, null, .One);
2290922910 const sentinel_ptr = try parent_block.addPtrElemPtr(ptr, sentinel_index, elem_ptr_ty);
2291022911 break :blk try parent_block.addTyOp(.load, sentinel_ty, sentinel_ptr);
2291122912 };
......@@ -24072,6 +24073,7 @@ fn elemPtr(
2407224073 elem_index: Air.Inst.Ref,
2407324074 elem_index_src: LazySrcLoc,
2407424075 init: bool,
24076 size: std.builtin.Type.Pointer.Size,
2407524077) CompileError!Air.Inst.Ref {
2407624078 const indexable_ptr_src = src; // TODO better source location
2407724079 const indexable_ptr_ty = sema.typeOf(indexable_ptr);
......@@ -24098,13 +24100,12 @@ fn elemPtr(
2409824100 const index_val = maybe_index_val orelse break :rs elem_index_src;
2409924101 const index = @intCast(usize, index_val.toUnsignedInt(target));
2410024102 const elem_ptr = try ptr_val.elemPtr(indexable_ty, sema.arena, index, sema.mod);
24101 const result_ty = try sema.elemPtrType(indexable_ty, index);
24102 return sema.addConstant(result_ty, elem_ptr);
24103 const elem_ptr_ty = try sema.elemPtrType(indexable_ty, index, size);
24104 return sema.addConstant(elem_ptr_ty, elem_ptr);
2410324105 };
24104 const result_ty = try sema.elemPtrType(indexable_ty, null);
24105
24106 const elem_ptr_ty = try sema.elemPtrType(indexable_ty, null, size);
2410624107 try sema.requireRuntimeBlock(block, src, runtime_src);
24107 return block.addPtrElemPtr(indexable, elem_index, result_ty);
24108 return block.addPtrElemPtr(indexable, elem_index, elem_ptr_ty);
2410824109 },
2410924110 .One => {
2411024111 assert(indexable_ty.childType().zigTypeTag() == .Array); // Guaranteed by isIndexable
......@@ -24166,7 +24167,7 @@ fn elemVal(
2416624167 },
2416724168 .One => {
2416824169 assert(indexable_ty.childType().zigTypeTag() == .Array); // Guaranteed by isIndexable
24169 const elem_ptr = try sema.elemPtr(block, indexable_src, indexable, elem_index, elem_index_src, false);
24170 const elem_ptr = try sema.elemPtr(block, indexable_src, indexable, elem_index, elem_index_src, false, .One);
2417024171 return sema.analyzeLoad(block, indexable_src, elem_ptr, elem_index_src);
2417124172 },
2417224173 },
......@@ -24404,7 +24405,7 @@ fn elemPtrArray(
2440424405 break :o index;
2440524406 } else null;
2440624407
24407 const elem_ptr_ty = try sema.elemPtrType(array_ptr_ty, offset);
24408 const elem_ptr_ty = try sema.elemPtrType(array_ptr_ty, offset, .One);
2440824409
2440924410 if (maybe_undef_array_ptr_val) |array_ptr_val| {
2441024411 if (array_ptr_val.isUndef()) {
......@@ -24509,7 +24510,7 @@ fn elemPtrSlice(
2450924510 break :o index;
2451024511 } else null;
2451124512
24512 const elem_ptr_ty = try sema.elemPtrType(slice_ty, offset);
24513 const elem_ptr_ty = try sema.elemPtrType(slice_ty, offset, .One);
2451324514
2451424515 if (maybe_undef_slice_val) |slice_val| {
2451524516 if (slice_val.isUndef()) {
......@@ -26239,7 +26240,7 @@ fn storePtr2(
2623926240 const elem_src = operand_src; // TODO better source location
2624026241 const elem = try sema.tupleField(block, operand_src, uncasted_operand, elem_src, i);
2624126242 const elem_index = try sema.addIntUnsigned(Type.usize, i);
26242 const elem_ptr = try sema.elemPtr(block, ptr_src, ptr, elem_index, elem_src, false);
26243 const elem_ptr = try sema.elemPtr(block, ptr_src, ptr, elem_index, elem_src, false, .One);
2624326244 try sema.storePtr2(block, src, elem_ptr, elem_src, elem, elem_src, .store);
2624426245 }
2624526246 return;
......@@ -33276,7 +33277,12 @@ fn compareVector(
3327633277/// For []T, returns *T
3327733278/// Handles const-ness and address spaces in particular.
3327833279/// This code is duplicated in `analyzePtrArithmetic`.
33279fn elemPtrType(sema: *Sema, ptr_ty: Type, offset: ?usize) !Type {
33280fn elemPtrType(
33281 sema: *Sema,
33282 ptr_ty: Type,
33283 offset: ?usize,
33284 size: std.builtin.Type.Pointer.Size,
33285) !Type {
3328033286 const ptr_info = ptr_ty.ptrInfo().data;
3328133287 const elem_ty = ptr_ty.elemType2();
3328233288 const allow_zero = ptr_info.@"allowzero" and (offset orelse 0) == 0;
......@@ -33321,6 +33327,7 @@ fn elemPtrType(sema: *Sema, ptr_ty: Type, offset: ?usize) !Type {
3332133327 break :a new_align;
3332233328 };
3332333329 return try Type.ptr(sema.arena, sema.mod, .{
33330 .size = size,
3332433331 .pointee_type = elem_ty,
3332533332 .mutable = ptr_info.mutable,
3332633333 .@"addrspace" = ptr_info.@"addrspace",
src/Zir.zig+10-1
......@@ -79,6 +79,7 @@ pub fn extraData(code: Zir, comptime T: type, index: usize) struct { data: T, en
7979 Inst.BuiltinCall.Flags => @bitCast(Inst.BuiltinCall.Flags, code.extra[i]),
8080 Inst.SwitchBlock.Bits => @bitCast(Inst.SwitchBlock.Bits, code.extra[i]),
8181 Inst.FuncFancy.Bits => @bitCast(Inst.FuncFancy.Bits, code.extra[i]),
82 Inst.ElemPtrImm.Bits => @bitCast(Inst.ElemPtrImm.Bits, code.extra[i]),
8283 else => @compileError("bad field type"),
8384 };
8485 i += 1;
......@@ -388,6 +389,8 @@ pub const Inst = struct {
388389 /// as a reference to another ZIR instruction.
389390 /// Uses the `pl_node` union field. AST node is an element inside array initialization
390391 /// syntax. Payload is `ElemPtrImm`.
392 /// This instruction has a way to set the result type to be a
393 /// single-pointer or a many-pointer.
391394 elem_ptr_imm,
392395 /// Given an array, slice, or pointer, returns the element at the provided index.
393396 /// Uses the `pl_node` union field. AST node is a[b] syntax. Payload is `Bin`.
......@@ -2972,7 +2975,13 @@ pub const Inst = struct {
29722975
29732976 pub const ElemPtrImm = struct {
29742977 ptr: Ref,
2975 index: u32,
2978 bits: Bits,
2979
2980 pub const Bits = packed struct(u32) {
2981 index: u31,
2982 /// Controls whether the type returned is `*T` or `[*]T`.
2983 manyptr: bool = false,
2984 };
29762985 };
29772986
29782987 /// 0. multi_cases_len: u32 // If has_multi_cases is set.
src/print_zir.zig+3-1
......@@ -888,7 +888,9 @@ const Writer = struct {
888888 const extra = self.code.extraData(Zir.Inst.ElemPtrImm, inst_data.payload_index).data;
889889
890890 try self.writeInstRef(stream, extra.ptr);
891 try stream.print(", {d}) ", .{extra.index});
891 try stream.print(", {d}", .{extra.bits.index});
892 try self.writeFlag(stream, ", manyptr", extra.bits.manyptr);
893 try stream.writeAll(") ");
892894 try self.writeSrc(stream, inst_data.src());
893895 }
894896