aboutsummaryrefslogtreecommitdiff
path: root/src/codegen/c.zig
diff options
context:
space:
mode:
authorJacob Young <jacobly0@users.noreply.github.com>2022-10-08 11:29:06 -0400
committerJacob Young <jacobly0@users.noreply.github.com>2022-10-25 05:11:28 -0400
commit962f33ee11676934eeece4595cdc372662986f6d (patch)
tree4171cf842088f08f54aa97815768a61b7c94592d /src/codegen/c.zig
parent6a4266d62aacf887a81549d81ae6f312992d2b2c (diff)
downloadzig-962f33ee11676934eeece4595cdc372662986f6d.tar.gz
zig-962f33ee11676934eeece4595cdc372662986f6d.zip
cbe: implement airUnionInit
Diffstat (limited to 'src/codegen/c.zig')
-rw-r--r--src/codegen/c.zig39
1 files changed, 33 insertions, 6 deletions
diff --git a/src/codegen/c.zig b/src/codegen/c.zig
index beafafc94f..39ed7cf477 100644
--- a/src/codegen/c.zig
+++ b/src/codegen/c.zig
@@ -4331,16 +4331,43 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue {
fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue {
if (f.liveness.isUnused(inst)) return CValue.none;
- const inst_ty = f.air.typeOfIndex(inst);
const ty_pl = f.air.instructions.items(.data)[inst].ty_pl;
+ const extra = f.air.extraData(Air.UnionInit, ty_pl.payload).data;
+ const union_ty = f.air.typeOfIndex(inst);
+ const target = f.object.dg.module.getTarget();
+ const layout = union_ty.unionGetLayout(target);
+ const union_obj = union_ty.cast(Type.Payload.Union).?.data;
+ const field_name = union_obj.fields.keys()[extra.field_index];
+ const payload = try f.resolveInst(extra.init);
const writer = f.object.writer();
- const local = try f.allocLocal(inst_ty, .Const);
- try writer.writeAll(" = ");
+ const local = try f.allocLocal(union_ty, .Const);
+ try writer.writeAll(" = {");
+ if (union_ty.unionTagTypeSafety()) |tag_ty| {
+ if (layout.tag_size != 0) {
+ const field_index = tag_ty.enumFieldIndex(field_name).?;
- _ = local;
- _ = ty_pl;
- return f.fail("TODO: C backend: implement airUnionInit", .{});
+ var tag_val_pl: Value.Payload.U32 = .{
+ .base = .{ .tag = .enum_field_index },
+ .data = @intCast(u32, field_index),
+ };
+ const tag_val = Value.initPayload(&tag_val_pl.base);
+
+ var int_val_pl: Value.Payload.U64 = undefined;
+ const int_val = tag_val.enumToInt(tag_ty, &int_val_pl);
+
+ try writer.print(".tag = {}, ", .{try f.fmtIntLiteral(tag_ty, int_val)});
+ }
+ try writer.writeAll(".payload = {");
+ }
+
+ try writer.print(".{ } = ", .{fmtIdent(field_name)});
+ try f.writeCValue(writer, payload);
+
+ if (union_ty.unionTagTypeSafety()) |_| try writer.writeByte('}');
+ try writer.writeAll("};\n");
+
+ return local;
}
fn airPrefetch(f: *Function, inst: Air.Inst.Index) !CValue {