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(...@@ -3843,6 +3843,8 @@ fn airCall(
3843 inst: Air.Inst.Index,3843 inst: Air.Inst.Index,
3844 modifier: std.builtin.CallOptions.Modifier,3844 modifier: std.builtin.CallOptions.Modifier,
3845) !CValue {3845) !CValue {
3846 // Not even allowed to call panic in a naked function.
3847 if (f.object.dg.decl.ty.fnCallingConvention() == .Naked) return .none;
3846 const gpa = f.object.dg.gpa;3848 const gpa = f.object.dg.gpa;
38473849
3848 switch (modifier) {3850 switch (modifier) {
...@@ -5497,7 +5499,7 @@ fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -5497,7 +5499,7 @@ fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue {
5497 try f.writeCValue(writer, local, .Other);5499 try f.writeCValue(writer, local, .Other);
5498 const array_len = f.air.typeOf(ty_op.operand).elemType().arrayLen();5500 const array_len = f.air.typeOf(ty_op.operand).elemType().arrayLen();
54995501
5500 try writer.writeAll(" = { .ptr = ");5502 try writer.writeAll(".ptr = ");
5501 if (operand == .undef) {5503 if (operand == .undef) {
5502 // Unfortunately, C does not support any equivalent to5504 // Unfortunately, C does not support any equivalent to
5503 // &(*(void *)p)[0], although LLVM does via GetElementPtr5505 // &(*(void *)p)[0], although LLVM does via GetElementPtr
...@@ -5511,7 +5513,9 @@ fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -5511,7 +5513,9 @@ fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue {
55115513
5512 var len_pl: Value.Payload.U64 = .{ .base = .{ .tag = .int_u64 }, .data = array_len };5514 var len_pl: Value.Payload.U64 = .{ .base = .{ .tag = .int_u64 }, .data = array_len };
5513 const len_val = Value.initPayload(&len_pl.base);5515 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)});
5515 return local;5519 return local;
5516}5520}
55175521
...@@ -5687,59 +5691,62 @@ fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue...@@ -5687,59 +5691,62 @@ fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue
5687 const ty_pl = f.air.instructions.items(.data)[inst].ty_pl;5691 const ty_pl = f.air.instructions.items(.data)[inst].ty_pl;
5688 const extra = f.air.extraData(Air.Cmpxchg, ty_pl.payload).data;5692 const extra = f.air.extraData(Air.Cmpxchg, ty_pl.payload).data;
5689 const inst_ty = f.air.typeOfIndex(inst);5693 const inst_ty = f.air.typeOfIndex(inst);
5690 const is_struct = !inst_ty.isPtrLikeOptional();
5691 const ptr_ty = f.air.typeOf(extra.ptr);
5692 const ptr = try f.resolveInst(extra.ptr);5694 const ptr = try f.resolveInst(extra.ptr);
5693 const expected_value = try f.resolveInst(extra.expected_value);5695 const expected_value = try f.resolveInst(extra.expected_value);
5694 const new_value = try f.resolveInst(extra.new_value);5696 const new_value = try f.resolveInst(extra.new_value);
5695 try reap(f, inst, &.{ extra.ptr, extra.expected_value, extra.new_value });5697 try reap(f, inst, &.{ extra.ptr, extra.expected_value, extra.new_value });
5696 const writer = f.object.writer();5698 const writer = f.object.writer();
56975699 const ptr_ty = f.air.typeOf(extra.ptr);
5698 const local = try f.allocLocal(inst, inst_ty);5700 const local = try f.allocLocal(inst, inst_ty);
5699 try f.writeCValue(writer, local, .Other);5701 if (inst_ty.isPtrLikeOptional()) {
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) {
5711 try f.writeCValue(writer, local, .Other);5702 try f.writeCValue(writer, local, .Other);
5712 try writer.writeAll(".is_null = ");5703 try writer.writeAll(" = ");
5713 } else {5704 try f.writeCValue(writer, expected_value, .Initializer);
5705 try writer.writeAll(";\n");
5714 try writer.writeAll("if (");5706 try writer.writeAll("if (");
5715 }5707 try writer.print("zig_cmpxchg_{s}((zig_atomic(", .{flavor});
5716 try writer.print("zig_cmpxchg_{s}((zig_atomic(", .{flavor});5708 try f.renderTypecast(writer, ptr_ty.elemType());
5717 try f.renderTypecast(writer, ptr_ty.elemType());5709 try writer.writeByte(')');
5718 try writer.writeByte(')');5710 if (ptr_ty.isVolatilePtr()) try writer.writeAll(" volatile");
5719 if (ptr_ty.isVolatilePtr()) try writer.writeAll(" volatile");5711 try writer.writeAll(" *)");
5720 try writer.writeAll(" *)");5712 try f.writeCValue(writer, ptr, .Other);
5721 try f.writeCValue(writer, ptr, .Other);5713 try writer.writeAll(", ");
5722 try writer.writeAll(", ");
5723 if (is_struct)
5724 try f.writeCValueMember(writer, local, .{ .identifier = "payload" })
5725 else
5726 try f.writeCValue(writer, local, .FunctionArgument);5714 try f.writeCValue(writer, local, .FunctionArgument);
5727 try writer.writeAll(", ");5715 try writer.writeAll(", ");
5728 try f.writeCValue(writer, new_value, .FunctionArgument);5716 try f.writeCValue(writer, new_value, .FunctionArgument);
5729 try writer.writeAll(", ");5717 try writer.writeAll(", ");
5730 try writeMemoryOrder(writer, extra.successOrder());5718 try writeMemoryOrder(writer, extra.successOrder());
5731 try writer.writeAll(", ");5719 try writer.writeAll(", ");
5732 try writeMemoryOrder(writer, extra.failureOrder());5720 try writeMemoryOrder(writer, extra.failureOrder());
5733 try writer.writeByte(')');5721 try writer.writeByte(')');
5734 if (is_struct) {
5735 try writer.writeAll(";\n");
5736 } else {
5737 try writer.writeAll(") {\n");5722 try writer.writeAll(") {\n");
5738 f.object.indent_writer.pushIndent();5723 f.object.indent_writer.pushIndent();
5739 try f.writeCValue(writer, local, .Other);5724 try f.writeCValue(writer, local, .Other);
5740 try writer.writeAll(" = NULL;\n");5725 try writer.writeAll(" = NULL;\n");
5741 f.object.indent_writer.popIndent();5726 f.object.indent_writer.popIndent();
5742 try writer.writeAll("}\n");5727 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");
5743 }5750 }
57445751
5745 return local;5752 return local;
...@@ -6211,7 +6218,9 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -6211,7 +6218,9 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue {
6211 const writer = f.object.writer();6218 const writer = f.object.writer();
6212 const local = try f.allocLocal(inst, inst_ty);6219 const local = try f.allocLocal(inst, inst_ty);
6213 try f.writeCValue(writer, local, .Other);6220 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(")");
6215 switch (inst_ty.zigTypeTag()) {6224 switch (inst_ty.zigTypeTag()) {
6216 .Array, .Vector => {6225 .Array, .Vector => {
6217 const elem_ty = inst_ty.childType();6226 const elem_ty = inst_ty.childType();
...@@ -6357,15 +6366,14 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -6357,15 +6366,14 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue {
63576366
6358 const writer = f.object.writer();6367 const writer = f.object.writer();
6359 const local = try f.allocLocal(inst, union_ty);6368 const local = try f.allocLocal(inst, union_ty);
6360 try f.writeCValue(writer, local, .Other);
6361 if (union_obj.layout == .Packed) {6369 if (union_obj.layout == .Packed) {
6370 try f.writeCValue(writer, local, .Other);
6362 try writer.writeAll(" = ");6371 try writer.writeAll(" = ");
6363 try f.writeCValue(writer, payload, .Initializer);6372 try f.writeCValue(writer, payload, .Initializer);
6364 try writer.writeAll(";\n");6373 try writer.writeAll(";\n");
6365 return local;6374 return local;
6366 }6375 }
63676376
6368 try writer.writeAll(" = {");
6369 if (union_ty.unionTagTypeSafety()) |tag_ty| {6377 if (union_ty.unionTagTypeSafety()) |tag_ty| {
6370 const layout = union_ty.unionGetLayout(target);6378 const layout = union_ty.unionGetLayout(target);
6371 if (layout.tag_size != 0) {6379 if (layout.tag_size != 0) {
...@@ -6380,16 +6388,15 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -6380,16 +6388,15 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue {
6380 var int_pl: Value.Payload.U64 = undefined;6388 var int_pl: Value.Payload.U64 = undefined;
6381 const int_val = tag_val.enumToInt(tag_ty, &int_pl);6389 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)});
6384 }6393 }
6385 try writer.writeAll(".payload = {");
6386 }6394 }
63876395
6388 try writer.print(".{ } = ", .{fmtIdent(field_name)});6396 try f.writeCValue(writer, local, .Other);
6389 try f.writeCValue(writer, payload, .Initializer);6397 try writer.print(".payload.{ } = ", .{fmtIdent(field_name)});
63906398 try f.writeCValue(writer, payload, .Other);
6391 if (union_ty.unionTagTypeSafety()) |_| try writer.writeByte('}');6399 try writer.writeAll(";\n");
6392 try writer.writeAll("};\n");
63936400
6394 return local;6401 return local;
6395}6402}