authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2022-11-15 23:18:00-05:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2022-11-15 23:33:48-05:00
logb82aec5a15acd3d10f2efbde52a0bfb9af411891
treee39b571ce3628601de2381908e6abbd4a17765f1
parentb5b507a7425d2cf6b09c16db96fa10c3d13f7799

cbe: fix indexing with a zero-bit element type

Fixes void dereference warnings on gcc 11.3.0.

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

src/codegen/c.zig+16-6
......@@ -2658,12 +2658,14 @@ fn airPtrElemPtr(f: *Function, inst: Air.Inst.Index) !CValue {
26582658 const ty_pl = f.air.instructions.items(.data)[inst].ty_pl;
26592659 const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data;
26602660 const ptr_ty = f.air.typeOf(bin_op.lhs);
2661 const child_ty = ptr_ty.childType();
26612662
26622663 const ptr = try f.resolveInst(bin_op.lhs);
2664 if (!child_ty.hasRuntimeBitsIgnoreComptime()) return ptr;
26632665 const index = try f.resolveInst(bin_op.rhs);
2666
26642667 const writer = f.object.writer();
26652668 const local = try f.allocLocal(f.air.typeOfIndex(inst), .Const);
2666
26672669 try writer.writeAll(" = &(");
26682670 if (ptr_ty.ptrSize() == .One) {
26692671 // It's a pointer to an array, so we need to de-reference.
......@@ -2717,15 +2719,23 @@ fn airSliceElemPtr(f: *Function, inst: Air.Inst.Index) !CValue {
27172719 const ty_pl = f.air.instructions.items(.data)[inst].ty_pl;
27182720 const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data;
27192721
2722 const slice_ty = f.air.typeOf(bin_op.lhs);
2723 const child_ty = slice_ty.elemType2();
27202724 const slice = try f.resolveInst(bin_op.lhs);
2721 const index = try f.resolveInst(bin_op.rhs);
2725
27222726 const writer = f.object.writer();
27232727 const local = try f.allocLocal(f.air.typeOfIndex(inst), .Const);
2724 try writer.writeAll(" = &");
2728 try writer.writeAll(" = ");
2729 if (child_ty.hasRuntimeBitsIgnoreComptime()) try writer.writeByte('&');
27252730 try f.writeCValue(writer, slice, .Other);
2726 try writer.writeAll(".ptr[");
2727 try f.writeCValue(writer, index, .Other);
2728 try writer.writeAll("];\n");
2731 try writer.writeAll(".ptr");
2732 if (child_ty.hasRuntimeBitsIgnoreComptime()) {
2733 const index = try f.resolveInst(bin_op.rhs);
2734 try writer.writeByte('[');
2735 try f.writeCValue(writer, index, .Other);
2736 try writer.writeByte(']');
2737 }
2738 try writer.writeAll(";\n");
27292739 return local;
27302740}
27312741