authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-04-27 14:39:21-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2025-04-27 14:39:21-04:00
log1b76d4c53adafebcbfcf0476276375353139dacb
tree5854ca686d0d016e2d109766fa17742fff27c6ac
parent227d2b15e449db1e84788d2c87e4f49100d316ca
parent2e9c1553ef40e9f21c2241294b8942369ef9007a
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #22605 from dweiller/memmove

add `@memmove` builtin

41 files changed, 714 insertions(+), 15 deletions(-)

doc/langref.html.in+17
......@@ -5149,6 +5149,23 @@ fn cmpxchgWeakButNotAtomic(comptime T: type, ptr: *T, expected_value: T, new_val
51495149 {#syntax#}std.crypto.secureZero{#endsyntax#}</p>
51505150 {#header_close#}
51515151
5152 {#header_open|@memmove#}
5153 <pre>{#syntax#}@memmove(dest, source) void{#endsyntax#}</pre>
5154 <p>This function copies bytes from one region of memory to another, but unlike
5155 {#link|@memcpy#} the regions may overlap.</p>
5156 <p>{#syntax#}dest{#endsyntax#} must be a mutable slice, a mutable pointer to an array, or
5157 a mutable many-item {#link|pointer|Pointers#}. It may have any
5158 alignment, and it may have any element type.</p>
5159 <p>{#syntax#}source{#endsyntax#} must be a slice, a pointer to
5160 an array, or a many-item {#link|pointer|Pointers#}. It may
5161 have any alignment, and it may have any element type.</p>
5162 <p>The {#syntax#}source{#endsyntax#} element type must have the same in-memory
5163 representation as the {#syntax#}dest{#endsyntax#} element type.</p>
5164 <p>Similar to {#link|for#} loops, at least one of {#syntax#}source{#endsyntax#} and
5165 {#syntax#}dest{#endsyntax#} must provide a length, and if two lengths are provided,
5166 they must be equal.</p>
5167 {#header_close#}
5168
51525169 {#header_open|@min#}
51535170 <pre>{#syntax#}@min(...) T{#endsyntax#}</pre>
51545171 <p>
lib/std/debug.zig+4
......@@ -134,6 +134,10 @@ pub fn FullPanic(comptime panicFn: fn ([]const u8, ?usize) noreturn) type {
134134 @branchHint(.cold);
135135 call("@memcpy arguments alias", @returnAddress());
136136 }
137 pub fn memmoveLenMismatch() noreturn {
138 @branchHint(.cold);
139 call("@memmove arguments have non-equal lengths", @returnAddress());
140 }
137141 pub fn noreturnReturned() noreturn {
138142 @branchHint(.cold);
139143 call("'noreturn' function returned", @returnAddress());
lib/std/debug/no_panic.zig+5
......@@ -135,6 +135,11 @@ pub fn memcpyAlias() noreturn {
135135 @trap();
136136}
137137
138pub fn memmoveLenMismatch() noreturn {
139 @branchHint(.cold);
140 @trap();
141}
142
138143pub fn noreturnReturned() noreturn {
139144 @branchHint(.cold);
140145 @trap();
lib/std/debug/simple_panic.zig+4
......@@ -128,6 +128,10 @@ pub fn memcpyAlias() noreturn {
128128 call("@memcpy arguments alias", null);
129129}
130130
131pub fn memmoveLenMismatch() noreturn {
132 call("@memmove arguments have non-equal lengths", null);
133}
134
131135pub fn noreturnReturned() noreturn {
132136 call("'noreturn' function returned", null);
133137}
lib/std/mem.zig+2
......@@ -232,6 +232,7 @@ test "Allocator alloc and remap with zero-bit type" {
232232/// Copy all of source into dest at position 0.
233233/// dest.len must be >= source.len.
234234/// If the slices overlap, dest.ptr must be <= src.ptr.
235/// This function is deprecated; use @memmove instead.
235236pub fn copyForwards(comptime T: type, dest: []T, source: []const T) void {
236237 for (dest[0..source.len], source) |*d, s| d.* = s;
237238}
......@@ -239,6 +240,7 @@ pub fn copyForwards(comptime T: type, dest: []T, source: []const T) void {
239240/// Copy all of source into dest at position 0.
240241/// dest.len must be >= source.len.
241242/// If the slices overlap, dest.ptr must be >= src.ptr.
243/// This function is deprecated; use @memmove instead.
242244pub fn copyBackwards(comptime T: type, dest: []T, source: []const T) void {
243245 // TODO instead of manually doing this check for the whole array
244246 // and turning off runtime safety, the compiler should detect loops like
lib/std/zig/AstGen.zig+8
......@@ -2919,6 +2919,7 @@ fn addEnsureResult(gz: *GenZir, maybe_unused_result: Zir.Inst.Ref, statement: As
29192919 .set_runtime_safety,
29202920 .memcpy,
29212921 .memset,
2922 .memmove,
29222923 .validate_deref,
29232924 .validate_destructure,
29242925 .save_err_ret_index,
......@@ -9717,6 +9718,13 @@ fn builtinCall(
97179718 });
97189719 return rvalue(gz, ri, .void_value, node);
97199720 },
9721 .memmove => {
9722 _ = try gz.addPlNode(.memmove, node, Zir.Inst.Bin{
9723 .lhs = try expr(gz, scope, .{ .rl = .none }, params[0]),
9724 .rhs = try expr(gz, scope, .{ .rl = .none }, params[1]),
9725 });
9726 return rvalue(gz, ri, .void_value, node);
9727 },
97209728 .shuffle => {
97219729 const result = try gz.addPlNode(.shuffle, node, Zir.Inst.Shuffle{
97229730 .elem_type = try typeExpr(gz, scope, params[0]),
lib/std/zig/AstRlAnnotate.zig+1-1
......@@ -1055,7 +1055,7 @@ fn builtinCall(astrl: *AstRlAnnotate, block: ?*Block, ri: ResultInfo, node: Ast.
10551055 _ = try astrl.expr(args[2], block, ResultInfo.none);
10561056 return false;
10571057 },
1058 .memcpy => {
1058 .memcpy, .memmove => {
10591059 _ = try astrl.expr(args[0], block, ResultInfo.none);
10601060 _ = try astrl.expr(args[1], block, ResultInfo.none);
10611061 return false;
lib/std/zig/BuiltinFn.zig+8
......@@ -68,6 +68,7 @@ pub const Tag = enum {
6868 max,
6969 memcpy,
7070 memset,
71 memmove,
7172 min,
7273 wasm_memory_size,
7374 wasm_memory_grow,
......@@ -641,6 +642,13 @@ pub const list = list: {
641642 .param_count = 2,
642643 },
643644 },
645 .{
646 "@memmove",
647 .{
648 .tag = .memmove,
649 .param_count = 2,
650 },
651 },
644652 .{
645653 "@min",
646654 .{
lib/std/zig/Zir.zig+7
......@@ -986,6 +986,9 @@ pub const Inst = struct {
986986 /// Implements the `@memset` builtin.
987987 /// Uses the `pl_node` union field with payload `Bin`.
988988 memset,
989 /// Implements the `@memmove` builtin.
990 /// Uses the `pl_node` union field with payload `Bin`.
991 memmove,
989992 /// Implements the `@min` builtin for 2 args.
990993 /// Uses the `pl_node` union field with payload `Bin`
991994 min,
......@@ -1272,6 +1275,7 @@ pub const Inst = struct {
12721275 .max,
12731276 .memcpy,
12741277 .memset,
1278 .memmove,
12751279 .min,
12761280 .c_import,
12771281 .@"resume",
......@@ -1355,6 +1359,7 @@ pub const Inst = struct {
13551359 .set_runtime_safety,
13561360 .memcpy,
13571361 .memset,
1362 .memmove,
13581363 .check_comptime_control_flow,
13591364 .@"defer",
13601365 .defer_err_code,
......@@ -1832,6 +1837,7 @@ pub const Inst = struct {
18321837 .max = .pl_node,
18331838 .memcpy = .pl_node,
18341839 .memset = .pl_node,
1840 .memmove = .pl_node,
18351841 .min = .pl_node,
18361842 .c_import = .pl_node,
18371843
......@@ -4291,6 +4297,7 @@ fn findTrackableInner(
42914297 .mul_add,
42924298 .memcpy,
42934299 .memset,
4300 .memmove,
42944301 .min,
42954302 .max,
42964303 .alloc,
lib/std/zig/llvm/Builder.zig+30
......@@ -6125,6 +6125,36 @@ pub const WipFunction = struct {
61256125 return value.unwrap().instruction;
61266126 }
61276127
6128 pub fn callMemMove(
6129 self: *WipFunction,
6130 dst: Value,
6131 dst_align: Alignment,
6132 src: Value,
6133 src_align: Alignment,
6134 len: Value,
6135 kind: MemoryAccessKind,
6136 ) Allocator.Error!Instruction.Index {
6137 var dst_attrs = [_]Attribute.Index{try self.builder.attr(.{ .@"align" = dst_align })};
6138 var src_attrs = [_]Attribute.Index{try self.builder.attr(.{ .@"align" = src_align })};
6139 const value = try self.callIntrinsic(
6140 .normal,
6141 try self.builder.fnAttrs(&.{
6142 .none,
6143 .none,
6144 try self.builder.attrs(&dst_attrs),
6145 try self.builder.attrs(&src_attrs),
6146 }),
6147 .memmove,
6148 &.{ dst.typeOfWip(self), src.typeOfWip(self), len.typeOfWip(self) },
6149 &.{ dst, src, len, switch (kind) {
6150 .normal => Value.false,
6151 .@"volatile" => Value.true,
6152 } },
6153 undefined,
6154 );
6155 return value.unwrap().instruction;
6156 }
6157
61286158 pub fn callMemSet(
61296159 self: *WipFunction,
61306160 dst: Value,
lib/zig.h+1
......@@ -481,6 +481,7 @@
481481
482482zig_extern void *memcpy (void *zig_restrict, void const *zig_restrict, size_t);
483483zig_extern void *memset (void *, int, size_t);
484zig_extern void *memmove (void *, void const *, size_t);
484485
485486/* ================ Bool and 8/16/32/64-bit Integer Support ================= */
486487
src/Air.zig+14
......@@ -730,6 +730,18 @@ pub const Inst = struct {
730730 /// source being a pointer-to-array), then it is guaranteed to be
731731 /// greater than zero.
732732 memcpy,
733 /// Given dest pointer and source pointer, copy elements from source to dest.
734 /// Dest pointer is either a slice or a pointer to array.
735 /// The dest element type may be any type.
736 /// Source pointer must have same element type as dest element type.
737 /// Dest slice may have any alignment; source pointer may have any alignment.
738 /// The two memory regions may overlap.
739 /// Result type is always void.
740 /// Uses the `bin_op` field. LHS is the dest slice. RHS is the source pointer.
741 /// If the length is compile-time known (due to the destination or
742 /// source being a pointer-to-array), then it is guaranteed to be
743 /// greater than zero.
744 memmove,
733745
734746 /// Uses the `ty_pl` field with payload `Cmpxchg`.
735747 cmpxchg_weak,
......@@ -1533,6 +1545,7 @@ pub fn typeOfIndex(air: *const Air, inst: Air.Inst.Index, ip: *const InternPool)
15331545 .memset,
15341546 .memset_safe,
15351547 .memcpy,
1548 .memmove,
15361549 .set_union_tag,
15371550 .prefetch,
15381551 .set_err_return_trace,
......@@ -1696,6 +1709,7 @@ pub fn mustLower(air: Air, inst: Air.Inst.Index, ip: *const InternPool) bool {
16961709 .memset,
16971710 .memset_safe,
16981711 .memcpy,
1712 .memmove,
16991713 .cmpxchg_weak,
17001714 .cmpxchg_strong,
17011715 .atomic_store_unordered,
src/Air/types_resolved.zig+1
......@@ -83,6 +83,7 @@ fn checkBody(air: Air, body: []const Air.Inst.Index, zcu: *Zcu) bool {
8383 .memset,
8484 .memset_safe,
8585 .memcpy,
86 .memmove,
8687 .atomic_store_unordered,
8788 .atomic_store_monotonic,
8889 .atomic_store_release,
src/Liveness.zig+2
......@@ -300,6 +300,7 @@ pub fn categorizeOperand(
300300 .memset,
301301 .memset_safe,
302302 .memcpy,
303 .memmove,
303304 => {
304305 const o = air_datas[@intFromEnum(inst)].bin_op;
305306 if (o.lhs == operand_ref) return matchOperandSmallIndex(l, inst, 0, .write);
......@@ -936,6 +937,7 @@ fn analyzeInst(
936937 .memset,
937938 .memset_safe,
938939 .memcpy,
940 .memmove,
939941 => {
940942 const o = inst_datas[@intFromEnum(inst)].bin_op;
941943 return analyzeOperands(a, pass, data, inst, .{ o.lhs, o.rhs, .none });
src/Liveness/Verify.zig+1
......@@ -267,6 +267,7 @@ fn verifyBody(self: *Verify, body: []const Air.Inst.Index) Error!void {
267267 .memset,
268268 .memset_safe,
269269 .memcpy,
270 .memmove,
270271 => {
271272 const bin_op = data[@intFromEnum(inst)].bin_op;
272273 try self.verifyInstOperands(inst, .{ bin_op.lhs, bin_op.rhs, .none });
src/Sema.zig+45-12
......@@ -1583,6 +1583,11 @@ fn analyzeBodyInner(
15831583 i += 1;
15841584 continue;
15851585 },
1586 .memmove => {
1587 try sema.zirMemmove(block, inst);
1588 i += 1;
1589 continue;
1590 },
15861591 .check_comptime_control_flow => {
15871592 if (!block.isComptime()) {
15881593 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;
......@@ -25610,6 +25615,19 @@ fn upgradeToArrayPtr(sema: *Sema, block: *Block, ptr: Air.Inst.Ref, len: u64) !A
2561025615}
2561125616
2561225617fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
25618 return sema.analyzeCopy(block, inst, .memcpy);
25619}
25620
25621fn zirMemmove(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
25622 return sema.analyzeCopy(block, inst, .memmove);
25623}
25624
25625fn analyzeCopy(
25626 sema: *Sema,
25627 block: *Block,
25628 inst: Zir.Inst.Index,
25629 op: enum { memcpy, memmove },
25630) CompileError!void {
2561325631 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
2561425632 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
2561525633 const src = block.nodeOffset(inst_data.src_node);
......@@ -25625,12 +25643,12 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
2562525643 const zcu = pt.zcu;
2562625644
2562725645 if (dest_ty.isConstPtr(zcu)) {
25628 return sema.fail(block, dest_src, "cannot memcpy to constant pointer", .{});
25646 return sema.fail(block, dest_src, "cannot {s} to constant pointer", .{@tagName(op)});
2562925647 }
2563025648
2563125649 if (dest_len == .none and src_len == .none) {
2563225650 const msg = msg: {
25633 const msg = try sema.errMsg(src, "unknown @memcpy length", .{});
25651 const msg = try sema.errMsg(src, "unknown @{s} length", .{@tagName(op)});
2563425652 errdefer msg.destroy(sema.gpa);
2563525653 try sema.errNote(dest_src, msg, "destination type '{}' provides no length", .{
2563625654 dest_ty.fmt(pt),
......@@ -25676,7 +25694,7 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
2567625694 if (try sema.resolveDefinedValue(block, src_src, src_len)) |src_len_val| {
2567725695 if (!(try sema.valuesEqual(dest_len_val, src_len_val, Type.usize))) {
2567825696 const msg = msg: {
25679 const msg = try sema.errMsg(src, "non-matching @memcpy lengths", .{});
25697 const msg = try sema.errMsg(src, "non-matching @{s} lengths", .{@tagName(op)});
2568025698 errdefer msg.destroy(sema.gpa);
2568125699 try sema.errNote(dest_src, msg, "length {} here", .{
2568225700 dest_len_val.fmtValueSema(pt, sema),
......@@ -25696,7 +25714,11 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
2569625714
2569725715 if (block.wantSafety()) {
2569825716 const ok = try block.addBinOp(.cmp_eq, dest_len, src_len);
25699 try sema.addSafetyCheck(block, src, ok, .memcpy_len_mismatch);
25717 const panic_id: Zcu.SimplePanicId = switch (op) {
25718 .memcpy => .memcpy_len_mismatch,
25719 .memmove => .memmove_len_mismatch,
25720 };
25721 try sema.addSafetyCheck(block, src, ok, panic_id);
2570025722 }
2570125723 } else if (dest_len != .none) {
2570225724 if (try sema.resolveDefinedValue(block, dest_src, dest_len)) |dest_len_val| {
......@@ -25724,6 +25746,11 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
2572425746 return;
2572525747 }
2572625748
25749 const check_aliasing = switch (op) {
25750 .memcpy => true,
25751 .memmove => false,
25752 };
25753
2572725754 const runtime_src = rs: {
2572825755 const dest_ptr_val = try sema.resolveDefinedValue(block, dest_src, dest_ptr) orelse break :rs dest_src;
2572925756 const src_ptr_val = try sema.resolveDefinedValue(block, src_src, src_ptr) orelse break :rs src_src;
......@@ -25733,12 +25760,14 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
2573325760
2573425761 const len_u64 = try len_val.?.toUnsignedIntSema(pt);
2573525762
25736 if (Value.doPointersOverlap(
25737 raw_src_ptr,
25738 raw_dest_ptr,
25739 len_u64,
25740 zcu,
25741 )) return sema.fail(block, src, "'@memcpy' arguments alias", .{});
25763 if (check_aliasing) {
25764 if (Value.doPointersOverlap(
25765 raw_src_ptr,
25766 raw_dest_ptr,
25767 len_u64,
25768 zcu,
25769 )) return sema.fail(block, src, "'@memcpy' arguments alias", .{});
25770 }
2574225771
2574325772 if (!sema.isComptimeMutablePtr(dest_ptr_val)) break :rs dest_src;
2574425773
......@@ -25810,7 +25839,7 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
2581025839 try sema.validateRuntimeValue(block, src_src, src_ptr);
2581125840
2581225841 // Aliasing safety check.
25813 if (block.wantSafety()) {
25842 if (check_aliasing and block.wantSafety()) {
2581425843 const len = if (len_val) |v|
2581525844 Air.internedToRef(v.toIntern())
2581625845 else if (dest_len != .none)
......@@ -25853,7 +25882,10 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
2585325882 }
2585425883
2585525884 _ = try block.addInst(.{
25856 .tag = .memcpy,
25885 .tag = switch (op) {
25886 .memcpy => .memcpy,
25887 .memmove => .memmove,
25888 },
2585725889 .data = .{ .bin_op = .{
2585825890 .lhs = new_dest_ptr,
2585925891 .rhs = new_src_ptr,
......@@ -38078,6 +38110,7 @@ fn getExpectedBuiltinFnType(sema: *Sema, decl: Zcu.BuiltinDecl) CompileError!Typ
3807838110 .@"panic.forLenMismatch",
3807938111 .@"panic.memcpyLenMismatch",
3808038112 .@"panic.memcpyAlias",
38113 .@"panic.memmoveLenMismatch",
3808138114 .@"panic.noreturnReturned",
3808238115 => try pt.funcType(.{
3808338116 .param_types = &.{},
src/Zcu.zig+4
......@@ -302,6 +302,7 @@ pub const BuiltinDecl = enum {
302302 @"panic.forLenMismatch",
303303 @"panic.memcpyLenMismatch",
304304 @"panic.memcpyAlias",
305 @"panic.memmoveLenMismatch",
305306 @"panic.noreturnReturned",
306307
307308 VaList,
......@@ -379,6 +380,7 @@ pub const BuiltinDecl = enum {
379380 .@"panic.forLenMismatch",
380381 .@"panic.memcpyLenMismatch",
381382 .@"panic.memcpyAlias",
383 .@"panic.memmoveLenMismatch",
382384 .@"panic.noreturnReturned",
383385 => .func,
384386 };
......@@ -446,6 +448,7 @@ pub const SimplePanicId = enum {
446448 for_len_mismatch,
447449 memcpy_len_mismatch,
448450 memcpy_alias,
451 memmove_len_mismatch,
449452 noreturn_returned,
450453
451454 pub fn toBuiltin(id: SimplePanicId) BuiltinDecl {
......@@ -470,6 +473,7 @@ pub const SimplePanicId = enum {
470473 .for_len_mismatch => .@"panic.forLenMismatch",
471474 .memcpy_len_mismatch => .@"panic.memcpyLenMismatch",
472475 .memcpy_alias => .@"panic.memcpyAlias",
476 .memmove_len_mismatch => .@"panic.memmoveLenMismatch",
473477 .noreturn_returned => .@"panic.noreturnReturned",
474478 // zig fmt: on
475479 };
src/arch/aarch64/CodeGen.zig+6
......@@ -760,6 +760,7 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
760760 .atomic_rmw => try self.airAtomicRmw(inst),
761761 .atomic_load => try self.airAtomicLoad(inst),
762762 .memcpy => try self.airMemcpy(inst),
763 .memmove => try self.airMemmove(inst),
763764 .memset => try self.airMemset(inst, false),
764765 .memset_safe => try self.airMemset(inst, true),
765766 .set_union_tag => try self.airSetUnionTag(inst),
......@@ -5993,6 +5994,11 @@ fn airMemcpy(self: *Self, inst: Air.Inst.Index) InnerError!void {
59935994 return self.fail("TODO implement airMemcpy for {}", .{self.target.cpu.arch});
59945995}
59955996
5997fn airMemmove(self: *Self, inst: Air.Inst.Index) InnerError!void {
5998 _ = inst;
5999 return self.fail("TODO implement airMemmove for {}", .{self.target.cpu.arch});
6000}
6001
59966002fn airTagName(self: *Self, inst: Air.Inst.Index) InnerError!void {
59976003 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
59986004 const operand = try self.resolveInst(un_op);
src/arch/arm/CodeGen.zig+6
......@@ -749,6 +749,7 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
749749 .atomic_rmw => try self.airAtomicRmw(inst),
750750 .atomic_load => try self.airAtomicLoad(inst),
751751 .memcpy => try self.airMemcpy(inst),
752 .memmove => try self.airMemmove(inst),
752753 .memset => try self.airMemset(inst, false),
753754 .memset_safe => try self.airMemset(inst, true),
754755 .set_union_tag => try self.airSetUnionTag(inst),
......@@ -5963,6 +5964,11 @@ fn airMemcpy(self: *Self, inst: Air.Inst.Index) !void {
59635964 return self.fail("TODO implement airMemcpy for {}", .{self.target.cpu.arch});
59645965}
59655966
5967fn airMemmove(self: *Self, inst: Air.Inst.Index) !void {
5968 _ = inst;
5969 return self.fail("TODO implement airMemmove for {}", .{self.target.cpu.arch});
5970}
5971
59665972fn airTagName(self: *Self, inst: Air.Inst.Index) !void {
59675973 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
59685974 const operand = try self.resolveInst(un_op);
src/arch/riscv64/CodeGen.zig+6
......@@ -1581,6 +1581,7 @@ fn genBody(func: *Func, body: []const Air.Inst.Index) InnerError!void {
15811581 .atomic_rmw => try func.airAtomicRmw(inst),
15821582 .atomic_load => try func.airAtomicLoad(inst),
15831583 .memcpy => try func.airMemcpy(inst),
1584 .memmove => try func.airMemmove(inst),
15841585 .memset => try func.airMemset(inst, false),
15851586 .memset_safe => try func.airMemset(inst, true),
15861587 .set_union_tag => try func.airSetUnionTag(inst),
......@@ -7919,6 +7920,11 @@ fn airMemcpy(func: *Func, inst: Air.Inst.Index) !void {
79197920 return func.finishAir(inst, .unreach, .{ bin_op.lhs, bin_op.rhs, .none });
79207921}
79217922
7923fn airMemmove(func: *Func, inst: Air.Inst.Index) !void {
7924 _ = inst;
7925 return func.fail("TODO implement airMemmove for riscv64", .{});
7926}
7927
79227928fn airTagName(func: *Func, inst: Air.Inst.Index) !void {
79237929 const pt = func.pt;
79247930
src/arch/sparc64/CodeGen.zig+1
......@@ -604,6 +604,7 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
604604 .atomic_rmw => try self.airAtomicRmw(inst),
605605 .atomic_load => try self.airAtomicLoad(inst),
606606 .memcpy => @panic("TODO try self.airMemcpy(inst)"),
607 .memmove => @panic("TODO try self.airMemmove(inst)"),
607608 .memset => try self.airMemset(inst, false),
608609 .memset_safe => try self.airMemset(inst, true),
609610 .set_union_tag => try self.airSetUnionTag(inst),
src/arch/wasm/CodeGen.zig+1
......@@ -2061,6 +2061,7 @@ fn genInst(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
20612061 .c_va_copy,
20622062 .c_va_end,
20632063 .c_va_start,
2064 .memmove,
20642065 => |tag| return cg.fail("TODO: Implement wasm inst: {s}", .{@tagName(tag)}),
20652066
20662067 .atomic_load => cg.airAtomicLoad(inst),
src/arch/x86_64/CodeGen.zig+6
......@@ -89453,6 +89453,7 @@ fn genBody(cg: *CodeGen, body: []const Air.Inst.Index) InnerError!void {
8945389453 .memset => try cg.airMemset(inst, false),
8945489454 .memset_safe => try cg.airMemset(inst, true),
8945589455 .memcpy => try cg.airMemcpy(inst),
89456 .memmove => try cg.airMemmove(inst),
8945689457 .cmpxchg_weak, .cmpxchg_strong => try cg.airCmpxchg(inst),
8945789458 .atomic_load => try cg.airAtomicLoad(inst),
8945889459 .atomic_store_unordered => try cg.airAtomicStore(inst, .unordered),
......@@ -106472,6 +106473,11 @@ fn airMemcpy(self: *CodeGen, inst: Air.Inst.Index) !void {
106472106473 return self.finishAir(inst, .unreach, .{ bin_op.lhs, bin_op.rhs, .none });
106473106474}
106474106475
106476fn airMemmove(self: *CodeGen, inst: Air.Inst.Index) !void {
106477 _ = inst;
106478 return self.fail("TODO implement airMemmove for {}", .{self.target.cpu.arch});
106479}
106480
106475106481fn airTagName(self: *CodeGen, inst: Air.Inst.Index, only_safety: bool) !void {
106476106482 const pt = self.pt;
106477106483 const zcu = pt.zcu;
src/codegen/c.zig+14-1
......@@ -3349,6 +3349,7 @@ fn genBodyInner(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail,
33493349 .memset => try airMemset(f, inst, false),
33503350 .memset_safe => try airMemset(f, inst, true),
33513351 .memcpy => try airMemcpy(f, inst),
3352 .memmove => try airMemmove(f, inst),
33523353 .set_union_tag => try airSetUnionTag(f, inst),
33533354 .get_union_tag => try airGetUnionTag(f, inst),
33543355 .clz => try airUnBuiltinCall(f, inst, air_datas[@intFromEnum(inst)].ty_op.operand, "clz", .bits),
......@@ -6976,6 +6977,14 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
69766977}
69776978
69786979fn airMemcpy(f: *Function, inst: Air.Inst.Index) !CValue {
6980 return copyOp(f, inst, .memcpy);
6981}
6982
6983fn airMemmove(f: *Function, inst: Air.Inst.Index) !CValue {
6984 return copyOp(f, inst, .memmove);
6985}
6986
6987fn copyOp(f: *Function, inst: Air.Inst.Index, op: enum { memcpy, memmove }) !CValue {
69796988 const pt = f.object.dg.pt;
69806989 const zcu = pt.zcu;
69816990 const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
......@@ -6990,7 +6999,11 @@ fn airMemcpy(f: *Function, inst: Air.Inst.Index) !CValue {
69906999 try writeArrayLen(f, writer, dest_ptr, dest_ty);
69917000 try writer.writeAll(" != 0) ");
69927001 }
6993 try writer.writeAll("memcpy(");
7002 const function_paren = switch (op) {
7003 .memcpy => "memcpy(",
7004 .memmove => "memmove(",
7005 };
7006 try writer.writeAll(function_paren);
69947007 try writeSliceOrPtr(f, writer, dest_ptr, dest_ty);
69957008 try writer.writeAll(", ");
69967009 try writeSliceOrPtr(f, writer, src_ptr, src_ty);
src/codegen/llvm.zig+27
......@@ -4940,6 +4940,7 @@ pub const FuncGen = struct {
49404940 .memset => try self.airMemset(inst, false),
49414941 .memset_safe => try self.airMemset(inst, true),
49424942 .memcpy => try self.airMemcpy(inst),
4943 .memmove => try self.airMemmove(inst),
49434944 .set_union_tag => try self.airSetUnionTag(inst),
49444945 .get_union_tag => try self.airGetUnionTag(inst),
49454946 .clz => try self.airClzCtz(inst, .ctlz),
......@@ -9926,6 +9927,32 @@ pub const FuncGen = struct {
99269927 return .none;
99279928 }
99289929
9930 fn airMemmove(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
9931 const o = self.ng.object;
9932 const pt = o.pt;
9933 const zcu = pt.zcu;
9934 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
9935 const dest_slice = try self.resolveInst(bin_op.lhs);
9936 const dest_ptr_ty = self.typeOf(bin_op.lhs);
9937 const src_slice = try self.resolveInst(bin_op.rhs);
9938 const src_ptr_ty = self.typeOf(bin_op.rhs);
9939 const src_ptr = try self.sliceOrArrayPtr(src_slice, src_ptr_ty);
9940 const len = try self.sliceOrArrayLenInBytes(dest_slice, dest_ptr_ty);
9941 const dest_ptr = try self.sliceOrArrayPtr(dest_slice, dest_ptr_ty);
9942 const access_kind: Builder.MemoryAccessKind = if (src_ptr_ty.isVolatilePtr(zcu) or
9943 dest_ptr_ty.isVolatilePtr(zcu)) .@"volatile" else .normal;
9944
9945 _ = try self.wip.callMemMove(
9946 dest_ptr,
9947 dest_ptr_ty.ptrAlignment(zcu).toLlvm(),
9948 src_ptr,
9949 src_ptr_ty.ptrAlignment(zcu).toLlvm(),
9950 len,
9951 access_kind,
9952 );
9953 return .none;
9954 }
9955
99299956 fn airSetUnionTag(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
99309957 const o = self.ng.object;
99319958 const pt = o.pt;
src/codegen/spirv.zig+6
......@@ -3344,6 +3344,7 @@ const NavGen = struct {
33443344 .slice => try self.airSlice(inst),
33453345 .aggregate_init => try self.airAggregateInit(inst),
33463346 .memcpy => return self.airMemcpy(inst),
3347 .memmove => return self.airMemmove(inst),
33473348
33483349 .slice_ptr => try self.airSliceField(inst, 0),
33493350 .slice_len => try self.airSliceField(inst, 1),
......@@ -4914,6 +4915,11 @@ const NavGen = struct {
49144915 });
49154916 }
49164917
4918 fn airMemmove(self: *NavGen, inst: Air.Inst.Index) !void {
4919 _ = inst;
4920 return self.fail("TODO implement airMemcpy for spirv", .{});
4921 }
4922
49174923 fn airSliceField(self: *NavGen, inst: Air.Inst.Index, field: u32) !?IdRef {
49184924 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
49194925 const field_ty = self.typeOfIndex(inst);
src/print_air.zig+1
......@@ -160,6 +160,7 @@ const Writer = struct {
160160 .cmp_gt_optimized,
161161 .cmp_neq_optimized,
162162 .memcpy,
163 .memmove,
163164 .memset,
164165 .memset_safe,
165166 => try w.writeBinOp(s, inst),
src/print_zir.zig+1
......@@ -413,6 +413,7 @@ const Writer = struct {
413413 .min,
414414 .memcpy,
415415 .memset,
416 .memmove,
416417 .elem_ptr_node,
417418 .elem_val_node,
418419 .elem_ptr,
test/behavior.zig+1
......@@ -55,6 +55,7 @@ test {
5555 _ = @import("behavior/member_func.zig");
5656 _ = @import("behavior/memcpy.zig");
5757 _ = @import("behavior/memset.zig");
58 _ = @import("behavior/memmove.zig");
5859 _ = @import("behavior/merge_error_sets.zig");
5960 _ = @import("behavior/muladd.zig");
6061 _ = @import("behavior/multiple_externs_with_conflicting_types.zig");
test/behavior/builtin_functions_returning_void_or_noreturn.zig+2
......@@ -6,6 +6,7 @@ var x: u8 = 1;
66
77// This excludes builtin functions that return void or noreturn that cannot be tested.
88test {
9 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
910 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
1011 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
1112 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
......@@ -17,6 +18,7 @@ test {
1718 try testing.expectEqual(void, @TypeOf(@breakpoint()));
1819 try testing.expectEqual({}, @export(&x, .{ .name = "x" }));
1920 try testing.expectEqual({}, @memcpy(@as([*]u8, @ptrFromInt(1))[0..0], @as([*]u8, @ptrFromInt(1))[0..0]));
21 try testing.expectEqual({}, @memmove(@as([*]u8, @ptrFromInt(1))[0..0], @as([*]u8, @ptrFromInt(1))[0..0]));
2022 try testing.expectEqual({}, @memset(@as([*]u8, @ptrFromInt(1))[0..0], undefined));
2123 try testing.expectEqual(noreturn, @TypeOf(if (true) @panic("") else {}));
2224 try testing.expectEqual({}, @prefetch(&val, .{}));
test/behavior/memmove.zig created+183
......@@ -0,0 +1,183 @@
1const std = @import("std");
2const builtin = @import("builtin");
3const expect = std.testing.expect;
4
5test "memmove and memset intrinsics" {
6 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
7 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
8 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
9 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
10 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
11 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
12 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
13
14 try testMemmoveMemset();
15 try comptime testMemmoveMemset();
16}
17
18fn testMemmoveMemset() !void {
19 var foo: [20]u8 = undefined;
20
21 @memset(foo[0..10], 'A');
22 @memset(foo[10..20], 'B');
23
24 try expect(foo[0] == 'A');
25 try expect(foo[11] == 'B');
26 try expect(foo[19] == 'B');
27
28 @memmove(foo[10..20], foo[0..10]);
29
30 try expect(foo[0] == 'A');
31 try expect(foo[11] == 'A');
32 try expect(foo[19] == 'A');
33}
34
35test "@memmove with both operands single-ptr-to-array, one is null-terminated" {
36 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
37 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
38 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
39 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest;
40 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
41 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
42 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
43
44 try testMemmoveBothSinglePtrArrayOneIsNullTerminated();
45 try comptime testMemmoveBothSinglePtrArrayOneIsNullTerminated();
46}
47
48fn testMemmoveBothSinglePtrArrayOneIsNullTerminated() !void {
49 var buf: [100]u8 = undefined;
50 const suffix = "hello";
51 @memmove(buf[buf.len - suffix.len ..], suffix);
52 try expect(buf[95] == 'h');
53 try expect(buf[96] == 'e');
54 try expect(buf[97] == 'l');
55 try expect(buf[98] == 'l');
56 try expect(buf[99] == 'o');
57
58 const start = buf.len - suffix.len - 3;
59 const end = start + suffix.len;
60 @memmove(buf[start..end], buf[buf.len - suffix.len ..]);
61 try expect(buf[92] == 'h');
62 try expect(buf[93] == 'e');
63 try expect(buf[94] == 'l');
64 try expect(buf[95] == 'l');
65 try expect(buf[96] == 'o');
66 try expect(buf[97] == 'l');
67 try expect(buf[98] == 'l');
68 try expect(buf[99] == 'o');
69
70 @memmove(buf[start + 2 .. end + 2], buf[start..end]);
71 try expect(buf[92] == 'h');
72 try expect(buf[93] == 'e');
73 try expect(buf[94] == 'h');
74 try expect(buf[95] == 'e');
75 try expect(buf[96] == 'l');
76 try expect(buf[97] == 'l');
77 try expect(buf[98] == 'o');
78 try expect(buf[99] == 'o');
79}
80
81test "@memmove dest many pointer" {
82 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
83 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
84 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
85 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest;
86 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
87 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
88 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
89
90 try testMemmoveDestManyPtr();
91 try comptime testMemmoveDestManyPtr();
92}
93
94fn testMemmoveDestManyPtr() !void {
95 var str = "hello".*;
96 var buf: [8]u8 = undefined;
97 var len: usize = 5;
98 _ = &len;
99 @memmove(@as([*]u8, @ptrCast(&buf)), @as([*]const u8, @ptrCast(&str))[0..len]);
100 try expect(buf[0] == 'h');
101 try expect(buf[1] == 'e');
102 try expect(buf[2] == 'l');
103 try expect(buf[3] == 'l');
104 try expect(buf[4] == 'o');
105 @memmove(buf[3..].ptr, buf[0..len]);
106 try expect(buf[0] == 'h');
107 try expect(buf[1] == 'e');
108 try expect(buf[2] == 'l');
109 try expect(buf[3] == 'h');
110 try expect(buf[4] == 'e');
111 try expect(buf[5] == 'l');
112 try expect(buf[6] == 'l');
113 try expect(buf[7] == 'o');
114 @memmove(buf[2..7].ptr, buf[3 .. len + 3]);
115 try expect(buf[0] == 'h');
116 try expect(buf[1] == 'e');
117 try expect(buf[2] == 'h');
118 try expect(buf[3] == 'e');
119 try expect(buf[4] == 'l');
120 try expect(buf[5] == 'l');
121 try expect(buf[6] == 'o');
122 try expect(buf[7] == 'o');
123}
124
125test "@memmove slice" {
126 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
127 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
128 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
129 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest;
130 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
131 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
132 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
133
134 try testMemmoveSlice();
135 try comptime testMemmoveSlice();
136}
137
138fn testMemmoveSlice() !void {
139 var buf: [8]u8 = undefined;
140 const dst1: []u8 = buf[0..5];
141 const dst2: []u8 = buf[3..8];
142 const dst3: []u8 = buf[2..7];
143 const src: []const u8 = "hello";
144 @memmove(dst1, src);
145 try expect(buf[0] == 'h');
146 try expect(buf[1] == 'e');
147 try expect(buf[2] == 'l');
148 try expect(buf[3] == 'l');
149 try expect(buf[4] == 'o');
150 @memmove(dst2, dst1);
151 try expect(buf[0] == 'h');
152 try expect(buf[1] == 'e');
153 try expect(buf[2] == 'l');
154 try expect(buf[3] == 'h');
155 try expect(buf[4] == 'e');
156 try expect(buf[5] == 'l');
157 try expect(buf[6] == 'l');
158 try expect(buf[7] == 'o');
159 @memmove(dst3, dst2);
160 try expect(buf[0] == 'h');
161 try expect(buf[1] == 'e');
162 try expect(buf[2] == 'h');
163 try expect(buf[3] == 'e');
164 try expect(buf[4] == 'l');
165 try expect(buf[5] == 'l');
166 try expect(buf[6] == 'o');
167 try expect(buf[7] == 'o');
168}
169
170comptime {
171 const S = struct {
172 buffer: [8]u8 = undefined,
173 fn set(self: *@This(), items: []const u8) void {
174 @memmove(self.buffer[0..items.len], items);
175 @memmove(self.buffer[3..], self.buffer[0..items.len]);
176 @memmove(self.buffer[2 .. 2 + items.len], self.buffer[3..]);
177 }
178 };
179
180 var s = S{};
181 s.set("hello");
182 if (!std.mem.eql(u8, s.buffer[0..8], "hehelloo")) @compileError("bad");
183}
test/cases/compile_errors/@memmove_type_mismatch.zig created+218
......@@ -0,0 +1,218 @@
1export fn foo() void {
2 var buf: [8]u8 = .{ 0, 1, 2, 3, 4, 5, 6, 7 };
3 const dest: []u8 = &buf;
4 const src: []align(1) u16 = @as([*]align(1) u16, @ptrCast(&buf))[0..4];
5
6 @memmove(dest, src);
7}
8
9export fn bar() void {
10 var buf: [8]u8 = .{ 0, 1, 2, 3, 4, 5, 6, 7 };
11 const dest: []u8 = &buf;
12 const src: *align(1) [8]u16 = @ptrCast(&buf);
13
14 @memmove(dest, src);
15}
16
17export fn baz() void {
18 var buf: [8]u8 = .{ 0, 1, 2, 3, 4, 5, 6, 7 };
19 const dest: []u8 = &buf;
20 const src: [*]align(1) u16 = @ptrCast(&buf);
21
22 @memmove(dest, src);
23}
24
25export fn qux() void {
26 var buf: [8]u8 = .{ 0, 1, 2, 3, 4, 5, 6, 7 };
27 const dest: *[8]u8 = &buf;
28 const src: []align(1) u16 = @as([*]align(1) u16, @ptrCast(&buf))[0..4];
29
30 @memmove(dest, src);
31}
32
33export fn quux() void {
34 var buf: [8]u8 = .{ 0, 1, 2, 3, 4, 5, 6, 7 };
35 const dest: *[8]u8 = &buf;
36 const src: *align(1) [8]u16 = @ptrCast(&buf);
37
38 @memmove(dest, src);
39}
40
41export fn quuux() void {
42 var buf: [8]u8 = .{ 0, 1, 2, 3, 4, 5, 6, 7 };
43 const dest: *[8]u8 = &buf;
44 const src: [*]align(1) u16 = @ptrCast(&buf);
45
46 @memmove(dest, src);
47}
48
49export fn foo2() void {
50 var buf: [8]u8 = .{ 0, 1, 2, 3, 4, 5, 6, 7 };
51 const dest: []align(1) u16 = @as([*]align(1) u16, @ptrCast(&buf))[0..4];
52 const src: []u8 = &buf;
53
54 @memmove(dest, src);
55}
56
57export fn bar2() void {
58 var buf: [8]u8 = .{ 0, 1, 2, 3, 4, 5, 6, 7 };
59 const dest: *align(1) [8]u16 = @ptrCast(&buf);
60 const src: []u8 = &buf;
61
62 @memmove(dest, src);
63}
64
65export fn baz2() void {
66 var buf: [8]u8 = .{ 0, 1, 2, 3, 4, 5, 6, 7 };
67 const dest: [*]align(1) u16 = @ptrCast(&buf);
68 const src: []u8 = &buf;
69
70 @memmove(dest, src);
71}
72
73export fn qux2() void {
74 var buf: [8]u8 = .{ 0, 1, 2, 3, 4, 5, 6, 7 };
75 const dest: []align(1) u16 = @as([*]align(1) u16, @ptrCast(&buf))[0..4];
76 const src: *[8]u8 = &buf;
77
78 @memmove(dest, src);
79}
80
81export fn quux2() void {
82 var buf: [8]u8 = .{ 0, 1, 2, 3, 4, 5, 6, 7 };
83 const dest: *align(1) [8]u16 = @ptrCast(&buf);
84 const src: *[8]u8 = &buf;
85
86 @memmove(dest, src);
87}
88
89export fn quuux2() void {
90 var buf: [8]u8 = .{ 0, 1, 2, 3, 4, 5, 6, 7 };
91 const dest: [*]align(1) u16 = @ptrCast(&buf);
92 const src: *[8]u8 = &buf;
93
94 @memmove(dest, src);
95}
96
97comptime {
98 var buf: [8]u8 = .{ 0, 1, 2, 3, 4, 5, 6, 7 };
99 const dest: []u8 = &buf;
100 const src: []align(1) u16 = @as([*]align(1) u16, @ptrCast(&buf))[0..4];
101 @memmove(dest, src);
102}
103
104comptime {
105 var buf: [8]u8 = .{ 0, 1, 2, 3, 4, 5, 6, 7 };
106 const dest: []u8 = &buf;
107 const src: *align(1) [8]u16 = @ptrCast(&buf);
108 @memmove(dest, src);
109}
110
111comptime {
112 var buf: [8]u8 = .{ 0, 1, 2, 3, 4, 5, 6, 7 };
113 const dest: []u8 = &buf;
114 const src: [*]align(1) u16 = @ptrCast(&buf);
115 @memmove(dest, src);
116}
117
118comptime {
119 var buf: [8]u8 = .{ 0, 1, 2, 3, 4, 5, 6, 7 };
120 const dest: *[8]u8 = &buf;
121 const src: []align(1) u16 = @as([*]align(1) u16, @ptrCast(&buf))[0..4];
122 @memmove(dest, src);
123}
124
125comptime {
126 var buf: [8]u8 = .{ 0, 1, 2, 3, 4, 5, 6, 7 };
127 const dest: *[8]u8 = &buf;
128 const src: *align(1) [8]u16 = @ptrCast(&buf);
129 @memmove(dest, src);
130}
131
132comptime {
133 var buf: [8]u8 = .{ 0, 1, 2, 3, 4, 5, 6, 7 };
134 const dest: *[8]u8 = &buf;
135 const src: [*]align(1) u16 = @ptrCast(&buf);
136 @memmove(dest, src);
137}
138
139comptime {
140 var buf: [8]u8 = .{ 0, 1, 2, 3, 4, 5, 6, 7 };
141 const dest: []align(1) u16 = @as([*]align(1) u16, @ptrCast(&buf))[0..4];
142 const src: []u8 = &buf;
143 @memmove(dest, src);
144}
145
146comptime {
147 var buf: [8]u8 = .{ 0, 1, 2, 3, 4, 5, 6, 7 };
148 const dest: *align(1) [8]u16 = @ptrCast(&buf);
149 const src: []u8 = &buf;
150 @memmove(dest, src);
151}
152
153comptime {
154 var buf: [8]u8 = .{ 0, 1, 2, 3, 4, 5, 6, 7 };
155 const dest: [*]align(1) u16 = @ptrCast(&buf);
156 const src: []u8 = &buf;
157 @memmove(dest, src);
158}
159
160comptime {
161 var buf: [8]u8 = .{ 0, 1, 2, 3, 4, 5, 6, 7 };
162 const dest: []align(1) u16 = @as([*]align(1) u16, @ptrCast(&buf))[0..4];
163 const src: *[8]u8 = &buf;
164 @memmove(dest, src);
165}
166
167comptime {
168 var buf: [8]u8 = .{ 0, 1, 2, 3, 4, 5, 6, 7 };
169 const dest: *align(1) [8]u16 = @ptrCast(&buf);
170 const src: *[8]u8 = &buf;
171 @memmove(dest, src);
172}
173
174comptime {
175 var buf: [8]u8 = .{ 0, 1, 2, 3, 4, 5, 6, 7 };
176 const dest: [*]align(1) u16 = @ptrCast(&buf);
177 const src: *[8]u8 = &buf;
178 @memmove(dest, src);
179}
180
181// error
182//
183// :6:5: error: pointer element type 'u16' cannot coerce into element type 'u8'
184// :6:5: note: unsigned 8-bit int cannot represent all possible unsigned 16-bit values
185// :14:5: error: pointer element type 'u16' cannot coerce into element type 'u8'
186// :14:5: note: unsigned 8-bit int cannot represent all possible unsigned 16-bit values
187// :22:5: error: pointer element type 'u16' cannot coerce into element type 'u8'
188// :22:5: note: unsigned 8-bit int cannot represent all possible unsigned 16-bit values
189// :30:5: error: pointer element type 'u16' cannot coerce into element type 'u8'
190// :30:5: note: unsigned 8-bit int cannot represent all possible unsigned 16-bit values
191// :38:5: error: pointer element type 'u16' cannot coerce into element type 'u8'
192// :38:5: note: unsigned 8-bit int cannot represent all possible unsigned 16-bit values
193// :46:5: error: pointer element type 'u16' cannot coerce into element type 'u8'
194// :46:5: note: unsigned 8-bit int cannot represent all possible unsigned 16-bit values
195// :54:5: error: pointer element type 'u8' cannot coerce into element type 'u16'
196// :62:5: error: pointer element type 'u8' cannot coerce into element type 'u16'
197// :70:5: error: pointer element type 'u8' cannot coerce into element type 'u16'
198// :78:5: error: pointer element type 'u8' cannot coerce into element type 'u16'
199// :86:5: error: pointer element type 'u8' cannot coerce into element type 'u16'
200// :94:5: error: pointer element type 'u8' cannot coerce into element type 'u16'
201// :101:5: error: pointer element type 'u16' cannot coerce into element type 'u8'
202// :101:5: note: unsigned 8-bit int cannot represent all possible unsigned 16-bit values
203// :108:5: error: pointer element type 'u16' cannot coerce into element type 'u8'
204// :108:5: note: unsigned 8-bit int cannot represent all possible unsigned 16-bit values
205// :115:5: error: pointer element type 'u16' cannot coerce into element type 'u8'
206// :115:5: note: unsigned 8-bit int cannot represent all possible unsigned 16-bit values
207// :122:5: error: pointer element type 'u16' cannot coerce into element type 'u8'
208// :122:5: note: unsigned 8-bit int cannot represent all possible unsigned 16-bit values
209// :129:5: error: pointer element type 'u16' cannot coerce into element type 'u8'
210// :129:5: note: unsigned 8-bit int cannot represent all possible unsigned 16-bit values
211// :136:5: error: pointer element type 'u16' cannot coerce into element type 'u8'
212// :136:5: note: unsigned 8-bit int cannot represent all possible unsigned 16-bit values
213// :143:5: error: pointer element type 'u8' cannot coerce into element type 'u16'
214// :150:5: error: pointer element type 'u8' cannot coerce into element type 'u16'
215// :157:5: error: pointer element type 'u8' cannot coerce into element type 'u16'
216// :164:5: error: pointer element type 'u8' cannot coerce into element type 'u16'
217// :171:5: error: pointer element type 'u8' cannot coerce into element type 'u16'
218// :178:5: error: pointer element type 'u8' cannot coerce into element type 'u16'
test/cases/compile_errors/bad_panic_call_signature.zig+1
......@@ -29,6 +29,7 @@ pub const panic = struct {
2929 pub const forLenMismatch = simple_panic.forLenMismatch;
3030 pub const memcpyLenMismatch = simple_panic.memcpyLenMismatch;
3131 pub const memcpyAlias = simple_panic.memcpyAlias;
32 pub const memmoveLenMismatch = simple_panic.memmoveLenMismatch;
3233 pub const noreturnReturned = simple_panic.noreturnReturned;
3334};
3435
test/cases/compile_errors/bad_panic_generic_signature.zig+1
......@@ -25,6 +25,7 @@ pub const panic = struct {
2525 pub const forLenMismatch = simple_panic.forLenMismatch;
2626 pub const memcpyLenMismatch = simple_panic.memcpyLenMismatch;
2727 pub const memcpyAlias = simple_panic.memcpyAlias;
28 pub const memmoveLenMismatch = simple_panic.memmoveLenMismatch;
2829 pub const noreturnReturned = simple_panic.noreturnReturned;
2930};
3031
test/cases/compile_errors/comptime_var_referenced_at_runtime.zig+11
......@@ -63,6 +63,14 @@ export fn far() void {
6363 @memset(&rt, elem);
6464}
6565
66export fn bax() void {
67 comptime var x: [2]u32 = undefined;
68 x = .{ 1, 2 };
69
70 var rt: [2]u32 = undefined;
71 @memmove(&rt, &x);
72}
73
6674// error
6775//
6876// :5:19: error: runtime value contains reference to comptime var
......@@ -92,3 +100,6 @@ export fn far() void {
92100// :63:18: error: runtime value contains reference to comptime var
93101// :63:18: note: comptime var pointers are not available at runtime
94102// :59:27: note: 'runtime_value' points to comptime var declared here
103// :71:19: error: runtime value contains reference to comptime var
104// :71:19: note: comptime var pointers are not available at runtime
105// :67:30: note: 'runtime_value' points to comptime var declared here
test/cases/compile_errors/incorrect_type_to_memset_memcpy.zig+43-1
......@@ -28,10 +28,39 @@ pub export fn memcpy_const_dest_ptr() void {
2828 var buf2: [5]u8 = .{ 1, 2, 3, 4, 5 };
2929 @memcpy(&buf1, &buf2);
3030}
31pub export fn memset_array() void {
31pub export fn memcpy_array() void {
3232 const buf: [5]u8 = .{ 1, 2, 3, 4, 5 };
3333 @memcpy(buf, 1);
3434}
35pub export fn entry_memmove() void {
36 var buf: [5]u8 = .{ 1, 2, 3, 4, 5 };
37 const slice: []u8 = &buf;
38 const a: u32 = 1234;
39 @memmove(slice.ptr, @as([*]const u8, @ptrCast(&a)));
40}
41pub export fn entry1_memmove() void {
42 var buf: [5]u8 = .{ 1, 2, 3, 4, 5 };
43 const ptr: *u8 = &buf[0];
44 @memmove(ptr, 0);
45}
46pub export fn non_matching_lengths_memmove() void {
47 var buf1: [5]u8 = .{ 1, 2, 3, 4, 5 };
48 var buf2: [6]u8 = .{ 1, 2, 3, 4, 5, 6 };
49 @memmove(&buf2, &buf1);
50}
51pub export fn memcpy_const_dest_ptr_memmove() void {
52 const buf1: [5]u8 = .{ 1, 2, 3, 4, 5 };
53 var buf2: [5]u8 = .{ 1, 2, 3, 4, 5 };
54 @memmove(&buf1, &buf2);
55}
56pub export fn memmove_array() void {
57 const buf: [5]u8 = .{ 1, 2, 3, 4, 5 };
58 @memmove(buf, 1);
59}
60pub export fn memset_array() void {
61 const buf: [5]u8 = .{ 1, 2, 3, 4, 5 };
62 @memset(buf, 1);
63}
3564
3665// error
3766// backend=stage2
......@@ -51,3 +80,16 @@ pub export fn memset_array() void {
5180// :29:13: error: cannot memcpy to constant pointer
5281// :33:13: error: type '[5]u8' is not an indexable pointer
5382// :33:13: note: operand must be a slice, a many pointer or a pointer to an array
83// :39:5: error: unknown @memmove length
84// :39:19: note: destination type '[*]u8' provides no length
85// :39:25: note: source type '[*]const u8' provides no length
86// :44:14: error: type '*u8' is not an indexable pointer
87// :44:14: note: operand must be a slice, a many pointer or a pointer to an array
88// :49:5: error: non-matching @memmove lengths
89// :49:14: note: length 6 here
90// :49:21: note: length 5 here
91// :54:14: error: cannot memmove to constant pointer
92// :58:14: error: type '[5]u8' is not an indexable pointer
93// :58:14: note: operand must be a slice, a many pointer or a pointer to an array
94// :62:13: error: type '[5]u8' is not an indexable pointer
95// :62:13: note: operand must be a slice, a many pointer or a pointer to an array
test/cases/safety/memcpy_alias.zig+1
......@@ -12,6 +12,7 @@ pub fn main() !void {
1212 var len: usize = 5;
1313 _ = &len;
1414 @memcpy(buffer[0..len], buffer[4 .. 4 + len]);
15 return error.TestFailed;
1516}
1617// run
1718// backend=stage2,llvm
test/cases/safety/memcpy_len_mismatch.zig+1
......@@ -12,6 +12,7 @@ pub fn main() !void {
1212 var len: usize = 5;
1313 _ = &len;
1414 @memcpy(buffer[0..len], buffer[len .. len + 4]);
15 return error.TestFailed;
1516}
1617// run
1718// backend=stage2,llvm
test/cases/safety/memmove_len_mismatch.zig created+19
......@@ -0,0 +1,19 @@
1const std = @import("std");
2
3pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace, _: ?usize) noreturn {
4 _ = stack_trace;
5 if (std.mem.eql(u8, message, "@memmove arguments have non-equal lengths")) {
6 std.process.exit(0);
7 }
8 std.process.exit(1);
9}
10pub fn main() !void {
11 var buffer = [2]u8{ 1, 2 } ** 5;
12 var len: usize = 5;
13 _ = &len;
14 @memmove(buffer[0..len], buffer[len .. len + 4]);
15 return error.TestFailed;
16}
17// run
18// backend=llvm
19// target=native
test/incremental/change_panic_handler_explicit+3
......@@ -39,6 +39,7 @@ pub const panic = struct {
3939 pub const forLenMismatch = no_panic.forLenMismatch;
4040 pub const memcpyLenMismatch = no_panic.memcpyLenMismatch;
4141 pub const memcpyAlias = no_panic.memcpyAlias;
42 pub const memmoveLenMismatch = no_panic.memmoveLenMismatch;
4243 pub const noreturnReturned = no_panic.noreturnReturned;
4344};
4445fn myPanic(msg: []const u8, _: ?usize) noreturn {
......@@ -86,6 +87,7 @@ pub const panic = struct {
8687 pub const forLenMismatch = no_panic.forLenMismatch;
8788 pub const memcpyLenMismatch = no_panic.memcpyLenMismatch;
8889 pub const memcpyAlias = no_panic.memcpyAlias;
90 pub const memmoveLenMismatch = no_panic.memmoveLenMismatch;
8991 pub const noreturnReturned = no_panic.noreturnReturned;
9092};
9193fn myPanic(msg: []const u8, _: ?usize) noreturn {
......@@ -133,6 +135,7 @@ pub const panic = struct {
133135 pub const forLenMismatch = no_panic.forLenMismatch;
134136 pub const memcpyLenMismatch = no_panic.memcpyLenMismatch;
135137 pub const memcpyAlias = no_panic.memcpyAlias;
138 pub const memmoveLenMismatch = no_panic.memmoveLenMismatch;
136139 pub const noreturnReturned = no_panic.noreturnReturned;
137140};
138141fn myPanicNew(msg: []const u8, _: ?usize) noreturn {
test/standalone/zerolength_check/src/main.zig+1
......@@ -5,6 +5,7 @@ test {
55 const source = foo();
66
77 @memcpy(dest, source);
8 @memmove(dest, source);
89 @memset(dest, 4);
910 @memset(dest, undefined);
1011