| author | |
| committer | |
| log | 962f33ee11676934eeece4595cdc372662986f6d |
| tree | 4171cf842088f08f54aa97815768a61b7c94592d |
| parent | 6a4266d62aacf887a81549d81ae6f312992d2b2c |
3 files changed, 33 insertions(+), 13 deletions(-)
src/codegen/c.zig+33-6| ... | ... | @@ -4331,16 +4331,43 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4331 | 4331 | fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4332 | 4332 | if (f.liveness.isUnused(inst)) return CValue.none; |
| 4333 | 4333 | |
| 4334 | const inst_ty = f.air.typeOfIndex(inst); | |
| 4335 | 4334 | const ty_pl = f.air.instructions.items(.data)[inst].ty_pl; |
| 4335 | const extra = f.air.extraData(Air.UnionInit, ty_pl.payload).data; | |
| 4336 | const union_ty = f.air.typeOfIndex(inst); | |
| 4337 | const target = f.object.dg.module.getTarget(); | |
| 4338 | const layout = union_ty.unionGetLayout(target); | |
| 4339 | const union_obj = union_ty.cast(Type.Payload.Union).?.data; | |
| 4340 | const field_name = union_obj.fields.keys()[extra.field_index]; | |
| 4341 | const payload = try f.resolveInst(extra.init); | |
| 4336 | 4342 | |
| 4337 | 4343 | const writer = f.object.writer(); |
| 4338 | const local = try f.allocLocal(inst_ty, .Const); | |
| 4339 | try writer.writeAll(" = "); | |
| 4344 | const local = try f.allocLocal(union_ty, .Const); | |
| 4345 | try writer.writeAll(" = {"); | |
| 4346 | if (union_ty.unionTagTypeSafety()) |tag_ty| { | |
| 4347 | if (layout.tag_size != 0) { | |
| 4348 | const field_index = tag_ty.enumFieldIndex(field_name).?; | |
| 4340 | 4349 | |
| 4341 | _ = local; | |
| 4342 | _ = ty_pl; | |
| 4343 | return f.fail("TODO: C backend: implement airUnionInit", .{}); | |
| 4350 | var tag_val_pl: Value.Payload.U32 = .{ | |
| 4351 | .base = .{ .tag = .enum_field_index }, | |
| 4352 | .data = @intCast(u32, field_index), | |
| 4353 | }; | |
| 4354 | const tag_val = Value.initPayload(&tag_val_pl.base); | |
| 4355 | ||
| 4356 | var int_val_pl: Value.Payload.U64 = undefined; | |
| 4357 | const int_val = tag_val.enumToInt(tag_ty, &int_val_pl); | |
| 4358 | ||
| 4359 | try writer.print(".tag = {}, ", .{try f.fmtIntLiteral(tag_ty, int_val)}); | |
| 4360 | } | |
| 4361 | try writer.writeAll(".payload = {"); | |
| 4362 | } | |
| 4363 | ||
| 4364 | try writer.print(".{ } = ", .{fmtIdent(field_name)}); | |
| 4365 | try f.writeCValue(writer, payload); | |
| 4366 | ||
| 4367 | if (union_ty.unionTagTypeSafety()) |_| try writer.writeByte('}'); | |
| 4368 | try writer.writeAll("};\n"); | |
| 4369 | ||
| 4370 | return local; | |
| 4344 | 4371 | } |
| 4345 | 4372 | |
| 4346 | 4373 | fn airPrefetch(f: *Function, inst: Air.Inst.Index) !CValue { |
test/behavior/translate_c_macros.zig-1| ... | ... | @@ -58,7 +58,6 @@ test "cast negative integer to pointer" { |
| 58 | 58 | test "casting to union with a macro" { |
| 59 | 59 | if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO |
| 60 | 60 | if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO |
| 61 | if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO | |
| 62 | 61 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |
| 63 | 62 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO |
| 64 | 63 |
test/behavior/union.zig-6| ... | ... | @@ -776,7 +776,6 @@ test "return union init with void payload" { |
| 776 | 776 | } |
| 777 | 777 | |
| 778 | 778 | test "@unionInit stored to a const" { |
| 779 | if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO | |
| 780 | 779 | if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO |
| 781 | 780 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO |
| 782 | 781 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |
| ... | ... | @@ -939,7 +938,6 @@ test "function call result coerces from tagged union to the tag" { |
| 939 | 938 | } |
| 940 | 939 | |
| 941 | 940 | test "cast from anonymous struct to union" { |
| 942 | if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO | |
| 943 | 941 | if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO |
| 944 | 942 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO |
| 945 | 943 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |
| ... | ... | @@ -972,7 +970,6 @@ test "cast from anonymous struct to union" { |
| 972 | 970 | } |
| 973 | 971 | |
| 974 | 972 | test "cast from pointer to anonymous struct to pointer to union" { |
| 975 | if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO | |
| 976 | 973 | if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO |
| 977 | 974 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO |
| 978 | 975 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |
| ... | ... | @@ -1135,7 +1132,6 @@ test "global variable struct contains union initialized to non-most-aligned fiel |
| 1135 | 1132 | } |
| 1136 | 1133 | |
| 1137 | 1134 | test "union with no result loc initiated with a runtime value" { |
| 1138 | if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO | |
| 1139 | 1135 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |
| 1140 | 1136 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO |
| 1141 | 1137 | if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO |
| ... | ... | @@ -1152,7 +1148,6 @@ test "union with no result loc initiated with a runtime value" { |
| 1152 | 1148 | } |
| 1153 | 1149 | |
| 1154 | 1150 | test "union with a large struct field" { |
| 1155 | if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO | |
| 1156 | 1151 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |
| 1157 | 1152 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO |
| 1158 | 1153 | if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO |
| ... | ... | @@ -1321,7 +1316,6 @@ test "union and enum field order doesn't match" { |
| 1321 | 1316 | } |
| 1322 | 1317 | |
| 1323 | 1318 | test "@unionInit uses tag value instead of field index" { |
| 1324 | if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO | |
| 1325 | 1319 | if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO |
| 1326 | 1320 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO |
| 1327 | 1321 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |