aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMatthew Lugg <mlugg@mlugg.co.uk>2024-09-16 22:04:06 +0100
committerGitHub <noreply@github.com>2024-09-16 22:04:06 +0100
commit7caa3d9da71c38665340247a1c2bf9bedb8db925 (patch)
treedcb7637f26db5363d0bdc3c60fd05660f8a371fd /src
parentf3445f8f6935b4532aab3f339f5d86319d2dca72 (diff)
parent7f60d2e4658ad78839ce0fce63a95dbcb893a256 (diff)
downloadzig-7caa3d9da71c38665340247a1c2bf9bedb8db925.tar.gz
zig-7caa3d9da71c38665340247a1c2bf9bedb8db925.zip
Merge pull request #21425 from mlugg/pointer-arith-inplace-res-ty
compiler: provide correct result types to `+=` and `-=`
Diffstat (limited to 'src')
-rw-r--r--src/Sema.zig28
-rw-r--r--src/arch/riscv64/CodeGen.zig17
-rw-r--r--src/print_zir.zig7
3 files changed, 45 insertions, 7 deletions
diff --git a/src/Sema.zig b/src/Sema.zig
index 559fbe3e53..ded3999b84 100644
--- a/src/Sema.zig
+++ b/src/Sema.zig
@@ -1361,6 +1361,7 @@ fn analyzeBodyInner(
.value_placeholder => unreachable, // never appears in a body
.field_parent_ptr => try sema.zirFieldParentPtr(block, extended),
.builtin_value => try sema.zirBuiltinValue(extended),
+ .inplace_arith_result_ty => try sema.zirInplaceArithResultTy(extended),
};
},
@@ -27342,6 +27343,33 @@ fn zirBuiltinValue(sema: *Sema, extended: Zir.Inst.Extended.InstData) CompileErr
return Air.internedToRef(ty.toIntern());
}
+fn zirInplaceArithResultTy(sema: *Sema, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref {
+ const pt = sema.pt;
+ const zcu = pt.zcu;
+
+ const lhs = try sema.resolveInst(@enumFromInt(extended.operand));
+ const lhs_ty = sema.typeOf(lhs);
+
+ const op: Zir.Inst.InplaceOp = @enumFromInt(extended.small);
+ const ty: Type = switch (op) {
+ .add_eq => ty: {
+ const ptr_size = lhs_ty.ptrSizeOrNull(zcu) orelse break :ty lhs_ty;
+ switch (ptr_size) {
+ .One, .Slice => break :ty lhs_ty, // invalid, let it error
+ .Many, .C => break :ty .usize, // `[*]T + usize`
+ }
+ },
+ .sub_eq => ty: {
+ const ptr_size = lhs_ty.ptrSizeOrNull(zcu) orelse break :ty lhs_ty;
+ switch (ptr_size) {
+ .One, .Slice => break :ty lhs_ty, // invalid, let it error
+ .Many, .C => break :ty .generic_poison, // could be `[*]T - [*]T` or `[*]T - usize`
+ }
+ },
+ };
+ return Air.internedToRef(ty.toIntern());
+}
+
fn zirBranchHint(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!void {
const pt = sema.pt;
const zcu = pt.zcu;
diff --git a/src/arch/riscv64/CodeGen.zig b/src/arch/riscv64/CodeGen.zig
index 0c6da840eb..73fa0460de 100644
--- a/src/arch/riscv64/CodeGen.zig
+++ b/src/arch/riscv64/CodeGen.zig
@@ -3897,7 +3897,7 @@ fn airArrayElemVal(func: *Func, inst: Air.Inst.Index) !void {
if (array_ty.isVector(zcu)) {
// we need to load the vector, vslidedown to get the element we want
- // and store that element at in a load frame.
+ // and store that element in a load frame.
const src_reg, const src_lock = try func.allocReg(.vector);
defer func.register_manager.unlockReg(src_lock);
@@ -3970,12 +3970,15 @@ fn airPtrElemVal(func: *Func, inst: Air.Inst.Index) !void {
};
defer if (index_lock) |lock| func.register_manager.unlockReg(lock);
- const elem_ptr_reg = if (base_ptr_mcv.isRegister() and func.liveness.operandDies(inst, 0))
- base_ptr_mcv.register
- else
- try func.copyToTmpRegister(base_ptr_ty, base_ptr_mcv);
- const elem_ptr_lock = func.register_manager.lockRegAssumeUnused(elem_ptr_reg);
- defer func.register_manager.unlockReg(elem_ptr_lock);
+ const elem_ptr_reg, const elem_ptr_lock = if (base_ptr_mcv.isRegister() and
+ func.liveness.operandDies(inst, 0))
+ .{ base_ptr_mcv.register, null }
+ else blk: {
+ const reg, const lock = try func.allocReg(.int);
+ try func.genSetReg(base_ptr_ty, reg, base_ptr_mcv);
+ break :blk .{ reg, lock };
+ };
+ defer if (elem_ptr_lock) |lock| func.register_manager.unlockReg(lock);
try func.genBinOp(
.ptr_add,
diff --git a/src/print_zir.zig b/src/print_zir.zig
index f5c83c98e2..c5f1517edd 100644
--- a/src/print_zir.zig
+++ b/src/print_zir.zig
@@ -620,6 +620,7 @@ const Writer = struct {
.closure_get => try self.writeClosureGet(stream, extended),
.field_parent_ptr => try self.writeFieldParentPtr(stream, extended),
.builtin_value => try self.writeBuiltinValue(stream, extended),
+ .inplace_arith_result_ty => try self.writeInplaceArithResultTy(stream, extended),
}
}
@@ -2781,6 +2782,12 @@ const Writer = struct {
try self.writeSrcNode(stream, @bitCast(extended.operand));
}
+ fn writeInplaceArithResultTy(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {
+ const op: Zir.Inst.InplaceOp = @enumFromInt(extended.small);
+ try self.writeInstRef(stream, @enumFromInt(extended.operand));
+ try stream.print(", {s}))", .{@tagName(op)});
+ }
+
fn writeInstRef(self: *Writer, stream: anytype, ref: Zir.Inst.Ref) !void {
if (ref == .none) {
return stream.writeAll(".none");