authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2022-10-10 04:08:52-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2022-10-25 05:11:29-04:00
log87d432328e564ac138a1b773ab025324b5c191ed
treead251a5754a841b10c657387da6427fc59131fb8
parentfeb8f81cd9d8df282d57441422dc70cee079a993

cbe: implement aggregate_init of struct


3 files changed, 54 insertions(+), 64 deletions(-)

src/codegen/c.zig+54-61
......@@ -62,7 +62,6 @@ const FormatTypeAsCIdentContext = struct {
6262};
6363
6464const ValueRenderLocation = enum {
65 Identifier,
6665 FunctionArgument,
6766 Other,
6867};
......@@ -340,7 +339,7 @@ pub const Function = struct {
340339 }
341340
342341 fn fmtIntLiteral(f: *Function, ty: Type, val: Value) !std.fmt.Formatter(formatIntLiteral) {
343 return f.object.dg.fmtIntLiteral(ty, val, .Other);
342 return f.object.dg.fmtIntLiteral(ty, val);
344343 }
345344};
346345
......@@ -533,16 +532,13 @@ pub const DeclGen = struct {
533532 // Using '{}' for integer and floats seemed to error C compilers (both GCC and Clang)
534533 // with 'error: expected expression' (including when built with 'zig cc')
535534 .Bool => return writer.writeAll("false"),
536 .Int,
537 .Enum,
538 .ErrorSet,
539 => return writer.print("{x}", .{try dg.fmtIntLiteral(ty, val, location)}),
535 .Int, .Enum, .ErrorSet => return writer.print("{x}", .{try dg.fmtIntLiteral(ty, val)}),
540536 .Float => switch (ty.tag()) {
541537 .f32 => return writer.print("zig_bitcast_f32_u32({x})", .{
542 try dg.fmtIntLiteral(Type.u32, val, location),
538 try dg.fmtIntLiteral(Type.u32, val),
543539 }),
544540 .f64 => return writer.print("zig_bitcast_f64_u64({x})", .{
545 try dg.fmtIntLiteral(Type.u64, val, location),
541 try dg.fmtIntLiteral(Type.u64, val),
546542 }),
547543 else => return dg.fail("TODO float types > 64 bits are not support in renderValue() as of now", .{}),
548544 },
......@@ -554,14 +550,12 @@ pub const DeclGen = struct {
554550 var buf: Type.SlicePtrFieldTypeBuffer = undefined;
555551 const ptr_ty = ty.slicePtrFieldType(&buf);
556552 try dg.renderTypecast(writer, ptr_ty);
557 return writer.print("){x}, {0x}}}", .{
558 try dg.fmtIntLiteral(Type.usize, val, location),
559 });
553 return writer.print("){x}, {0x}}}", .{try dg.fmtIntLiteral(Type.usize, val)});
560554 },
561555 .Many, .C, .One => {
562556 try writer.writeAll("((");
563557 try dg.renderTypecast(writer, ty);
564 return writer.print("){x})", .{try dg.fmtIntLiteral(Type.usize, val, location)});
558 return writer.print("){x})", .{try dg.fmtIntLiteral(Type.usize, val)});
565559 },
566560 },
567561 .Optional => {
......@@ -598,9 +592,7 @@ pub const DeclGen = struct {
598592
599593 empty = false;
600594 }
601 if (empty) try writer.print("{x}", .{
602 try dg.fmtIntLiteral(Type.u8, Value.undef, location),
603 });
595 if (empty) try writer.print("{x}", .{try dg.fmtIntLiteral(Type.u8, Value.undef)});
604596
605597 return writer.writeByte('}');
606598 },
......@@ -613,9 +605,7 @@ pub const DeclGen = struct {
613605 if (!field.ty.hasRuntimeBits()) continue;
614606 try dg.renderValue(writer, field.ty, val, location);
615607 break;
616 } else try writer.print("{x}", .{
617 try dg.fmtIntLiteral(Type.u8, Value.undef, location),
618 });
608 } else try writer.print("{x}", .{try dg.fmtIntLiteral(Type.u8, Value.undef)});
619609
620610 return writer.writeByte('}');
621611 },
......@@ -625,7 +615,7 @@ pub const DeclGen = struct {
625615 try writer.writeAll("){ .payload = ");
626616 try dg.renderValue(writer, ty.errorUnionPayload(), val, location);
627617 return writer.print(", .error = {x} }}", .{
628 try dg.fmtIntLiteral(ty.errorUnionSet(), Value.undef, location),
618 try dg.fmtIntLiteral(ty.errorUnionSet(), Value.undef),
629619 });
630620 },
631621 .Array => {
......@@ -670,7 +660,7 @@ pub const DeclGen = struct {
670660 .decl_ref_mut,
671661 .decl_ref,
672662 => try dg.renderParentPtr(writer, val, ty),
673 else => try writer.print("{}", .{try dg.fmtIntLiteral(ty, val, location)}),
663 else => try writer.print("{}", .{try dg.fmtIntLiteral(ty, val)}),
674664 },
675665 .Float => {
676666 if (ty.floatBits(target) <= 64) {
......@@ -682,22 +672,20 @@ pub const DeclGen = struct {
682672 .base = .{ .tag = .int_u64 },
683673 .data = @bitCast(u32, val.toFloat(f32)),
684674 };
685 return writer.print("zig_bitcast_f32_u32({x})", .{try dg.fmtIntLiteral(
686 Type.u32,
687 Value.initPayload(&bitcast_val_pl.base),
688 location,
689 )});
675 const bitcast_val = Value.initPayload(&bitcast_val_pl.base);
676 return writer.print("zig_bitcast_f32_u32({x})", .{
677 try dg.fmtIntLiteral(Type.u32, bitcast_val),
678 });
690679 },
691680 .f64 => {
692681 var bitcast_val_pl = Value.Payload.U64{
693682 .base = .{ .tag = .int_u64 },
694683 .data = @bitCast(u64, val.toFloat(f64)),
695684 };
696 return writer.print("zig_bitcast_f32_u32({x})", .{try dg.fmtIntLiteral(
697 Type.u64,
698 Value.initPayload(&bitcast_val_pl.base),
699 location,
700 )});
685 const bitcast_val = Value.initPayload(&bitcast_val_pl.base);
686 return writer.print("zig_bitcast_f64_u64({x})", .{
687 try dg.fmtIntLiteral(Type.u64, bitcast_val),
688 });
701689 },
702690 else => return dg.fail("TODO float types > 64 bits are not support in renderValue() as of now", .{}),
703691 }
......@@ -740,7 +728,7 @@ pub const DeclGen = struct {
740728 .int_u64, .one => {
741729 try writer.writeAll("((");
742730 try dg.renderTypecast(writer, ty);
743 return writer.print("){x})", .{try dg.fmtIntLiteral(Type.usize, val, location)});
731 return writer.print("){x})", .{try dg.fmtIntLiteral(Type.usize, val)});
744732 },
745733 .field_ptr,
746734 .elem_ptr,
......@@ -918,7 +906,7 @@ pub const DeclGen = struct {
918906
919907 empty = false;
920908 }
921 if (empty) try writer.writeByte('0');
909 if (empty) try writer.print("{}", .{try dg.fmtIntLiteral(Type.u8, Value.zero)});
922910
923911 try writer.writeByte('}');
924912 },
......@@ -1730,7 +1718,7 @@ pub const DeclGen = struct {
17301718 var len_val_pl = Value.Payload.U64{ .base = .{ .tag = .int_u64 }, .data = name.len };
17311719 const len_val = Value.initPayload(&len_val_pl.base);
17321720
1733 try bw.print(" case {}: {{\n static ", .{try dg.fmtIntLiteral(enum_ty, int_val, .Other)});
1721 try bw.print(" case {}: {{\n static ", .{try dg.fmtIntLiteral(enum_ty, int_val)});
17341722 try dg.renderTypeAndName(bw, name_ty, .{ .identifier = "name" }, .Const, 0);
17351723 try buffer.appendSlice(" = ");
17361724 try dg.renderValue(bw, name_ty, name_val, .Other);
......@@ -1738,7 +1726,7 @@ pub const DeclGen = struct {
17381726 try dg.renderTypecast(bw, name_slice_ty);
17391727 try bw.print("){{{}, {}}};\n", .{
17401728 fmtIdent("name"),
1741 try dg.fmtIntLiteral(Type.usize, len_val, .Other),
1729 try dg.fmtIntLiteral(Type.usize, len_val),
17421730 });
17431731
17441732 try buffer.appendSlice(" }\n");
......@@ -1793,7 +1781,7 @@ pub const DeclGen = struct {
17931781 .undefined_ptr => |ty| {
17941782 try w.writeAll("((");
17951783 try dg.renderTypecast(w, ty);
1796 return w.print("){x})", .{try dg.fmtIntLiteral(Type.usize, Value.undef, .Other)});
1784 return w.print("){x})", .{try dg.fmtIntLiteral(Type.usize, Value.undef)});
17971785 },
17981786 .identifier => |ident| return w.print("{ }", .{fmtIdent(ident)}),
17991787 .bytes => |bytes| return w.writeAll(bytes),
......@@ -1843,7 +1831,6 @@ pub const DeclGen = struct {
18431831 dg: *DeclGen,
18441832 ty: Type,
18451833 val: Value,
1846 location: ValueRenderLocation,
18471834 ) !std.fmt.Formatter(formatIntLiteral) {
18481835 const int_info = ty.intInfo(dg.module.getTarget());
18491836 const c_bits = toCIntBits(int_info.bits);
......@@ -1853,7 +1840,6 @@ pub const DeclGen = struct {
18531840 .ty = ty,
18541841 .val = val,
18551842 .mod = dg.module,
1856 .location = location,
18571843 } };
18581844 }
18591845};
......@@ -1916,7 +1902,7 @@ pub fn genErrDecls(o: *Object) !void {
19161902
19171903 try writer.print("{{" ++ name_prefix ++ "_{}, {}}}", .{
19181904 fmtIdent(name),
1919 try o.dg.fmtIntLiteral(Type.usize, len_val, .Other),
1905 try o.dg.fmtIntLiteral(Type.usize, len_val),
19201906 });
19211907 }
19221908 try writer.writeAll("};\n");
......@@ -4415,27 +4401,44 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue {
44154401
44164402 const inst_ty = f.air.typeOfIndex(inst);
44174403 const ty_pl = f.air.instructions.items(.data)[inst].ty_pl;
4418 const vector_ty = f.air.getRefType(ty_pl.ty);
4419 const len = vector_ty.vectorLen();
4404 const len = @intCast(usize, inst_ty.arrayLen());
44204405 const elements = @ptrCast([]const Air.Inst.Ref, f.air.extra[ty_pl.payload..][0..len]);
44214406
44224407 const writer = f.object.writer();
44234408 const local = try f.allocLocal(inst_ty, .Const);
44244409 try writer.writeAll(" = {");
4425 switch (vector_ty.zigTypeTag()) {
4410 switch (inst_ty.zigTypeTag()) {
4411 .Array => {
4412 const elem_ty = inst_ty.childType();
4413 var empty = true;
4414 for (elements) |element| {
4415 if (empty) try writer.writeAll(", ");
4416 try f.writeCValue(writer, try f.resolveInst(element));
4417 empty = false;
4418 }
4419 if (inst_ty.sentinel()) |sentinel| {
4420 if (empty) try writer.writeAll(", ");
4421 try f.object.dg.renderValue(writer, elem_ty, sentinel, .Other);
4422 empty = false;
4423 }
4424 if (empty) try writer.print("{}", .{try f.fmtIntLiteral(Type.u8, Value.zero)});
4425 },
44264426 .Struct => {
4427 const tuple = vector_ty.tupleFields();
4428 var i: usize = 0;
4429 for (elements) |elem, elem_index| {
4430 if (tuple.values[elem_index].tag() != .unreachable_value) continue;
4431
4432 const value = try f.resolveInst(elem);
4433 if (i != 0) try writer.writeAll(", ");
4434 try f.writeCValue(writer, value);
4435 i += 1;
4427 var empty = true;
4428 for (elements) |element, index| {
4429 if (inst_ty.structFieldValueComptime(index)) |_| continue;
4430
4431 if (!empty) try writer.writeAll(", ");
4432 if (!inst_ty.isTupleOrAnonStruct()) {
4433 try writer.print(".{ } = ", .{fmtIdent(inst_ty.structFieldName(index))});
4434 }
4435 try f.writeCValue(writer, try f.resolveInst(element));
4436 empty = false;
44364437 }
4438 if (empty) try writer.print("{}", .{try f.fmtIntLiteral(Type.u8, Value.zero)});
44374439 },
4438 else => |tag| return f.fail("TODO: C backend: implement airAggregateInit for type {s}", .{@tagName(tag)}),
4440 .Vector => return f.fail("TODO: C backend: implement airAggregateInit for vectors", .{}),
4441 else => unreachable,
44394442 }
44404443 try writer.writeAll("};\n");
44414444
......@@ -4706,7 +4709,6 @@ const FormatIntLiteralContext = struct {
47064709 ty: Type,
47074710 val: Value,
47084711 mod: *Module,
4709 location: ValueRenderLocation,
47104712};
47114713fn formatIntLiteral(
47124714 data: FormatIntLiteralContext,
......@@ -4755,13 +4757,6 @@ fn formatIntLiteral(
47554757 } else data.val.toBigInt(&int_buf, target);
47564758 assert(int.fitsInTwosComp(int_info.signedness, int_info.bits));
47574759
4758 if (data.location == .Identifier) {
4759 const str = try int.toStringAlloc(allocator, 10, undefined);
4760 defer allocator.free(str);
4761
4762 return writer.writeAll(str);
4763 }
4764
47654760 const limbs_count_64 = @divExact(64, @bitSizeOf(Limb));
47664761 const c_bits = toCIntBits(int_info.bits) orelse unreachable;
47674762 if (c_bits == 128) {
......@@ -4788,7 +4783,6 @@ fn formatIntLiteral(
47884783 .ty = Type.u64,
47894784 .val = upper_val,
47904785 .mod = data.mod,
4791 .location = data.location,
47924786 }, fmt, options, writer);
47934787 try writer.writeAll("<<64|");
47944788 }
......@@ -4802,7 +4796,6 @@ fn formatIntLiteral(
48024796 .ty = Type.u64,
48034797 .val = lower_val,
48044798 .mod = data.mod,
4805 .location = data.location,
48064799 }, fmt, options, writer);
48074800
48084801 if (have_upper) try writer.writeByte(')');
test/behavior/call.zig-1
......@@ -86,7 +86,6 @@ test "tuple parameters" {
8686}
8787
8888test "result location of function call argument through runtime condition and struct init" {
89 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
9089 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
9190 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
9291
test/behavior/struct.zig-2
......@@ -1017,7 +1017,6 @@ test "struct with union field" {
10171017}
10181018
10191019test "type coercion of anon struct literal to struct" {
1020 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
10211020 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
10221021 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
10231022 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
......@@ -1056,7 +1055,6 @@ test "type coercion of anon struct literal to struct" {
10561055
10571056test "type coercion of pointer to anon struct literal to pointer to struct" {
10581057 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
1059 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
10601058 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
10611059 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
10621060 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO