From 0942bf73c90eabf87d0ca965b50beb0fd9a8fc8c Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Sun, 24 Oct 2021 16:34:42 +0200 Subject: stage2: improve slicing * Allow slicing many- and c-pointers. * Allow comptime pointer arithmetic on undefined values. * Return the correct type for slicing of slices. --- src/Sema.zig | 47 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 10 deletions(-) (limited to 'src/Sema.zig') diff --git a/src/Sema.zig b/src/Sema.zig index a87e0739e8..d290ea8ec0 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -7989,11 +7989,16 @@ fn analyzePtrArithmetic( const offset = try sema.coerce(block, Type.usize, uncasted_offset, offset_src); // TODO adjust the return type according to alignment and other factors const runtime_src = rs: { - if (try sema.resolveDefinedValue(block, ptr_src, ptr)) |ptr_val| { - if (try sema.resolveDefinedValue(block, offset_src, offset)) |offset_val| { + if (try sema.resolveMaybeUndefVal(block, ptr_src, ptr)) |ptr_val| { + if (try sema.resolveMaybeUndefVal(block, offset_src, offset)) |offset_val| { const ptr_ty = sema.typeOf(ptr); - const offset_int = offset_val.toUnsignedInt(); const new_ptr_ty = ptr_ty; // TODO modify alignment + + if (ptr_val.isUndef() or offset_val.isUndef()) { + return sema.addConstUndef(new_ptr_ty); + } + + const offset_int = offset_val.toUnsignedInt(); if (ptr_val.getUnsignedInt()) |addr| { const target = sema.mod.getTarget(); const elem_ty = ptr_ty.childType(); @@ -13206,8 +13211,8 @@ fn analyzeSlice( var elem_ty = ptr_ptr_child_ty.childType(); switch (ptr_ptr_child_ty.zigTypeTag()) { .Array => {}, - .Pointer => { - if (ptr_ptr_child_ty.isSinglePointer()) { + .Pointer => switch (ptr_ptr_child_ty.ptrSize()) { + .One => { const double_child_ty = ptr_ptr_child_ty.childType(); if (double_child_ty.zigTypeTag() == .Array) { ptr_or_slice = try sema.analyzeLoad(block, src, ptr_ptr, ptr_src); @@ -13217,10 +13222,23 @@ fn analyzeSlice( } else { return sema.fail(block, ptr_src, "slice of single-item pointer", .{}); } - } + }, + .Many, .C => { + ptr_or_slice = try sema.analyzeLoad(block, src, ptr_ptr, ptr_src); + slice_ty = ptr_ptr_child_ty; + array_ty = ptr_ptr_child_ty; + elem_ty = ptr_ptr_child_ty.childType(); + }, + .Slice => { + ptr_or_slice = try sema.analyzeLoad(block, src, ptr_ptr, ptr_src); + slice_ty = ptr_ptr_child_ty; + array_ty = ptr_ptr_child_ty; + elem_ty = ptr_ptr_child_ty.childType(); + }, }, else => return sema.fail(block, ptr_src, "slice of non-array type '{}'", .{ptr_ptr_child_ty}), } + const ptr = if (slice_ty.isSlice()) try sema.analyzeSlicePtr(block, src, ptr_or_slice, slice_ty, ptr_src) else @@ -13252,7 +13270,6 @@ fn analyzeSlice( const new_len = try sema.analyzeArithmetic(block, .sub, end, start, src, end_src, start_src); - const opt_new_ptr_val = try sema.resolveDefinedValue(block, ptr_src, new_ptr); const opt_new_len_val = try sema.resolveDefinedValue(block, src, new_len); const new_ptr_ty_info = sema.typeOf(new_ptr).ptrInfo().data; @@ -13276,11 +13293,21 @@ fn analyzeSlice( .size = .One, }); - if (opt_new_ptr_val) |new_ptr_val| { - return sema.addConstant(return_ty, new_ptr_val); - } else { + const opt_new_ptr_val = try sema.resolveMaybeUndefVal(block, ptr_src, new_ptr); + const new_ptr_val = opt_new_ptr_val orelse { return block.addBitCast(return_ty, new_ptr); + }; + + if (!new_ptr_val.isUndef()) { + return sema.addConstant(return_ty, new_ptr_val); } + + // Special case: @as([]i32, undefined)[x..x] + if (new_len_int == 0) { + return sema.addConstUndef(return_ty); + } + + return sema.fail(block, ptr_src, "non-zero length slice of undefined pointer", .{}); } const return_ty = try Type.ptr(sema.arena, .{ -- cgit v1.2.3 From 7062c8a8865bbd2fb8181b579da552295cd68e6a Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Sun, 24 Oct 2021 20:37:09 +0200 Subject: stage2: comptime slice of pointer to hardcoded address --- src/Sema.zig | 7 ++++++- test/behavior.zig | 1 + test/behavior/slice_stage2.zig | 12 ++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 test/behavior/slice_stage2.zig (limited to 'src/Sema.zig') diff --git a/src/Sema.zig b/src/Sema.zig index d290ea8ec0..eaa99a42a7 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -8001,7 +8001,12 @@ fn analyzePtrArithmetic( const offset_int = offset_val.toUnsignedInt(); if (ptr_val.getUnsignedInt()) |addr| { const target = sema.mod.getTarget(); - const elem_ty = ptr_ty.childType(); + const ptr_child_ty = ptr_ty.childType(); + const elem_ty = if (ptr_ty.isSinglePointer() and ptr_child_ty.zigTypeTag() == .Array) + ptr_child_ty.childType() + else + ptr_child_ty; + const elem_size = elem_ty.abiSize(target); const new_addr = switch (air_tag) { .ptr_add => addr + elem_size * offset_int, diff --git a/test/behavior.zig b/test/behavior.zig index 05e05d51fc..42e3e7f07d 100644 --- a/test/behavior.zig +++ b/test/behavior.zig @@ -65,6 +65,7 @@ test { if (builtin.zig_is_stage2) { // When all comptime_memory.zig tests pass, #9646 can be closed. // _ = @import("behavior/comptime_memory.zig"); + _ = @import("behavior/slice_stage2.zig"); } else { _ = @import("behavior/align_stage1.zig"); _ = @import("behavior/alignof.zig"); diff --git a/test/behavior/slice_stage2.zig b/test/behavior/slice_stage2.zig new file mode 100644 index 0000000000..360527e8ba --- /dev/null +++ b/test/behavior/slice_stage2.zig @@ -0,0 +1,12 @@ +const std = @import("std"); +const expect = std.testing.expect; + +const x = @intToPtr([*]i32, 0x1000)[0..0x500]; +const y = x[0x100..]; +test "compile time slice of pointer to hard coded address" { + try expect(@ptrToInt(x) == 0x1000); + try expect(x.len == 0x500); + + try expect(@ptrToInt(y) == 0x1400); + try expect(y.len == 0x400); +} -- cgit v1.2.3