From a62b5d84d834757682be9b5a0606ff8168c1d594 Mon Sep 17 00:00:00 2001 From: dweiller <4678790+dweiller@users.noreplay.github.com> Date: Sun, 30 Apr 2023 12:25:13 +1000 Subject: [PATCH] zir: add slice_length tag --- src/AstGen.zig | 1 + src/Autodoc.zig | 1 + src/Sema.zig | 10 ++++++++++ src/Zir.zig | 14 ++++++++++++++ src/print_zir.zig | 13 +++++++++++++ 5 files changed, 39 insertions(+) diff --git a/src/AstGen.zig b/src/AstGen.zig index 749e3d28c4..0233a9819d 100644 --- a/src/AstGen.zig +++ b/src/AstGen.zig @@ -2557,6 +2557,7 @@ fn addEnsureResult(gz: *GenZir, maybe_unused_result: Zir.Inst.Ref, statement: As .slice_start, .slice_end, .slice_sentinel, + .slice_length, .import, .switch_block, .switch_cond, diff --git a/src/Autodoc.zig b/src/Autodoc.zig index 42813f7614..db24f747f7 100644 --- a/src/Autodoc.zig +++ b/src/Autodoc.zig @@ -1287,6 +1287,7 @@ fn walkInstruction( .expr = .{ .sliceIndex = slice_index }, }; }, + .slice_length => @panic("TODO: implement walkInstruction for .slice_length"), // @check array_cat and array_mul .add, diff --git a/src/Sema.zig b/src/Sema.zig index b2387dfe12..d225a8f7b3 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -985,6 +985,7 @@ fn analyzeBodyInner( .slice_end => try sema.zirSliceEnd(block, inst), .slice_sentinel => try sema.zirSliceSentinel(block, inst), .slice_start => try sema.zirSliceStart(block, inst), + .slice_length => try sema.zirSliceLength(block, inst), .str => try sema.zirStr(block, inst), .switch_block => try sema.zirSwitchBlock(block, inst), .switch_cond => try sema.zirSwitchCond(block, inst, false), @@ -9961,6 +9962,15 @@ fn zirSliceSentinel(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr return sema.analyzeSlice(block, src, array_ptr, start, end, sentinel, sentinel_src, ptr_src, start_src, end_src); } +fn zirSliceLength(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { + const tracy = trace(@src()); + defer tracy.end(); + + const inst_data = sema.code.instructions.items(.data)[inst].pl_node; + const src = inst_data.src(); + return sema.fail(block, src, "TODO: implement .slice_length", .{}); +} + fn zirSwitchCapture( sema: *Sema, block: *Block, diff --git a/src/Zir.zig b/src/Zir.zig index 7f9681e533..27ea02e642 100644 --- a/src/Zir.zig +++ b/src/Zir.zig @@ -570,6 +570,10 @@ pub const Inst = struct { /// Returns a pointer to the subslice. /// Uses the `pl_node` field. AST node is the slice syntax. Payload is `SliceSentinel`. slice_sentinel, + /// Slice operation `array_ptr[start..][0..len]`. No sentinel. + /// Returns a pointer to the subslice. + /// Uses the `pl_node` field. AST node is the slice syntax. Payload is `SliceLength`. + slice_length, /// Write a value to a pointer. For loading, see `load`. /// Source location is assumed to be same as previous instruction. /// Uses the `bin` union field. @@ -1135,6 +1139,7 @@ pub const Inst = struct { .slice_start, .slice_end, .slice_sentinel, + .slice_length, .import, .typeof_log2_int_type, .resolve_inferred_alloc, @@ -1430,6 +1435,7 @@ pub const Inst = struct { .slice_start, .slice_end, .slice_sentinel, + .slice_length, .import, .typeof_log2_int_type, .switch_capture, @@ -1667,6 +1673,7 @@ pub const Inst = struct { .slice_start = .pl_node, .slice_end = .pl_node, .slice_sentinel = .pl_node, + .slice_length = .pl_node, .store = .bin, .store_node = .pl_node, .store_to_block_ptr = .bin, @@ -2980,6 +2987,13 @@ pub const Inst = struct { sentinel: Ref, }; + pub const SliceLength = struct { + lhs: Ref, + start: Ref, + len: Ref, + start_src_node_offset: i32, + }; + /// The meaning of these operands depends on the corresponding `Tag`. pub const Bin = struct { lhs: Ref, diff --git a/src/print_zir.zig b/src/print_zir.zig index f228adc7ea..6d1a716d3c 100644 --- a/src/print_zir.zig +++ b/src/print_zir.zig @@ -267,6 +267,7 @@ const Writer = struct { .slice_start => try self.writeSliceStart(stream, inst), .slice_end => try self.writeSliceEnd(stream, inst), .slice_sentinel => try self.writeSliceSentinel(stream, inst), + .slice_length => try self.writeSliceLength(stream, inst), .union_init => try self.writeUnionInit(stream, inst), @@ -756,6 +757,18 @@ const Writer = struct { try self.writeSrc(stream, inst_data.src()); } + fn writeSliceLength(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void { + const inst_data = self.code.instructions.items(.data)[inst].pl_node; + const extra = self.code.extraData(Zir.Inst.SliceLength, inst_data.payload_index).data; + try self.writeInstRef(stream, extra.lhs); + try stream.writeAll(", "); + try self.writeInstRef(stream, extra.start); + try stream.writeAll(", "); + try self.writeInstRef(stream, extra.len); + try stream.writeAll(") "); + try self.writeSrc(stream, inst_data.src()); + } + fn writeUnionInit(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void { const inst_data = self.code.instructions.items(.data)[inst].pl_node; const extra = self.code.extraData(Zir.Inst.UnionInit, inst_data.payload_index).data;