authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-09-16 22:04:06+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-09-16 22:04:06+01:00
log7caa3d9da71c38665340247a1c2bf9bedb8db925
treedcb7637f26db5363d0bdc3c60fd05660f8a371fd
parentf3445f8f6935b4532aab3f339f5d86319d2dca72
parent7f60d2e4658ad78839ce0fce63a95dbcb893a256
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #21425 from mlugg/pointer-arith-inplace-res-ty

compiler: provide correct result types to `+=` and `-=`

6 files changed, 90 insertions(+), 9 deletions(-)

lib/std/zig/AstGen.zig+20-2
......@@ -3785,8 +3785,26 @@ fn assignOp(
37853785 else => undefined,
37863786 };
37873787 const lhs = try gz.addUnNode(.load, lhs_ptr, infix_node);
3788 const lhs_type = try gz.addUnNode(.typeof, lhs, infix_node);
3789 const rhs = try expr(gz, scope, .{ .rl = .{ .coerced_ty = lhs_type } }, node_datas[infix_node].rhs);
3788
3789 const rhs_res_ty = switch (op_inst_tag) {
3790 .add,
3791 .sub,
3792 => try gz.add(.{
3793 .tag = .extended,
3794 .data = .{ .extended = .{
3795 .opcode = .inplace_arith_result_ty,
3796 .small = @intFromEnum(@as(Zir.Inst.InplaceOp, switch (op_inst_tag) {
3797 .add => .add_eq,
3798 .sub => .sub_eq,
3799 else => unreachable,
3800 })),
3801 .operand = @intFromEnum(lhs),
3802 } },
3803 }),
3804 else => try gz.addUnNode(.typeof, lhs, infix_node), // same as LHS type
3805 };
3806 // Not `coerced_ty` since `add`/etc won't coerce to this type.
3807 const rhs = try expr(gz, scope, .{ .rl = .{ .ty = rhs_res_ty } }, node_datas[infix_node].rhs);
37903808
37913809 switch (op_inst_tag) {
37923810 .add, .sub, .mul, .div, .mod_rem => {
lib/std/zig/Zir.zig+10
......@@ -2086,6 +2086,10 @@ pub const Inst = struct {
20862086 /// `operand` is payload index to `UnNode`.
20872087 /// `small` is unused.
20882088 branch_hint,
2089 /// Compute the result type for in-place arithmetic, e.g. `+=`.
2090 /// `operand` is `Zir.Inst.Ref` of the loaded LHS (*not* its type).
2091 /// `small` is an `Inst.InplaceOp`.
2092 inplace_arith_result_ty,
20892093
20902094 pub const InstData = struct {
20912095 opcode: Extended,
......@@ -3188,6 +3192,11 @@ pub const Inst = struct {
31883192 calling_convention_inline,
31893193 };
31903194
3195 pub const InplaceOp = enum(u16) {
3196 add_eq,
3197 sub_eq,
3198 };
3199
31913200 /// Trailing:
31923201 /// 0. tag_type: Ref, // if has_tag_type
31933202 /// 1. captures_len: u32, // if has_captures_len
......@@ -4032,6 +4041,7 @@ fn findDeclsInner(
40324041 .field_parent_ptr,
40334042 .builtin_value,
40344043 .branch_hint,
4044 .inplace_arith_result_ty,
40354045 => return,
40364046
40374047 // `@TypeOf` has a body.
src/Sema.zig+28
......@@ -1361,6 +1361,7 @@ fn analyzeBodyInner(
13611361 .value_placeholder => unreachable, // never appears in a body
13621362 .field_parent_ptr => try sema.zirFieldParentPtr(block, extended),
13631363 .builtin_value => try sema.zirBuiltinValue(extended),
1364 .inplace_arith_result_ty => try sema.zirInplaceArithResultTy(extended),
13641365 };
13651366 },
13661367
......@@ -27342,6 +27343,33 @@ fn zirBuiltinValue(sema: *Sema, extended: Zir.Inst.Extended.InstData) CompileErr
2734227343 return Air.internedToRef(ty.toIntern());
2734327344}
2734427345
27346fn zirInplaceArithResultTy(sema: *Sema, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref {
27347 const pt = sema.pt;
27348 const zcu = pt.zcu;
27349
27350 const lhs = try sema.resolveInst(@enumFromInt(extended.operand));
27351 const lhs_ty = sema.typeOf(lhs);
27352
27353 const op: Zir.Inst.InplaceOp = @enumFromInt(extended.small);
27354 const ty: Type = switch (op) {
27355 .add_eq => ty: {
27356 const ptr_size = lhs_ty.ptrSizeOrNull(zcu) orelse break :ty lhs_ty;
27357 switch (ptr_size) {
27358 .One, .Slice => break :ty lhs_ty, // invalid, let it error
27359 .Many, .C => break :ty .usize, // `[*]T + usize`
27360 }
27361 },
27362 .sub_eq => ty: {
27363 const ptr_size = lhs_ty.ptrSizeOrNull(zcu) orelse break :ty lhs_ty;
27364 switch (ptr_size) {
27365 .One, .Slice => break :ty lhs_ty, // invalid, let it error
27366 .Many, .C => break :ty .generic_poison, // could be `[*]T - [*]T` or `[*]T - usize`
27367 }
27368 },
27369 };
27370 return Air.internedToRef(ty.toIntern());
27371}
27372
2734527373fn zirBranchHint(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!void {
2734627374 const pt = sema.pt;
2734727375 const zcu = pt.zcu;
src/arch/riscv64/CodeGen.zig+10-7
......@@ -3897,7 +3897,7 @@ fn airArrayElemVal(func: *Func, inst: Air.Inst.Index) !void {
38973897
38983898 if (array_ty.isVector(zcu)) {
38993899 // we need to load the vector, vslidedown to get the element we want
3900 // and store that element at in a load frame.
3900 // and store that element in a load frame.
39013901
39023902 const src_reg, const src_lock = try func.allocReg(.vector);
39033903 defer func.register_manager.unlockReg(src_lock);
......@@ -3970,12 +3970,15 @@ fn airPtrElemVal(func: *Func, inst: Air.Inst.Index) !void {
39703970 };
39713971 defer if (index_lock) |lock| func.register_manager.unlockReg(lock);
39723972
3973 const elem_ptr_reg = if (base_ptr_mcv.isRegister() and func.liveness.operandDies(inst, 0))
3974 base_ptr_mcv.register
3975 else
3976 try func.copyToTmpRegister(base_ptr_ty, base_ptr_mcv);
3977 const elem_ptr_lock = func.register_manager.lockRegAssumeUnused(elem_ptr_reg);
3978 defer func.register_manager.unlockReg(elem_ptr_lock);
3973 const elem_ptr_reg, const elem_ptr_lock = if (base_ptr_mcv.isRegister() and
3974 func.liveness.operandDies(inst, 0))
3975 .{ base_ptr_mcv.register, null }
3976 else blk: {
3977 const reg, const lock = try func.allocReg(.int);
3978 try func.genSetReg(base_ptr_ty, reg, base_ptr_mcv);
3979 break :blk .{ reg, lock };
3980 };
3981 defer if (elem_ptr_lock) |lock| func.register_manager.unlockReg(lock);
39793982
39803983 try func.genBinOp(
39813984 .ptr_add,
src/print_zir.zig+7
......@@ -620,6 +620,7 @@ const Writer = struct {
620620 .closure_get => try self.writeClosureGet(stream, extended),
621621 .field_parent_ptr => try self.writeFieldParentPtr(stream, extended),
622622 .builtin_value => try self.writeBuiltinValue(stream, extended),
623 .inplace_arith_result_ty => try self.writeInplaceArithResultTy(stream, extended),
623624 }
624625 }
625626
......@@ -2781,6 +2782,12 @@ const Writer = struct {
27812782 try self.writeSrcNode(stream, @bitCast(extended.operand));
27822783 }
27832784
2785 fn writeInplaceArithResultTy(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {
2786 const op: Zir.Inst.InplaceOp = @enumFromInt(extended.small);
2787 try self.writeInstRef(stream, @enumFromInt(extended.operand));
2788 try stream.print(", {s}))", .{@tagName(op)});
2789 }
2790
27842791 fn writeInstRef(self: *Writer, stream: anytype, ref: Zir.Inst.Ref) !void {
27852792 if (ref == .none) {
27862793 return stream.writeAll(".none");
test/behavior/pointers.zig+15
......@@ -98,6 +98,21 @@ test "pointer subtraction" {
9898 }
9999}
100100
101test "pointer arithmetic with non-trivial RHS" {
102 var t: bool = undefined;
103 t = true;
104
105 var ptr: [*]const u8 = "Hello, World!";
106 ptr += if (t) 5 else 2;
107 try expect(ptr[0] == ',');
108 ptr += if (!t) 4 else 2;
109 try expect(ptr[0] == 'W');
110 ptr -= if (t) @as(usize, 6) else 3;
111 try expect(ptr[0] == 'e');
112 ptr -= if (!t) @as(usize, 0) else 1;
113 try expect(ptr[0] == 'H');
114}
115
101116test "double pointer parsing" {
102117 comptime assert(PtrOf(PtrOf(i32)) == **i32);
103118}