authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-12-03 14:25:42-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-12-04 15:57:40-07:00
logda73410e7ff41fe1f196236ca405f225650a0726
treedb2f754af8f048fc7a85fa7b9989df764b21044b
parentdb1819e8ed02d3bb230b82f875b45fe7a6274c49

CBE: avoid curly inits because they don't work in assignments


1 files changed, 57 insertions(+), 50 deletions(-)

src/codegen/c.zig+57-50
......@@ -3843,6 +3843,8 @@ fn airCall(
38433843 inst: Air.Inst.Index,
38443844 modifier: std.builtin.CallOptions.Modifier,
38453845) !CValue {
3846 // Not even allowed to call panic in a naked function.
3847 if (f.object.dg.decl.ty.fnCallingConvention() == .Naked) return .none;
38463848 const gpa = f.object.dg.gpa;
38473849
38483850 switch (modifier) {
......@@ -5497,7 +5499,7 @@ fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue {
54975499 try f.writeCValue(writer, local, .Other);
54985500 const array_len = f.air.typeOf(ty_op.operand).elemType().arrayLen();
54995501
5500 try writer.writeAll(" = { .ptr = ");
5502 try writer.writeAll(".ptr = ");
55015503 if (operand == .undef) {
55025504 // Unfortunately, C does not support any equivalent to
55035505 // &(*(void *)p)[0], although LLVM does via GetElementPtr
......@@ -5511,7 +5513,9 @@ fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue {
55115513
55125514 var len_pl: Value.Payload.U64 = .{ .base = .{ .tag = .int_u64 }, .data = array_len };
55135515 const len_val = Value.initPayload(&len_pl.base);
5514 try writer.print(", .len = {} }};\n", .{try f.fmtIntLiteral(Type.usize, len_val)});
5516 try writer.writeAll("; ");
5517 try f.writeCValue(writer, local, .Other);
5518 try writer.print(".len = {};\n", .{try f.fmtIntLiteral(Type.usize, len_val)});
55155519 return local;
55165520}
55175521
......@@ -5687,59 +5691,62 @@ fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue
56875691 const ty_pl = f.air.instructions.items(.data)[inst].ty_pl;
56885692 const extra = f.air.extraData(Air.Cmpxchg, ty_pl.payload).data;
56895693 const inst_ty = f.air.typeOfIndex(inst);
5690 const is_struct = !inst_ty.isPtrLikeOptional();
5691 const ptr_ty = f.air.typeOf(extra.ptr);
56925694 const ptr = try f.resolveInst(extra.ptr);
56935695 const expected_value = try f.resolveInst(extra.expected_value);
56945696 const new_value = try f.resolveInst(extra.new_value);
56955697 try reap(f, inst, &.{ extra.ptr, extra.expected_value, extra.new_value });
56965698 const writer = f.object.writer();
5697
5699 const ptr_ty = f.air.typeOf(extra.ptr);
56985700 const local = try f.allocLocal(inst, inst_ty);
5699 try f.writeCValue(writer, local, .Other);
5700 try writer.writeAll(" = ");
5701 if (is_struct) try writer.writeAll("{ .payload = ");
5702 try f.writeCValue(writer, expected_value, .Initializer);
5703 if (is_struct) {
5704 try writer.writeAll(", .is_null = ");
5705 try f.object.dg.renderValue(writer, Type.bool, Value.false, .Initializer);
5706 try writer.writeAll(" }");
5707 }
5708 try writer.writeAll(";\n");
5709
5710 if (is_struct) {
5701 if (inst_ty.isPtrLikeOptional()) {
57115702 try f.writeCValue(writer, local, .Other);
5712 try writer.writeAll(".is_null = ");
5713 } else {
5703 try writer.writeAll(" = ");
5704 try f.writeCValue(writer, expected_value, .Initializer);
5705 try writer.writeAll(";\n");
57145706 try writer.writeAll("if (");
5715 }
5716 try writer.print("zig_cmpxchg_{s}((zig_atomic(", .{flavor});
5717 try f.renderTypecast(writer, ptr_ty.elemType());
5718 try writer.writeByte(')');
5719 if (ptr_ty.isVolatilePtr()) try writer.writeAll(" volatile");
5720 try writer.writeAll(" *)");
5721 try f.writeCValue(writer, ptr, .Other);
5722 try writer.writeAll(", ");
5723 if (is_struct)
5724 try f.writeCValueMember(writer, local, .{ .identifier = "payload" })
5725 else
5707 try writer.print("zig_cmpxchg_{s}((zig_atomic(", .{flavor});
5708 try f.renderTypecast(writer, ptr_ty.elemType());
5709 try writer.writeByte(')');
5710 if (ptr_ty.isVolatilePtr()) try writer.writeAll(" volatile");
5711 try writer.writeAll(" *)");
5712 try f.writeCValue(writer, ptr, .Other);
5713 try writer.writeAll(", ");
57265714 try f.writeCValue(writer, local, .FunctionArgument);
5727 try writer.writeAll(", ");
5728 try f.writeCValue(writer, new_value, .FunctionArgument);
5729 try writer.writeAll(", ");
5730 try writeMemoryOrder(writer, extra.successOrder());
5731 try writer.writeAll(", ");
5732 try writeMemoryOrder(writer, extra.failureOrder());
5733 try writer.writeByte(')');
5734 if (is_struct) {
5735 try writer.writeAll(";\n");
5736 } else {
5715 try writer.writeAll(", ");
5716 try f.writeCValue(writer, new_value, .FunctionArgument);
5717 try writer.writeAll(", ");
5718 try writeMemoryOrder(writer, extra.successOrder());
5719 try writer.writeAll(", ");
5720 try writeMemoryOrder(writer, extra.failureOrder());
5721 try writer.writeByte(')');
57375722 try writer.writeAll(") {\n");
57385723 f.object.indent_writer.pushIndent();
57395724 try f.writeCValue(writer, local, .Other);
57405725 try writer.writeAll(" = NULL;\n");
57415726 f.object.indent_writer.popIndent();
57425727 try writer.writeAll("}\n");
5728 } else {
5729 try f.writeCValue(writer, local, .Other);
5730 try writer.writeAll(".payload = ");
5731 try f.writeCValue(writer, expected_value, .Other);
5732 try writer.writeAll(";\n");
5733 try f.writeCValue(writer, local, .Other);
5734 try writer.print(".is_null = zig_cmpxchg_{s}((zig_atomic(", .{flavor});
5735 try f.renderTypecast(writer, ptr_ty.elemType());
5736 try writer.writeByte(')');
5737 if (ptr_ty.isVolatilePtr()) try writer.writeAll(" volatile");
5738 try writer.writeAll(" *)");
5739 try f.writeCValue(writer, ptr, .Other);
5740 try writer.writeAll(", ");
5741 try f.writeCValueMember(writer, local, .{ .identifier = "payload" });
5742 try writer.writeAll(", ");
5743 try f.writeCValue(writer, new_value, .FunctionArgument);
5744 try writer.writeAll(", ");
5745 try writeMemoryOrder(writer, extra.successOrder());
5746 try writer.writeAll(", ");
5747 try writeMemoryOrder(writer, extra.failureOrder());
5748 try writer.writeByte(')');
5749 try writer.writeAll(";\n");
57435750 }
57445751
57455752 return local;
......@@ -6211,7 +6218,9 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue {
62116218 const writer = f.object.writer();
62126219 const local = try f.allocLocal(inst, inst_ty);
62136220 try f.writeCValue(writer, local, .Other);
6214 try writer.writeAll(" = ");
6221 try writer.writeAll(" = (");
6222 try f.renderTypecast(writer, inst_ty);
6223 try writer.writeAll(")");
62156224 switch (inst_ty.zigTypeTag()) {
62166225 .Array, .Vector => {
62176226 const elem_ty = inst_ty.childType();
......@@ -6357,15 +6366,14 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue {
63576366
63586367 const writer = f.object.writer();
63596368 const local = try f.allocLocal(inst, union_ty);
6360 try f.writeCValue(writer, local, .Other);
63616369 if (union_obj.layout == .Packed) {
6370 try f.writeCValue(writer, local, .Other);
63626371 try writer.writeAll(" = ");
63636372 try f.writeCValue(writer, payload, .Initializer);
63646373 try writer.writeAll(";\n");
63656374 return local;
63666375 }
63676376
6368 try writer.writeAll(" = {");
63696377 if (union_ty.unionTagTypeSafety()) |tag_ty| {
63706378 const layout = union_ty.unionGetLayout(target);
63716379 if (layout.tag_size != 0) {
......@@ -6380,16 +6388,15 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue {
63806388 var int_pl: Value.Payload.U64 = undefined;
63816389 const int_val = tag_val.enumToInt(tag_ty, &int_pl);
63826390
6383 try writer.print(".tag = {}, ", .{try f.fmtIntLiteral(tag_ty, int_val)});
6391 try f.writeCValue(writer, local, .Other);
6392 try writer.print(".tag = {}; ", .{try f.fmtIntLiteral(tag_ty, int_val)});
63846393 }
6385 try writer.writeAll(".payload = {");
63866394 }
63876395
6388 try writer.print(".{ } = ", .{fmtIdent(field_name)});
6389 try f.writeCValue(writer, payload, .Initializer);
6390
6391 if (union_ty.unionTagTypeSafety()) |_| try writer.writeByte('}');
6392 try writer.writeAll("};\n");
6396 try f.writeCValue(writer, local, .Other);
6397 try writer.print(".payload.{ } = ", .{fmtIdent(field_name)});
6398 try f.writeCValue(writer, payload, .Other);
6399 try writer.writeAll(";\n");
63936400
63946401 return local;
63956402}