Merge pull request #23708 from ziglang/memmove-followups

`@memmove` followups
This commit is contained in:
Andrew Kelley 2025-04-28 15:06:18 -04:00 committed by GitHub
commit 8facd99d41
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 61 additions and 97 deletions

View file

@ -126,18 +126,16 @@ pub fn FullPanic(comptime panicFn: fn ([]const u8, ?usize) noreturn) type {
@branchHint(.cold);
call("for loop over objects with non-equal lengths", @returnAddress());
}
pub fn memcpyLenMismatch() noreturn {
/// Delete after next zig1.wasm update
pub const memcpyLenMismatch = copyLenMismatch;
pub fn copyLenMismatch() noreturn {
@branchHint(.cold);
call("@memcpy arguments have non-equal lengths", @returnAddress());
call("source and destination arguments have non-equal lengths", @returnAddress());
}
pub fn memcpyAlias() noreturn {
@branchHint(.cold);
call("@memcpy arguments alias", @returnAddress());
}
pub fn memmoveLenMismatch() noreturn {
@branchHint(.cold);
call("@memmove arguments have non-equal lengths", @returnAddress());
}
pub fn noreturnReturned() noreturn {
@branchHint(.cold);
call("'noreturn' function returned", @returnAddress());

View file

@ -125,7 +125,10 @@ pub fn forLenMismatch() noreturn {
@trap();
}
pub fn memcpyLenMismatch() noreturn {
/// Delete after next zig1.wasm update
pub const memcpyLenMismatch = copyLenMismatch;
pub fn copyLenMismatch() noreturn {
@branchHint(.cold);
@trap();
}
@ -135,11 +138,6 @@ pub fn memcpyAlias() noreturn {
@trap();
}
pub fn memmoveLenMismatch() noreturn {
@branchHint(.cold);
@trap();
}
pub fn noreturnReturned() noreturn {
@branchHint(.cold);
@trap();

View file

@ -120,18 +120,17 @@ pub fn forLenMismatch() noreturn {
call("for loop over objects with non-equal lengths", null);
}
pub fn memcpyLenMismatch() noreturn {
call("@memcpy arguments have non-equal lengths", null);
/// Delete after next zig1.wasm update
pub const memcpyLenMismatch = copyLenMismatch;
pub fn copyLenMismatch() noreturn {
call("source and destination have non-equal lengths", null);
}
pub fn memcpyAlias() noreturn {
call("@memcpy arguments alias", null);
}
pub fn memmoveLenMismatch() noreturn {
call("@memmove arguments have non-equal lengths", null);
}
pub fn noreturnReturned() noreturn {
call("'noreturn' function returned", null);
}

View file

@ -983,12 +983,12 @@ pub const Inst = struct {
/// Implements the `@memcpy` builtin.
/// Uses the `pl_node` union field with payload `Bin`.
memcpy,
/// Implements the `@memset` builtin.
/// Uses the `pl_node` union field with payload `Bin`.
memset,
/// Implements the `@memmove` builtin.
/// Uses the `pl_node` union field with payload `Bin`.
memmove,
/// Implements the `@memset` builtin.
/// Uses the `pl_node` union field with payload `Bin`.
memset,
/// Implements the `@min` builtin for 2 args.
/// Uses the `pl_node` union field with payload `Bin`
min,

View file

@ -1574,7 +1574,12 @@ fn analyzeBodyInner(
continue;
},
.memcpy => {
try sema.zirMemcpy(block, inst);
try sema.zirMemcpy(block, inst, .memcpy, true);
i += 1;
continue;
},
.memmove => {
try sema.zirMemcpy(block, inst, .memmove, false);
i += 1;
continue;
},
@ -1583,11 +1588,6 @@ fn analyzeBodyInner(
i += 1;
continue;
},
.memmove => {
try sema.zirMemmove(block, inst);
i += 1;
continue;
},
.check_comptime_control_flow => {
if (!block.isComptime()) {
const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;
@ -25630,19 +25630,12 @@ fn upgradeToArrayPtr(sema: *Sema, block: *Block, ptr: Air.Inst.Ref, len: u64) !A
return block.addBitCast(new_ty, non_slice_ptr);
}
fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
return sema.analyzeCopy(block, inst, .memcpy);
}
fn zirMemmove(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
return sema.analyzeCopy(block, inst, .memmove);
}
fn analyzeCopy(
fn zirMemcpy(
sema: *Sema,
block: *Block,
inst: Zir.Inst.Index,
op: enum { memcpy, memmove },
air_tag: Air.Inst.Tag,
check_aliasing: bool,
) CompileError!void {
const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
@ -25659,12 +25652,12 @@ fn analyzeCopy(
const zcu = pt.zcu;
if (dest_ty.isConstPtr(zcu)) {
return sema.fail(block, dest_src, "cannot {s} to constant pointer", .{@tagName(op)});
return sema.fail(block, dest_src, "cannot copy to constant pointer", .{});
}
if (dest_len == .none and src_len == .none) {
const msg = msg: {
const msg = try sema.errMsg(src, "unknown @{s} length", .{@tagName(op)});
const msg = try sema.errMsg(src, "unknown copy length", .{});
errdefer msg.destroy(sema.gpa);
try sema.errNote(dest_src, msg, "destination type '{}' provides no length", .{
dest_ty.fmt(pt),
@ -25710,7 +25703,7 @@ fn analyzeCopy(
if (try sema.resolveDefinedValue(block, src_src, src_len)) |src_len_val| {
if (!(try sema.valuesEqual(dest_len_val, src_len_val, Type.usize))) {
const msg = msg: {
const msg = try sema.errMsg(src, "non-matching @{s} lengths", .{@tagName(op)});
const msg = try sema.errMsg(src, "non-matching copy lengths", .{});
errdefer msg.destroy(sema.gpa);
try sema.errNote(dest_src, msg, "length {} here", .{
dest_len_val.fmtValueSema(pt, sema),
@ -25730,11 +25723,7 @@ fn analyzeCopy(
if (block.wantSafety()) {
const ok = try block.addBinOp(.cmp_eq, dest_len, src_len);
const panic_id: Zcu.SimplePanicId = switch (op) {
.memcpy => .memcpy_len_mismatch,
.memmove => .memmove_len_mismatch,
};
try sema.addSafetyCheck(block, src, ok, panic_id);
try sema.addSafetyCheck(block, src, ok, .copy_len_mismatch);
}
} else if (dest_len != .none) {
if (try sema.resolveDefinedValue(block, dest_src, dest_len)) |dest_len_val| {
@ -25762,11 +25751,6 @@ fn analyzeCopy(
return;
}
const check_aliasing = switch (op) {
.memcpy => true,
.memmove => false,
};
const runtime_src = rs: {
const dest_ptr_val = try sema.resolveDefinedValue(block, dest_src, dest_ptr) orelse break :rs dest_src;
const src_ptr_val = try sema.resolveDefinedValue(block, src_src, src_ptr) orelse break :rs src_src;
@ -25898,10 +25882,7 @@ fn analyzeCopy(
}
_ = try block.addInst(.{
.tag = switch (op) {
.memcpy => .memcpy,
.memmove => .memmove,
},
.tag = air_tag,
.data = .{ .bin_op = .{
.lhs = new_dest_ptr,
.rhs = new_src_ptr,
@ -38153,9 +38134,8 @@ fn getExpectedBuiltinFnType(sema: *Sema, decl: Zcu.BuiltinDecl) CompileError!Typ
.@"panic.shiftRhsTooBig",
.@"panic.invalidEnumValue",
.@"panic.forLenMismatch",
.@"panic.memcpyLenMismatch",
.@"panic.copyLenMismatch",
.@"panic.memcpyAlias",
.@"panic.memmoveLenMismatch",
.@"panic.noreturnReturned",
=> try pt.funcType(.{
.param_types = &.{},

View file

@ -300,9 +300,8 @@ pub const BuiltinDecl = enum {
@"panic.shiftRhsTooBig",
@"panic.invalidEnumValue",
@"panic.forLenMismatch",
@"panic.memcpyLenMismatch",
@"panic.copyLenMismatch",
@"panic.memcpyAlias",
@"panic.memmoveLenMismatch",
@"panic.noreturnReturned",
VaList,
@ -378,9 +377,8 @@ pub const BuiltinDecl = enum {
.@"panic.shiftRhsTooBig",
.@"panic.invalidEnumValue",
.@"panic.forLenMismatch",
.@"panic.memcpyLenMismatch",
.@"panic.copyLenMismatch",
.@"panic.memcpyAlias",
.@"panic.memmoveLenMismatch",
.@"panic.noreturnReturned",
=> .func,
};
@ -446,9 +444,8 @@ pub const SimplePanicId = enum {
shift_rhs_too_big,
invalid_enum_value,
for_len_mismatch,
memcpy_len_mismatch,
copy_len_mismatch,
memcpy_alias,
memmove_len_mismatch,
noreturn_returned,
pub fn toBuiltin(id: SimplePanicId) BuiltinDecl {
@ -471,9 +468,8 @@ pub const SimplePanicId = enum {
.shift_rhs_too_big => .@"panic.shiftRhsTooBig",
.invalid_enum_value => .@"panic.invalidEnumValue",
.for_len_mismatch => .@"panic.forLenMismatch",
.memcpy_len_mismatch => .@"panic.memcpyLenMismatch",
.copy_len_mismatch => .@"panic.copyLenMismatch",
.memcpy_alias => .@"panic.memcpyAlias",
.memmove_len_mismatch => .@"panic.memmoveLenMismatch",
.noreturn_returned => .@"panic.noreturnReturned",
// zig fmt: on
};

View file

@ -3348,8 +3348,8 @@ fn genBodyInner(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail,
.atomic_load => try airAtomicLoad(f, inst),
.memset => try airMemset(f, inst, false),
.memset_safe => try airMemset(f, inst, true),
.memcpy => try airMemcpy(f, inst),
.memmove => try airMemmove(f, inst),
.memcpy => try airMemcpy(f, inst, "memcpy("),
.memmove => try airMemcpy(f, inst, "memmove("),
.set_union_tag => try airSetUnionTag(f, inst),
.get_union_tag => try airGetUnionTag(f, inst),
.clz => try airUnBuiltinCall(f, inst, air_datas[@intFromEnum(inst)].ty_op.operand, "clz", .bits),
@ -6976,15 +6976,7 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
return .none;
}
fn airMemcpy(f: *Function, inst: Air.Inst.Index) !CValue {
return copyOp(f, inst, .memcpy);
}
fn airMemmove(f: *Function, inst: Air.Inst.Index) !CValue {
return copyOp(f, inst, .memmove);
}
fn copyOp(f: *Function, inst: Air.Inst.Index, op: enum { memcpy, memmove }) !CValue {
fn airMemcpy(f: *Function, inst: Air.Inst.Index, function_paren: []const u8) !CValue {
const pt = f.object.dg.pt;
const zcu = pt.zcu;
const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
@ -6999,10 +6991,6 @@ fn copyOp(f: *Function, inst: Air.Inst.Index, op: enum { memcpy, memmove }) !CVa
try writeArrayLen(f, writer, dest_ptr, dest_ty);
try writer.writeAll(" != 0) ");
}
const function_paren = switch (op) {
.memcpy => "memcpy(",
.memmove => "memmove(",
};
try writer.writeAll(function_paren);
try writeSliceOrPtr(f, writer, dest_ptr, dest_ty);
try writer.writeAll(", ");

View file

@ -27,9 +27,10 @@ pub const panic = struct {
pub const shiftRhsTooBig = simple_panic.shiftRhsTooBig;
pub const invalidEnumValue = simple_panic.invalidEnumValue;
pub const forLenMismatch = simple_panic.forLenMismatch;
pub const memcpyLenMismatch = simple_panic.memcpyLenMismatch;
/// Delete after next zig1.wasm update
pub const memcpyLenMismatch = copyLenMismatch;
pub const copyLenMismatch = simple_panic.copyLenMismatch;
pub const memcpyAlias = simple_panic.memcpyAlias;
pub const memmoveLenMismatch = simple_panic.memmoveLenMismatch;
pub const noreturnReturned = simple_panic.noreturnReturned;
};

View file

@ -23,9 +23,10 @@ pub const panic = struct {
pub const shiftRhsTooBig = simple_panic.shiftRhsTooBig;
pub const invalidEnumValue = simple_panic.invalidEnumValue;
pub const forLenMismatch = simple_panic.forLenMismatch;
pub const memcpyLenMismatch = simple_panic.memcpyLenMismatch;
/// Delete after next zig1.wasm update
pub const memcpyLenMismatch = copyLenMismatch;
pub const copyLenMismatch = simple_panic.copyLenMismatch;
pub const memcpyAlias = simple_panic.memcpyAlias;
pub const memmoveLenMismatch = simple_panic.memmoveLenMismatch;
pub const noreturnReturned = simple_panic.noreturnReturned;
};

View file

@ -66,29 +66,29 @@ pub export fn memset_array() void {
// backend=stage2
// target=native
//
// :5:5: error: unknown @memcpy length
// :5:5: error: unknown copy length
// :5:18: note: destination type '[*]u8' provides no length
// :5:24: note: source type '[*]const u8' provides no length
// :10:13: error: type '*u8' is not an indexable pointer
// :10:13: note: operand must be a slice, a many pointer or a pointer to an array
// :15:13: error: type '*u8' is not an indexable pointer
// :15:13: note: operand must be a slice, a many pointer or a pointer to an array
// :20:5: error: non-matching @memcpy lengths
// :20:5: error: non-matching copy lengths
// :20:13: note: length 6 here
// :20:20: note: length 5 here
// :24:13: error: cannot memset constant pointer
// :29:13: error: cannot memcpy to constant pointer
// :29:13: error: cannot copy to constant pointer
// :33:13: error: type '[5]u8' is not an indexable pointer
// :33:13: note: operand must be a slice, a many pointer or a pointer to an array
// :39:5: error: unknown @memmove length
// :39:5: error: unknown copy length
// :39:19: note: destination type '[*]u8' provides no length
// :39:25: note: source type '[*]const u8' provides no length
// :44:14: error: type '*u8' is not an indexable pointer
// :44:14: note: operand must be a slice, a many pointer or a pointer to an array
// :49:5: error: non-matching @memmove lengths
// :49:5: error: non-matching copy lengths
// :49:14: note: length 6 here
// :49:21: note: length 5 here
// :54:14: error: cannot memmove to constant pointer
// :54:14: error: cannot copy to constant pointer
// :58:14: error: type '[5]u8' is not an indexable pointer
// :58:14: note: operand must be a slice, a many pointer or a pointer to an array
// :62:13: error: type '[5]u8' is not an indexable pointer

View file

@ -2,7 +2,7 @@ const std = @import("std");
pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace, _: ?usize) noreturn {
_ = stack_trace;
if (std.mem.eql(u8, message, "@memcpy arguments have non-equal lengths")) {
if (std.mem.eql(u8, message, "source and destination arguments have non-equal lengths")) {
std.process.exit(0);
}
std.process.exit(1);

View file

@ -2,7 +2,7 @@ const std = @import("std");
pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace, _: ?usize) noreturn {
_ = stack_trace;
if (std.mem.eql(u8, message, "@memmove arguments have non-equal lengths")) {
if (std.mem.eql(u8, message, "source and destination arguments have non-equal lengths")) {
std.process.exit(0);
}
std.process.exit(1);

View file

@ -37,9 +37,10 @@ pub const panic = struct {
pub const shiftRhsTooBig = no_panic.shiftRhsTooBig;
pub const invalidEnumValue = no_panic.invalidEnumValue;
pub const forLenMismatch = no_panic.forLenMismatch;
pub const memcpyLenMismatch = no_panic.memcpyLenMismatch;
/// Delete after next zig1.wasm update
pub const memcpyLenMismatch = copyLenMismatch;
pub const copyLenMismatch = no_panic.copyLenMismatch;
pub const memcpyAlias = no_panic.memcpyAlias;
pub const memmoveLenMismatch = no_panic.memmoveLenMismatch;
pub const noreturnReturned = no_panic.noreturnReturned;
};
fn myPanic(msg: []const u8, _: ?usize) noreturn {
@ -85,9 +86,10 @@ pub const panic = struct {
pub const shiftRhsTooBig = no_panic.shiftRhsTooBig;
pub const invalidEnumValue = no_panic.invalidEnumValue;
pub const forLenMismatch = no_panic.forLenMismatch;
pub const memcpyLenMismatch = no_panic.memcpyLenMismatch;
/// Delete after next zig1.wasm update
pub const memcpyLenMismatch = copyLenMismatch;
pub const copyLenMismatch = no_panic.copyLenMismatch;
pub const memcpyAlias = no_panic.memcpyAlias;
pub const memmoveLenMismatch = no_panic.memmoveLenMismatch;
pub const noreturnReturned = no_panic.noreturnReturned;
};
fn myPanic(msg: []const u8, _: ?usize) noreturn {
@ -133,9 +135,10 @@ pub const panic = struct {
pub const shiftRhsTooBig = no_panic.shiftRhsTooBig;
pub const invalidEnumValue = no_panic.invalidEnumValue;
pub const forLenMismatch = no_panic.forLenMismatch;
pub const memcpyLenMismatch = no_panic.memcpyLenMismatch;
/// Delete after next zig1.wasm update
pub const memcpyLenMismatch = copyLenMismatch;
pub const copyLenMismatch = no_panic.copyLenMismatch;
pub const memcpyAlias = no_panic.memcpyAlias;
pub const memmoveLenMismatch = no_panic.memmoveLenMismatch;
pub const noreturnReturned = no_panic.noreturnReturned;
};
fn myPanicNew(msg: []const u8, _: ?usize) noreturn {