motiejus/zig

fork of https://codeberg.org/ziglang/zig
git clone https://git.jakstys.lt/motiejus/zig.git
Log | Tree | Refs | README | LICENSE

commit b82aec5a15acd3d10f2efbde52a0bfb9af411891 (tree)
parent b5b507a7425d2cf6b09c16db96fa10c3d13f7799
Author: Jacob Young <jacobly0@users.noreply.github.com>
Date:   Tue, 15 Nov 2022 23:18:00 -0500

cbe: fix indexing with a zero-bit element type

Fixes void dereference warnings on gcc 11.3.0.

Diffstat:
Msrc/codegen/c.zig | 22++++++++++++++++------
1 file changed, 16 insertions(+), 6 deletions(-)

diff --git a/src/codegen/c.zig b/src/codegen/c.zig @@ -2658,12 +2658,14 @@ fn airPtrElemPtr(f: *Function, inst: Air.Inst.Index) !CValue { const ty_pl = f.air.instructions.items(.data)[inst].ty_pl; const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data; const ptr_ty = f.air.typeOf(bin_op.lhs); + const child_ty = ptr_ty.childType(); const ptr = try f.resolveInst(bin_op.lhs); + if (!child_ty.hasRuntimeBitsIgnoreComptime()) return ptr; const index = try f.resolveInst(bin_op.rhs); + const writer = f.object.writer(); const local = try f.allocLocal(f.air.typeOfIndex(inst), .Const); - try writer.writeAll(" = &("); if (ptr_ty.ptrSize() == .One) { // 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 { const ty_pl = f.air.instructions.items(.data)[inst].ty_pl; const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data; + const slice_ty = f.air.typeOf(bin_op.lhs); + const child_ty = slice_ty.elemType2(); const slice = try f.resolveInst(bin_op.lhs); - const index = try f.resolveInst(bin_op.rhs); + const writer = f.object.writer(); const local = try f.allocLocal(f.air.typeOfIndex(inst), .Const); - try writer.writeAll(" = &"); + try writer.writeAll(" = "); + if (child_ty.hasRuntimeBitsIgnoreComptime()) try writer.writeByte('&'); try f.writeCValue(writer, slice, .Other); - try writer.writeAll(".ptr["); - try f.writeCValue(writer, index, .Other); - try writer.writeAll("];\n"); + try writer.writeAll(".ptr"); + if (child_ty.hasRuntimeBitsIgnoreComptime()) { + const index = try f.resolveInst(bin_op.rhs); + try writer.writeByte('['); + try f.writeCValue(writer, index, .Other); + try writer.writeByte(']'); + } + try writer.writeAll(";\n"); return local; }