authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-02-23 20:29:59-05:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-02-23 20:30:59-05:00
log1f3d9f79c19c225b2043a1d0355cbc74addcf03a
treeaf079b529fd8f6a445b5ac4dd8d2e9aacb63419f
parentc0671a92c7f200a3c32d03db7b7e342c3efdd1fe

CBE: apply some maybe payload cleanups


1 files changed, 17 insertions(+), 29 deletions(-)

src/codegen/c.zig+17-29
...@@ -5356,11 +5356,6 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -5356,11 +5356,6 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue {
5356 // Ensure complete type definition is visible before accessing fields.5356 // Ensure complete type definition is visible before accessing fields.
5357 _ = try f.typeToIndex(struct_ty, .complete);5357 _ = try f.typeToIndex(struct_ty, .complete);
53585358
5359 const extra_name: CValue = switch (struct_ty.tag()) {
5360 .union_tagged, .union_safety_tagged => .{ .identifier = "payload" },
5361 else => .none,
5362 };
5363
5364 const field_name: CValue = switch (struct_ty.tag()) {5359 const field_name: CValue = switch (struct_ty.tag()) {
5365 .tuple, .anon_struct, .@"struct" => switch (struct_ty.containerLayout()) {5360 .tuple, .anon_struct, .@"struct" => switch (struct_ty.containerLayout()) {
5366 .Auto, .Extern => if (struct_ty.isSimpleTuple())5361 .Auto, .Extern => if (struct_ty.isSimpleTuple())
...@@ -5458,31 +5453,29 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -5458,31 +5453,29 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue {
5458 }5453 }
54595454
5460 return local;5455 return local;
5461 } else .{5456 } else field_name: {
5462 .identifier = struct_ty.unionFields().keys()[extra.field_index],5457 const name = struct_ty.unionFields().keys()[extra.field_index];
5458 break :field_name if (struct_ty.unionTagTypeSafety()) |_|
5459 .{ .payload_identifier = name }
5460 else
5461 .{ .identifier = name };
5463 },5462 },
5464 else => unreachable,5463 else => unreachable,
5465 };5464 };
54665465
5467 const is_array = lowersToArray(inst_ty, target);
5468 const local = try f.allocLocal(inst, inst_ty);5466 const local = try f.allocLocal(inst, inst_ty);
5469 if (is_array) {5467 if (lowersToArray(inst_ty, target)) {
5470 try writer.writeAll("memcpy(");5468 try writer.writeAll("memcpy(");
5471 try f.writeCValue(writer, local, .FunctionArgument);5469 try f.writeCValue(writer, local, .FunctionArgument);
5472 try writer.writeAll(", ");5470 try writer.writeAll(", ");
5473 } else {5471 try f.writeCValueMember(writer, struct_byval, field_name);
5474 try f.writeCValue(writer, local, .Other);
5475 try writer.writeAll(" = ");
5476 }
5477 if (extra_name != .none) {
5478 try f.writeCValueMember(writer, struct_byval, extra_name);
5479 try writer.writeByte('.');
5480 try f.writeCValue(writer, field_name, .Other);
5481 } else try f.writeCValueMember(writer, struct_byval, field_name);
5482 if (is_array) {
5483 try writer.writeAll(", sizeof(");5472 try writer.writeAll(", sizeof(");
5484 try f.renderType(writer, inst_ty);5473 try f.renderType(writer, inst_ty);
5485 try writer.writeAll("))");5474 try writer.writeAll("))");
5475 } else {
5476 try f.writeCValue(writer, local, .Other);
5477 try writer.writeAll(" = ");
5478 try f.writeCValueMember(writer, struct_byval, field_name);
5486 }5479 }
5487 try writer.writeAll(";\n");5480 try writer.writeAll(";\n");
5488 return local;5481 return local;
...@@ -6700,7 +6693,7 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -6700,7 +6693,7 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue {
6700 return local;6693 return local;
6701 }6694 }
67026695
6703 if (union_ty.unionTagTypeSafety()) |tag_ty| {6696 const field: CValue = if (union_ty.unionTagTypeSafety()) |tag_ty| field: {
6704 const layout = union_ty.unionGetLayout(target);6697 const layout = union_ty.unionGetLayout(target);
6705 if (layout.tag_size != 0) {6698 if (layout.tag_size != 0) {
6706 const field_index = tag_ty.enumFieldIndex(field_name).?;6699 const field_index = tag_ty.enumFieldIndex(field_name).?;
...@@ -6717,18 +6710,13 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -6717,18 +6710,13 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue {
6717 try f.writeCValue(writer, local, .Other);6710 try f.writeCValue(writer, local, .Other);
6718 try writer.print(".tag = {}; ", .{try f.fmtIntLiteral(tag_ty, int_val)});6711 try writer.print(".tag = {}; ", .{try f.fmtIntLiteral(tag_ty, int_val)});
6719 }6712 }
6720 try f.writeCValue(writer, local, .Other);6713 break :field .{ .payload_identifier = field_name };
6721 try writer.print(".payload.{ } = ", .{fmtIdent(field_name)});6714 } else .{ .identifier = field_name };
6722 try f.writeCValue(writer, payload, .Other);
6723 try writer.writeAll(";\n");
6724 return local;
6725 }
67266715
6727 try f.writeCValue(writer, local, .Other);6716 try f.writeCValueMember(writer, local, field);
6728 try writer.print(".{ } = ", .{fmtIdent(field_name)});6717 try writer.writeAll(" = ");
6729 try f.writeCValue(writer, payload, .Other);6718 try f.writeCValue(writer, payload, .Other);
6730 try writer.writeAll(";\n");6719 try writer.writeAll(";\n");
6731
6732 return local;6720 return local;
6733}6721}
67346722