authorgravatar for topolarity@tapscott.meCody Tapscott <topolarity@tapscott.me> 2022-03-02 21:52:49-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-03 13:08:14-07:00
logb6a6f05c0d139ebb572bbc6672d8ecfe5e63d89f
tree323110982fdd6b4a1a76a9388eb2bf14b4d5560a
parentb6f1a8612bc5120b729f94031119315334e54e35

stage2: support @ptrCast for slices with an offset


3 files changed, 27 insertions(+), 15 deletions(-)

src/Sema.zig+17-10
......@@ -15363,7 +15363,7 @@ fn unionFieldVal(
1536315363 return sema.addConstant(field.ty, tag_and_val.val);
1536415364 } else {
1536515365 const old_ty = union_ty.unionFieldType(tag_and_val.tag);
15366 const new_val = try sema.bitCastVal(block, src, tag_and_val.val, old_ty, field.ty);
15366 const new_val = try sema.bitCastVal(block, src, tag_and_val.val, old_ty, field.ty, 0);
1536715367 return sema.addConstant(field.ty, new_val);
1536815368 }
1536915369 },
......@@ -16470,7 +16470,7 @@ fn storePtrVal(
1647016470 var kit = try beginComptimePtrMutation(sema, block, src, ptr_val);
1647116471 try sema.checkComptimeVarStore(block, src, kit.decl_ref_mut);
1647216472
16473 const bitcasted_val = try sema.bitCastVal(block, src, operand_val, operand_ty, kit.ty);
16473 const bitcasted_val = try sema.bitCastVal(block, src, operand_val, operand_ty, kit.ty, 0);
1647416474
1647516475 const arena = kit.beginArena(sema.gpa);
1647616476 defer kit.finishArena();
......@@ -16764,6 +16764,8 @@ fn beginComptimePtrMutation(
1676416764const ComptimePtrLoadKit = struct {
1676516765 /// The Value of the Decl that owns this memory.
1676616766 root_val: Value,
16767 /// The Type of the Decl that owns this memory.
16768 root_ty: Type,
1676716769 /// Parent Value.
1676816770 val: Value,
1676916771 /// The Type of the parent Value.
......@@ -16794,6 +16796,7 @@ fn beginComptimePtrLoad(
1679416796 if (decl_val.tag() == .variable) return error.RuntimeLoad;
1679516797 return ComptimePtrLoadKit{
1679616798 .root_val = decl_val,
16799 .root_ty = decl.ty,
1679716800 .val = decl_val,
1679816801 .ty = decl.ty,
1679916802 .byte_offset = 0,
......@@ -16806,6 +16809,7 @@ fn beginComptimePtrLoad(
1680616809 if (decl_val.tag() == .variable) return error.RuntimeLoad;
1680716810 return ComptimePtrLoadKit{
1680816811 .root_val = decl_val,
16812 .root_ty = decl.ty,
1680916813 .val = decl_val,
1681016814 .ty = decl.ty,
1681116815 .byte_offset = 0,
......@@ -16840,6 +16844,7 @@ fn beginComptimePtrLoad(
1684016844 };
1684116845 return ComptimePtrLoadKit{
1684216846 .root_val = parent.root_val,
16847 .root_ty = parent.ty,
1684316848 .val = try parent.val.elemValue(sema.arena, elem_ptr.index),
1684416849 .ty = elem_ty,
1684516850 .byte_offset = byte_offset,
......@@ -16855,6 +16860,7 @@ fn beginComptimePtrLoad(
1685516860 }
1685616861 return ComptimePtrLoadKit{
1685716862 .root_val = parent.root_val,
16863 .root_ty = parent.ty,
1685816864 .val = parent.val,
1685916865 .ty = parent.ty,
1686016866 .byte_offset = parent.byte_offset,
......@@ -16882,6 +16888,7 @@ fn beginComptimePtrLoad(
1688216888 };
1688316889 return ComptimePtrLoadKit{
1688416890 .root_val = parent.root_val,
16891 .root_ty = parent.ty,
1688516892 .val = try parent.val.fieldValue(sema.arena, field_index),
1688616893 .ty = parent.ty.structFieldType(field_index),
1688716894 .byte_offset = byte_offset,
......@@ -16893,6 +16900,7 @@ fn beginComptimePtrLoad(
1689316900 const parent = try beginComptimePtrLoad(sema, block, src, err_union_ptr);
1689416901 return ComptimePtrLoadKit{
1689516902 .root_val = parent.root_val,
16903 .root_ty = parent.root_ty,
1689616904 .val = parent.val.castTag(.eu_payload).?.data,
1689716905 .ty = parent.ty.errorUnionPayload(),
1689816906 .byte_offset = null,
......@@ -16904,6 +16912,7 @@ fn beginComptimePtrLoad(
1690416912 const parent = try beginComptimePtrLoad(sema, block, src, opt_ptr);
1690516913 return ComptimePtrLoadKit{
1690616914 .root_val = parent.root_val,
16915 .root_ty = parent.root_ty,
1690716916 .val = parent.val.castTag(.opt_payload).?.data,
1690816917 .ty = try parent.ty.optionalChildAlloc(sema.arena),
1690916918 .byte_offset = null,
......@@ -16941,7 +16950,7 @@ fn bitCast(
1694116950
1694216951 // TODO validate the type size and other compile errors
1694316952 if (try sema.resolveMaybeUndefVal(block, inst_src, inst)) |val| {
16944 const result_val = try sema.bitCastVal(block, inst_src, val, old_ty, dest_ty);
16953 const result_val = try sema.bitCastVal(block, inst_src, val, old_ty, dest_ty, 0);
1694516954 return sema.addConstant(dest_ty, result_val);
1694616955 }
1694716956 try sema.requireRuntimeBlock(block, inst_src);
......@@ -16955,6 +16964,7 @@ pub fn bitCastVal(
1695516964 val: Value,
1695616965 old_ty: Type,
1695716966 new_ty: Type,
16967 buffer_offset: usize,
1695816968) !Value {
1695916969 if (old_ty.eql(new_ty)) return val;
1696016970
......@@ -16965,7 +16975,7 @@ pub fn bitCastVal(
1696516975 const buffer = try sema.gpa.alloc(u8, abi_size);
1696616976 defer sema.gpa.free(buffer);
1696716977 val.writeToMemory(old_ty, target, buffer);
16968 return Value.readFromMemory(new_ty, target, buffer, sema.arena);
16978 return Value.readFromMemory(new_ty, target, buffer[buffer_offset..], sema.arena);
1696916979}
1697016980
1697116981fn coerceArrayPtrToSlice(
......@@ -19831,13 +19841,10 @@ fn pointerDeref(sema: *Sema, block: *Block, src: LazySrcLoc, ptr_val: Value, ptr
1983119841 // The Type it is stored as in the compiler has an ABI size greater or equal to
1983219842 // the ABI size of `load_ty`. We may perform the bitcast based on
1983319843 // `parent.val` alone (more efficient).
19834 return try sema.bitCastVal(block, src, parent.val, parent.ty, load_ty);
19844 return try sema.bitCastVal(block, src, parent.val, parent.ty, load_ty, 0);
19845 } else {
19846 return try sema.bitCastVal(block, src, parent.root_val, parent.root_ty, load_ty, parent.byte_offset.?);
1983519847 }
19836
19837 // The Type it is stored as in the compiler has an ABI size less than the ABI size
19838 // of `load_ty`. The bitcast must be performed based on the `parent.root_val`
19839 // and reinterpreted starting at `parent.byte_offset`.
19840 return sema.fail(block, src, "TODO: implement bitcast with index offset", .{});
1984119848}
1984219849
1984319850/// Used to convert a u64 value to a usize value, emitting a compile error if the number
src/type.zig+4-2
......@@ -5287,8 +5287,10 @@ pub const Type = extern union {
52875287 // type, we change it to 0 here. If this causes an assertion trip because the
52885288 // pointee type needs to be resolved more, that needs to be done before calling
52895289 // this ptr() function.
5290 if (d.@"align" != 0 and d.@"align" == d.pointee_type.abiAlignment(target)) {
5291 d.@"align" = 0;
5290 if (d.@"align" != 0) {
5291 if (d.pointee_type.zigTypeTag() == .Opaque or d.@"align" == d.pointee_type.abiAlignment(target)) {
5292 d.@"align" = 0;
5293 }
52925294 }
52935295
52945296 // Canonicalize host_size. If it matches the bit size of the pointee type,
test/behavior/ptrcast.zig+6-3
......@@ -4,7 +4,9 @@ const expect = std.testing.expect;
44const native_endian = builtin.target.cpu.arch.endian();
55
66test "reinterpret bytes as integer with nonzero offset" {
7 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
7 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
8 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
9 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
810
911 try testReinterpretBytesAsInteger();
1012 comptime try testReinterpretBytesAsInteger();
......@@ -43,7 +45,6 @@ fn testReinterpretBytesAsExternStruct() !void {
4345test "reinterpret struct field at comptime" {
4446 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
4547 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
46 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
4748 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
4849 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
4950
......@@ -75,7 +76,9 @@ test "comptime ptrcast keeps larger alignment" {
7576}
7677
7778test "implicit optional pointer to optional anyopaque pointer" {
78 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
79 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
80 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
81 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
7982
8083 var buf: [4]u8 = "aoeu".*;
8184 var x: ?[*]u8 = &buf;