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 {...@@ -5774,6 +5774,7 @@ fn airPtrToInt(f: *Function, inst: Air.Inst.Index) !CValue {
5774 const un_op = f.air.instructions.items(.data)[inst].un_op;5774 const un_op = f.air.instructions.items(.data)[inst].un_op;
57755775
5776 const operand = try f.resolveInst(un_op);5776 const operand = try f.resolveInst(un_op);
5777 const operand_ty = f.air.typeOf(un_op);
5777 try reap(f, inst, &.{un_op});5778 try reap(f, inst, &.{un_op});
5778 const inst_ty = f.air.typeOfIndex(inst);5779 const inst_ty = f.air.typeOfIndex(inst);
5779 const writer = f.object.writer();5780 const writer = f.object.writer();
...@@ -5783,7 +5784,11 @@ fn airPtrToInt(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -5783,7 +5784,11 @@ fn airPtrToInt(f: *Function, inst: Air.Inst.Index) !CValue {
5783 try writer.writeAll(" = (");5784 try writer.writeAll(" = (");
5784 try f.renderType(writer, inst_ty);5785 try f.renderType(writer, inst_ty);
5785 try writer.writeByte(')');5786 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 }
5787 try writer.writeAll(";\n");5792 try writer.writeAll(";\n");
5788 return local;5793 return local;
5789}5794}
...@@ -6176,6 +6181,14 @@ fn airAtomicStore(f: *Function, inst: Air.Inst.Index, order: [*:0]const u8) !CVa...@@ -6176,6 +6181,14 @@ fn airAtomicStore(f: *Function, inst: Air.Inst.Index, order: [*:0]const u8) !CVa
6176 return .none;6181 return .none;
6177}6182}
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
6179fn airMemset(f: *Function, inst: Air.Inst.Index) !CValue {6192fn airMemset(f: *Function, inst: Air.Inst.Index) !CValue {
6180 const bin_op = f.air.instructions.items(.data)[inst].bin_op;6193 const bin_op = f.air.instructions.items(.data)[inst].bin_op;
6181 const dest_ty = f.air.typeOf(bin_op.lhs);6194 const dest_ty = f.air.typeOf(bin_op.lhs);
...@@ -6239,11 +6252,7 @@ fn airMemset(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -6239,11 +6252,7 @@ fn airMemset(f: *Function, inst: Air.Inst.Index) !CValue {
6239 try writer.writeAll(" += ");6252 try writer.writeAll(" += ");
6240 try f.object.dg.renderValue(writer, Type.usize, Value.one, .Other);6253 try f.object.dg.renderValue(writer, Type.usize, Value.one, .Other);
6241 try writer.writeAll(") (");6254 try writer.writeAll(") (");
6242 switch (dest_ty.ptrSize()) {6255 try writeSliceOrPtr(f, writer, dest_slice, dest_ty);
6243 .Slice => try f.writeCValueMember(writer, dest_slice, .{ .identifier = "ptr" }),
6244 .One => try f.writeCValue(writer, dest_slice, .FunctionArgument),
6245 .Many, .C => unreachable,
6246 }
6247 try writer.writeAll(")[");6256 try writer.writeAll(")[");
6248 try f.writeCValue(writer, index, .Other);6257 try f.writeCValue(writer, index, .Other);
6249 try writer.writeAll("] = ");6258 try writer.writeAll("] = ");
...@@ -6282,22 +6291,41 @@ fn airMemset(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -6282,22 +6291,41 @@ fn airMemset(f: *Function, inst: Air.Inst.Index) !CValue {
6282}6291}
62836292
6284fn airMemcpy(f: *Function, inst: Air.Inst.Index) !CValue {6293fn airMemcpy(f: *Function, inst: Air.Inst.Index) !CValue {
6285 const pl_op = f.air.instructions.items(.data)[inst].pl_op;6294 const bin_op = f.air.instructions.items(.data)[inst].bin_op;
6286 const extra = f.air.extraData(Air.Bin, pl_op.payload).data;6295 const dest_ptr = try f.resolveInst(bin_op.lhs);
6287 const dest_ptr = try f.resolveInst(pl_op.operand);6296 const src_ptr = try f.resolveInst(bin_op.rhs);
6288 const src_ptr = try f.resolveInst(extra.lhs);6297 const dest_ty = f.air.typeOf(bin_op.lhs);
6289 const len = try f.resolveInst(extra.rhs);6298 const src_ty = f.air.typeOf(bin_op.rhs);
6290 try reap(f, inst, &.{ pl_op.operand, extra.lhs, extra.rhs });6299 const target = f.object.dg.module.getTarget();
6291 const writer = f.object.writer();6300 const writer = f.object.writer();
62926301
6293 try writer.writeAll("memcpy(");6302 try writer.writeAll("memcpy(");
6294 try f.writeCValue(writer, dest_ptr, .FunctionArgument);6303 try writeSliceOrPtr(f, writer, dest_ptr, dest_ty);
6295 try writer.writeAll(", ");6304 try writer.writeAll(", ");
6296 try f.writeCValue(writer, src_ptr, .FunctionArgument);6305 try writeSliceOrPtr(f, writer, src_ptr, src_ty);
6297 try writer.writeAll(", ");6306 try writer.writeAll(", ");
6298 try f.writeCValue(writer, len, .FunctionArgument);6307 switch (dest_ty.ptrSize()) {
6299 try writer.writeAll(");\n");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 });
6301 return .none;6329 return .none;
6302}6330}
63036331