authorgravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2023-04-30 12:25:13+10:00
committergravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2023-05-07 15:55:20+10:00
loga62b5d84d834757682be9b5a0606ff8168c1d594
treee72d2fcb10666ca1225e60dce251a414b1fd82d4
parentd71a43ec2c28a53a3e1d9bcb538707eca00a6fc0

zir: add slice_length tag


5 files changed, 39 insertions(+), 0 deletions(-)

src/AstGen.zig+1
......@@ -2557,6 +2557,7 @@ fn addEnsureResult(gz: *GenZir, maybe_unused_result: Zir.Inst.Ref, statement: As
25572557 .slice_start,
25582558 .slice_end,
25592559 .slice_sentinel,
2560 .slice_length,
25602561 .import,
25612562 .switch_block,
25622563 .switch_cond,
src/Autodoc.zig+1
......@@ -1287,6 +1287,7 @@ fn walkInstruction(
12871287 .expr = .{ .sliceIndex = slice_index },
12881288 };
12891289 },
1290 .slice_length => @panic("TODO: implement walkInstruction for .slice_length"),
12901291
12911292 // @check array_cat and array_mul
12921293 .add,
src/Sema.zig+10
......@@ -985,6 +985,7 @@ fn analyzeBodyInner(
985985 .slice_end => try sema.zirSliceEnd(block, inst),
986986 .slice_sentinel => try sema.zirSliceSentinel(block, inst),
987987 .slice_start => try sema.zirSliceStart(block, inst),
988 .slice_length => try sema.zirSliceLength(block, inst),
988989 .str => try sema.zirStr(block, inst),
989990 .switch_block => try sema.zirSwitchBlock(block, inst),
990991 .switch_cond => try sema.zirSwitchCond(block, inst, false),
......@@ -9961,6 +9962,15 @@ fn zirSliceSentinel(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr
99619962 return sema.analyzeSlice(block, src, array_ptr, start, end, sentinel, sentinel_src, ptr_src, start_src, end_src);
99629963}
99639964
9965fn zirSliceLength(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
9966 const tracy = trace(@src());
9967 defer tracy.end();
9968
9969 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
9970 const src = inst_data.src();
9971 return sema.fail(block, src, "TODO: implement .slice_length", .{});
9972}
9973
99649974fn zirSwitchCapture(
99659975 sema: *Sema,
99669976 block: *Block,
src/Zir.zig+14
......@@ -570,6 +570,10 @@ pub const Inst = struct {
570570 /// Returns a pointer to the subslice.
571571 /// Uses the `pl_node` field. AST node is the slice syntax. Payload is `SliceSentinel`.
572572 slice_sentinel,
573 /// Slice operation `array_ptr[start..][0..len]`. No sentinel.
574 /// Returns a pointer to the subslice.
575 /// Uses the `pl_node` field. AST node is the slice syntax. Payload is `SliceLength`.
576 slice_length,
573577 /// Write a value to a pointer. For loading, see `load`.
574578 /// Source location is assumed to be same as previous instruction.
575579 /// Uses the `bin` union field.
......@@ -1135,6 +1139,7 @@ pub const Inst = struct {
11351139 .slice_start,
11361140 .slice_end,
11371141 .slice_sentinel,
1142 .slice_length,
11381143 .import,
11391144 .typeof_log2_int_type,
11401145 .resolve_inferred_alloc,
......@@ -1430,6 +1435,7 @@ pub const Inst = struct {
14301435 .slice_start,
14311436 .slice_end,
14321437 .slice_sentinel,
1438 .slice_length,
14331439 .import,
14341440 .typeof_log2_int_type,
14351441 .switch_capture,
......@@ -1667,6 +1673,7 @@ pub const Inst = struct {
16671673 .slice_start = .pl_node,
16681674 .slice_end = .pl_node,
16691675 .slice_sentinel = .pl_node,
1676 .slice_length = .pl_node,
16701677 .store = .bin,
16711678 .store_node = .pl_node,
16721679 .store_to_block_ptr = .bin,
......@@ -2980,6 +2987,13 @@ pub const Inst = struct {
29802987 sentinel: Ref,
29812988 };
29822989
2990 pub const SliceLength = struct {
2991 lhs: Ref,
2992 start: Ref,
2993 len: Ref,
2994 start_src_node_offset: i32,
2995 };
2996
29832997 /// The meaning of these operands depends on the corresponding `Tag`.
29842998 pub const Bin = struct {
29852999 lhs: Ref,
src/print_zir.zig+13
......@@ -267,6 +267,7 @@ const Writer = struct {
267267 .slice_start => try self.writeSliceStart(stream, inst),
268268 .slice_end => try self.writeSliceEnd(stream, inst),
269269 .slice_sentinel => try self.writeSliceSentinel(stream, inst),
270 .slice_length => try self.writeSliceLength(stream, inst),
270271
271272 .union_init => try self.writeUnionInit(stream, inst),
272273
......@@ -756,6 +757,18 @@ const Writer = struct {
756757 try self.writeSrc(stream, inst_data.src());
757758 }
758759
760 fn writeSliceLength(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
761 const inst_data = self.code.instructions.items(.data)[inst].pl_node;
762 const extra = self.code.extraData(Zir.Inst.SliceLength, inst_data.payload_index).data;
763 try self.writeInstRef(stream, extra.lhs);
764 try stream.writeAll(", ");
765 try self.writeInstRef(stream, extra.start);
766 try stream.writeAll(", ");
767 try self.writeInstRef(stream, extra.len);
768 try stream.writeAll(") ");
769 try self.writeSrc(stream, inst_data.src());
770 }
771
759772 fn writeUnionInit(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
760773 const inst_data = self.code.instructions.items(.data)[inst].pl_node;
761774 const extra = self.code.extraData(Zir.Inst.UnionInit, inst_data.payload_index).data;