Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Module.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6607,6 +6607,7 @@ pub fn unionFieldNormalAlignment(mod: *Module, u: InternPool.UnionType, field_in
return field_ty.abiAlignment(mod);
}

/// Returns the index of the active field, given the current tag value
pub fn unionTagFieldIndex(mod: *Module, u: InternPool.UnionType, enum_tag: Value) ?u32 {
const ip = &mod.intern_pool;
if (enum_tag.toIntern() == .none) return null;
Expand Down
132 changes: 110 additions & 22 deletions src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27251,7 +27251,7 @@ fn unionFieldVal(
return sema.failWithOwnedErrorMsg(block, msg);
}
},
.Packed, .Extern => {
.Packed, .Extern => |layout| {
if (tag_matches) {
return Air.internedToRef(un.val);
} else {
Expand All @@ -27260,7 +27260,7 @@ fn unionFieldVal(
else
union_ty.unionFieldType(un.tag.toValue(), mod).?;

if (try sema.bitCastVal(block, src, un.val.toValue(), old_ty, field_ty, 0)) |new_val| {
if (try sema.bitCastUnionFieldVal(block, src, un.val.toValue(), old_ty, field_ty, layout)) |new_val| {
return Air.internedToRef(new_val.toIntern());
}
}
Expand Down Expand Up @@ -29781,13 +29781,19 @@ fn storePtrVal(
error.IllDefinedMemoryLayout => unreachable, // Sema was supposed to emit a compile error already
error.Unimplemented => return sema.fail(block, src, "TODO: implement writeToMemory for type '{}'", .{mut_kit.ty.fmt(mod)}),
};
operand_val.writeToMemory(operand_ty, mod, buffer[reinterpret.byte_offset..]) catch |err| switch (err) {
error.OutOfMemory => return error.OutOfMemory,
error.ReinterpretDeclRef => unreachable,
error.IllDefinedMemoryLayout => unreachable, // Sema was supposed to emit a compile error already
error.Unimplemented => return sema.fail(block, src, "TODO: implement writeToMemory for type '{}'", .{operand_ty.fmt(mod)}),
};

if (reinterpret.write_packed) {
operand_val.writeToPackedMemory(operand_ty, mod, buffer[reinterpret.byte_offset..], 0) catch |err| switch (err) {
error.OutOfMemory => return error.OutOfMemory,
error.ReinterpretDeclRef => unreachable,
};
} else {
operand_val.writeToMemory(operand_ty, mod, buffer[reinterpret.byte_offset..]) catch |err| switch (err) {
error.OutOfMemory => return error.OutOfMemory,
error.ReinterpretDeclRef => unreachable,
error.IllDefinedMemoryLayout => unreachable, // Sema was supposed to emit a compile error already
error.Unimplemented => return sema.fail(block, src, "TODO: implement writeToMemory for type '{}'", .{operand_ty.fmt(mod)}),
};
}
const val = Value.readFromMemory(mut_kit.ty, mod, buffer, sema.arena) catch |err| switch (err) {
error.OutOfMemory => return error.OutOfMemory,
error.IllDefinedMemoryLayout => unreachable,
Expand Down Expand Up @@ -29819,6 +29825,8 @@ const ComptimePtrMutationKit = struct {
reinterpret: struct {
val_ptr: *Value,
byte_offset: usize,
/// If set, write the operand to packed memory
write_packed: bool = false,
},
/// If the root decl could not be used as parent, this means `ty` is the type that
/// caused that by not having a well-defined layout.
Expand Down Expand Up @@ -30182,21 +30190,43 @@ fn beginComptimePtrMutation(
);
},
.@"union" => {
// We need to set the active field of the union.
const union_tag_ty = base_child_ty.unionTagTypeHypothetical(mod);

const payload = &val_ptr.castTag(.@"union").?.data;
payload.tag = try mod.enumValueFieldIndex(union_tag_ty, field_index);
const layout = base_child_ty.containerLayout(mod);

return beginComptimePtrMutationInner(
sema,
block,
src,
parent.ty.structFieldType(field_index, mod),
&payload.val,
ptr_elem_ty,
parent.mut_decl,
);
const tag_type = base_child_ty.unionTagTypeHypothetical(mod);
const hypothetical_tag = try mod.enumValueFieldIndex(tag_type, field_index);
if (layout == .Auto or (payload.tag != null and hypothetical_tag.eql(payload.tag.?, tag_type, mod))) {
// We need to set the active field of the union.
payload.tag = hypothetical_tag;

const field_ty = parent.ty.structFieldType(field_index, mod);
return beginComptimePtrMutationInner(
sema,
block,
src,
field_ty,
&payload.val,
ptr_elem_ty,
parent.mut_decl,
);
} else {
// Writing to a different field (a different or unknown tag is active) requires reinterpreting
// memory of the entire union, which requires knowing its abiSize.
try sema.resolveTypeLayout(parent.ty);

// This union value no longer has a well-defined tag type.
// The reinterpretation will read it back out as .none.
payload.val = try payload.val.unintern(sema.arena, mod);
return ComptimePtrMutationKit{
.mut_decl = parent.mut_decl,
.pointee = .{ .reinterpret = .{
.val_ptr = val_ptr,
.byte_offset = 0,
.write_packed = layout == .Packed,
} },
.ty = parent.ty,
};
}
},
.slice => switch (field_index) {
Value.slice_ptr_index => return beginComptimePtrMutationInner(
Expand Down Expand Up @@ -30697,6 +30727,7 @@ fn bitCastVal(
// For types with well-defined memory layouts, we serialize them a byte buffer,
// then deserialize to the new type.
const abi_size = try sema.usizeCast(block, src, old_ty.abiSize(mod));

const buffer = try sema.gpa.alloc(u8, abi_size);
defer sema.gpa.free(buffer);
val.writeToMemory(old_ty, mod, buffer) catch |err| switch (err) {
Expand All @@ -30713,6 +30744,63 @@ fn bitCastVal(
};
}

fn bitCastUnionFieldVal(
sema: *Sema,
block: *Block,
src: LazySrcLoc,
val: Value,
old_ty: Type,
field_ty: Type,
layout: std.builtin.Type.ContainerLayout,
) !?Value {
const mod = sema.mod;
if (old_ty.eql(field_ty, mod)) return val;

const old_size = try sema.usizeCast(block, src, old_ty.abiSize(mod));
const field_size = try sema.usizeCast(block, src, field_ty.abiSize(mod));
const endian = mod.getTarget().cpu.arch.endian();

const buffer = try sema.gpa.alloc(u8, @max(old_size, field_size));
defer sema.gpa.free(buffer);

// Reading a larger value means we need to reinterpret from undefined bytes.
const offset = switch (layout) {
.Extern => offset: {
if (field_size > old_size) @memset(buffer[old_size..], 0xaa);
val.writeToMemory(old_ty, mod, buffer) catch |err| switch (err) {
error.OutOfMemory => return error.OutOfMemory,
error.ReinterpretDeclRef => return null,
error.IllDefinedMemoryLayout => unreachable, // Sema was supposed to emit a compile error already
error.Unimplemented => return sema.fail(block, src, "TODO: implement writeToMemory for type '{}'", .{old_ty.fmt(mod)}),
};
break :offset 0;
},
.Packed => offset: {
if (field_size > old_size) {
const min_size = @max(old_size, 1);
switch (endian) {
.Little => @memset(buffer[min_size - 1 ..], 0xaa),
.Big => @memset(buffer[0 .. buffer.len - min_size + 1], 0xaa),
}
}

val.writeToPackedMemory(old_ty, mod, buffer, 0) catch |err| switch (err) {
error.OutOfMemory => return error.OutOfMemory,
error.ReinterpretDeclRef => return null,
};

break :offset if (endian == .Big) buffer.len - field_size else 0;
},
.Auto => unreachable,
};

return Value.readFromMemory(field_ty, mod, buffer[offset..], sema.arena) catch |err| switch (err) {
error.OutOfMemory => return error.OutOfMemory,
error.IllDefinedMemoryLayout => unreachable,
error.Unimplemented => return sema.fail(block, src, "TODO: implement readFromMemory for type '{}'", .{field_ty.fmt(mod)}),
};
}

fn coerceArrayPtrToSlice(
sema: *Sema,
block: *Block,
Expand Down
24 changes: 17 additions & 7 deletions src/TypedValue.zig
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,27 @@ pub fn print(
if (level == 0) {
return writer.writeAll(".{ ... }");
}
const union_val = val.castTag(.@"union").?.data;
const payload = val.castTag(.@"union").?.data;
try writer.writeAll(".{ ");

if (union_val.tag.toIntern() != .none) {
if (payload.tag) |tag| {
try print(.{
.ty = ip.indexToKey(ty.toIntern()).union_type.enum_tag_ty.toType(),
.val = union_val.tag,
.val = tag,
}, writer, level - 1, mod);
try writer.writeAll(" = ");
const field_ty = ty.unionFieldType(union_val.tag, mod).?;
const field_ty = ty.unionFieldType(tag, mod).?;
try print(.{
.ty = field_ty,
.val = union_val.val,
.val = payload.val,
}, writer, level - 1, mod);
} else {
return writer.writeAll("(unknown tag)");
try writer.writeAll("(unknown tag) = ");
const backing_ty = try ty.unionBackingType(mod);
try print(.{
.ty = backing_ty,
.val = payload.val,
}, writer, level - 1, mod);
}

return writer.writeAll(" }");
Expand Down Expand Up @@ -421,7 +426,12 @@ pub fn print(
.val = un.val.toValue(),
}, writer, level - 1, mod);
} else {
try writer.writeAll("(unknown tag)");
try writer.writeAll("(unknown tag) = ");
const backing_ty = try ty.unionBackingType(mod);
try print(.{
.ty = backing_ty,
.val = un.val.toValue(),
}, writer, level - 1, mod);
}
} else try writer.writeAll("...");
return writer.writeAll(" }");
Expand Down
10 changes: 10 additions & 0 deletions src/type.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1954,6 +1954,16 @@ pub const Type = struct {
return true;
}

/// Returns the type used for backing storage of this union during comptime operations.
/// Asserts the type is either an extern or packed union.
pub fn unionBackingType(ty: Type, mod: *Module) !Type {
return switch (ty.containerLayout(mod)) {
.Extern => try mod.arrayType(.{ .len = ty.abiSize(mod), .child = .u8_type }),
.Packed => try mod.intType(.unsigned, @intCast(ty.bitSize(mod))),
.Auto => unreachable,
};
}

pub fn unionGetLayout(ty: Type, mod: *Module) Module.UnionLayout {
const ip = &mod.intern_pool;
const union_type = ip.indexToKey(ty.toIntern()).union_type;
Expand Down
47 changes: 25 additions & 22 deletions src/value.zig
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,19 @@ pub const Value = struct {
},
.@"union" => {
const pl = val.castTag(.@"union").?.data;
return mod.intern(.{ .un = .{
.ty = ty.toIntern(),
.tag = try pl.tag.intern(ty.unionTagTypeHypothetical(mod), mod),
.val = try pl.val.intern(ty.unionFieldType(pl.tag, mod).?, mod),
} });
if (pl.tag) |pl_tag| {
return mod.intern(.{ .un = .{
.ty = ty.toIntern(),
.tag = try pl_tag.intern(ty.unionTagTypeHypothetical(mod), mod),
.val = try pl.val.intern(ty.unionFieldType(pl_tag, mod).?, mod),
} });
} else {
return mod.intern(.{ .un = .{
.ty = ty.toIntern(),
.tag = .none,
.val = try pl.val.intern(try ty.unionBackingType(mod), mod),
} });
}
},
}
}
Expand Down Expand Up @@ -399,10 +407,7 @@ pub const Value = struct {

.un => |un| Tag.@"union".create(arena, .{
// toValue asserts that the value cannot be .none which is valid on unions.
.tag = .{
.ip_index = un.tag,
.legacy = undefined,
},
.tag = if (un.tag == .none) null else un.tag.toValue(),
.val = un.val.toValue(),
}),

Expand Down Expand Up @@ -709,21 +714,22 @@ pub const Value = struct {
.Union => switch (ty.containerLayout(mod)) {
.Auto => return error.IllDefinedMemoryLayout, // Sema is supposed to have emitted a compile error already
.Extern => {
const union_obj = mod.typeToUnion(ty).?;
if (val.unionTag(mod)) |union_tag| {
const union_obj = mod.typeToUnion(ty).?;
const field_index = mod.unionTagFieldIndex(union_obj, union_tag).?;
const field_type = union_obj.field_types.get(&mod.intern_pool)[field_index].toType();
const field_val = try val.fieldValue(mod, field_index);
const byte_count = @as(usize, @intCast(field_type.abiSize(mod)));
return writeToMemory(field_val, field_type, mod, buffer[0..byte_count]);
} else {
const union_size = ty.abiSize(mod);
const array_type = try mod.arrayType(.{ .len = union_size, .child = .u8_type });
return writeToMemory(val.unionValue(mod), array_type, mod, buffer[0..@as(usize, @intCast(union_size))]);
const backing_ty = try ty.unionBackingType(mod);
const byte_count: usize = @intCast(backing_ty.abiSize(mod));
return writeToMemory(val.unionValue(mod), backing_ty, mod, buffer[0..byte_count]);
}
},
.Packed => {
const byte_count = (@as(usize, @intCast(ty.bitSize(mod))) + 7) / 8;
const backing_ty = try ty.unionBackingType(mod);
const byte_count: usize = @intCast(backing_ty.abiSize(mod));
return writeToPackedMemory(val, ty, mod, buffer[0..byte_count], 0);
},
},
Expand Down Expand Up @@ -842,9 +848,8 @@ pub const Value = struct {
const field_val = try val.fieldValue(mod, field_index);
return field_val.writeToPackedMemory(field_type, mod, buffer, bit_offset);
} else {
const union_bits: u16 = @intCast(ty.bitSize(mod));
const int_ty = try mod.intType(.unsigned, union_bits);
return val.unionValue(mod).writeToPackedMemory(int_ty, mod, buffer, bit_offset);
const backing_ty = try ty.unionBackingType(mod);
return val.unionValue(mod).writeToPackedMemory(backing_ty, mod, buffer, bit_offset);
}
},
}
Expand Down Expand Up @@ -1146,10 +1151,8 @@ pub const Value = struct {
.Union => switch (ty.containerLayout(mod)) {
.Auto, .Extern => unreachable, // Handled by non-packed readFromMemory
.Packed => {
const union_bits: u16 = @intCast(ty.bitSize(mod));
assert(union_bits != 0);
const int_ty = try mod.intType(.unsigned, union_bits);
const val = (try readFromPackedMemory(int_ty, mod, buffer, bit_offset, arena)).toIntern();
const backing_ty = try ty.unionBackingType(mod);
const val = (try readFromPackedMemory(backing_ty, mod, buffer, bit_offset, arena)).toIntern();
return (try mod.intern(.{ .un = .{
.ty = ty.toIntern(),
.tag = .none,
Expand Down Expand Up @@ -4017,7 +4020,7 @@ pub const Value = struct {
data: Data,

pub const Data = struct {
tag: Value,
tag: ?Value,
val: Value,
};
};
Expand Down
Loading