authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-04-21 20:11:43-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-04-25 11:23:40-07:00
log92186b8c137d14917dc058f228be351f658c1e7e
treed1acb60dbc0ad9709b2399c572d4f786dca3598a
parentedb5e493e6d9a4478b3d9c06aa590694757d8c03

C backend: implement new memcpy and inttoptr semantics


1 files changed, 44 insertions(+), 16 deletions(-)

src/codegen/c.zig+44-16
......@@ -5774,6 +5774,7 @@ fn airPtrToInt(f: *Function, inst: Air.Inst.Index) !CValue {
57745774 const un_op = f.air.instructions.items(.data)[inst].un_op;
57755775
57765776 const operand = try f.resolveInst(un_op);
5777 const operand_ty = f.air.typeOf(un_op);
57775778 try reap(f, inst, &.{un_op});
57785779 const inst_ty = f.air.typeOfIndex(inst);
57795780 const writer = f.object.writer();
......@@ -5783,7 +5784,11 @@ fn airPtrToInt(f: *Function, inst: Air.Inst.Index) !CValue {
57835784 try writer.writeAll(" = (");
57845785 try f.renderType(writer, inst_ty);
57855786 try writer.writeByte(')');
5786 try f.writeCValue(writer, operand, .Other);
5787 if (operand_ty.isSlice()) {
5788 try f.writeCValueMember(writer, operand, .{ .identifier = "len" });
5789 } else {
5790 try f.writeCValue(writer, operand, .Other);
5791 }
57875792 try writer.writeAll(";\n");
57885793 return local;
57895794}
......@@ -6176,6 +6181,14 @@ fn airAtomicStore(f: *Function, inst: Air.Inst.Index, order: [*:0]const u8) !CVa
61766181 return .none;
61776182}
61786183
6184fn writeSliceOrPtr(f: *Function, writer: anytype, ptr: CValue, ptr_ty: Type) !void {
6185 if (ptr_ty.isSlice()) {
6186 try f.writeCValueMember(writer, ptr, .{ .identifier = "ptr" });
6187 } else {
6188 try f.writeCValue(writer, ptr, .FunctionArgument);
6189 }
6190}
6191
61796192fn airMemset(f: *Function, inst: Air.Inst.Index) !CValue {
61806193 const bin_op = f.air.instructions.items(.data)[inst].bin_op;
61816194 const dest_ty = f.air.typeOf(bin_op.lhs);
......@@ -6239,11 +6252,7 @@ fn airMemset(f: *Function, inst: Air.Inst.Index) !CValue {
62396252 try writer.writeAll(" += ");
62406253 try f.object.dg.renderValue(writer, Type.usize, Value.one, .Other);
62416254 try writer.writeAll(") (");
6242 switch (dest_ty.ptrSize()) {
6243 .Slice => try f.writeCValueMember(writer, dest_slice, .{ .identifier = "ptr" }),
6244 .One => try f.writeCValue(writer, dest_slice, .FunctionArgument),
6245 .Many, .C => unreachable,
6246 }
6255 try writeSliceOrPtr(f, writer, dest_slice, dest_ty);
62476256 try writer.writeAll(")[");
62486257 try f.writeCValue(writer, index, .Other);
62496258 try writer.writeAll("] = ");
......@@ -6282,22 +6291,41 @@ fn airMemset(f: *Function, inst: Air.Inst.Index) !CValue {
62826291}
62836292
62846293fn airMemcpy(f: *Function, inst: Air.Inst.Index) !CValue {
6285 const pl_op = f.air.instructions.items(.data)[inst].pl_op;
6286 const extra = f.air.extraData(Air.Bin, pl_op.payload).data;
6287 const dest_ptr = try f.resolveInst(pl_op.operand);
6288 const src_ptr = try f.resolveInst(extra.lhs);
6289 const len = try f.resolveInst(extra.rhs);
6290 try reap(f, inst, &.{ pl_op.operand, extra.lhs, extra.rhs });
6294 const bin_op = f.air.instructions.items(.data)[inst].bin_op;
6295 const dest_ptr = try f.resolveInst(bin_op.lhs);
6296 const src_ptr = try f.resolveInst(bin_op.rhs);
6297 const dest_ty = f.air.typeOf(bin_op.lhs);
6298 const src_ty = f.air.typeOf(bin_op.rhs);
6299 const target = f.object.dg.module.getTarget();
62916300 const writer = f.object.writer();
62926301
62936302 try writer.writeAll("memcpy(");
6294 try f.writeCValue(writer, dest_ptr, .FunctionArgument);
6303 try writeSliceOrPtr(f, writer, dest_ptr, dest_ty);
62956304 try writer.writeAll(", ");
6296 try f.writeCValue(writer, src_ptr, .FunctionArgument);
6305 try writeSliceOrPtr(f, writer, src_ptr, src_ty);
62976306 try writer.writeAll(", ");
6298 try f.writeCValue(writer, len, .FunctionArgument);
6299 try writer.writeAll(");\n");
6307 switch (dest_ty.ptrSize()) {
6308 .Slice => {
6309 const elem_ty = dest_ty.childType();
6310 const elem_abi_size = elem_ty.abiSize(target);
6311 try f.writeCValueMember(writer, dest_ptr, .{ .identifier = "len" });
6312 if (elem_abi_size > 1) {
6313 try writer.print(" * {d});\n", .{elem_abi_size});
6314 } else {
6315 try writer.writeAll(");\n");
6316 }
6317 },
6318 .One => {
6319 const array_ty = dest_ty.childType();
6320 const elem_ty = array_ty.childType();
6321 const elem_abi_size = elem_ty.abiSize(target);
6322 const len = array_ty.arrayLen() * elem_abi_size;
6323 try writer.print("{d});\n", .{len});
6324 },
6325 .Many, .C => unreachable,
6326 }
63006327
6328 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });
63016329 return .none;
63026330}
63036331