aboutsummaryrefslogtreecommitdiff
path: root/src/codegen/c.zig
diff options
context:
space:
mode:
authorJustus Klausecker <justus@klausecker.de>2025-08-03 13:43:03 +0200
committerJustus Klausecker <justus@klausecker.de>2025-08-12 16:33:57 +0200
commitd0586da18e08d0b8bdc2347fabdc0ba531901641 (patch)
tree97ce80427e777a766cb3e3521141325f1b588e3c /src/codegen/c.zig
parent749f10af49022597d873d41df5c600e97e5c4a37 (diff)
downloadzig-d0586da18e08d0b8bdc2347fabdc0ba531901641.tar.gz
zig-d0586da18e08d0b8bdc2347fabdc0ba531901641.zip
Sema: Improve comptime arithmetic undef handling
This commit expands on the foundations laid by https://github.com/ziglang/zig/pull/23177 and moves even more `Sema`-only functionality from `Value` to `Sema.arith`. Specifically all shift and bitwise operations, `@truncate`, `@bitReverse` and `@byteSwap` have been moved and adapted to the new rules around `undefined`. Especially the comptime shift operations have been basically rewritten, fixing many open issues in the process. New rules applied to operators: * `<<`, `@shlExact`, `@shlWithOverflow`, `>>`, `@shrExact`: compile error if any operand is undef * `<<|`, `~`, `^`, `@truncate`, `@bitReverse`, `@byteSwap`: return undef if any operand is undef * `&`, `|`: Return undef if both operands are undef, turn undef into actual `0xAA` bytes otherwise Additionally this commit canonicalizes the representation of aggregates with all-undefined members in the `InternPool` by disallowing them and enforcing the usage of a single typed `undef` value instead. This reduces the amount of edge cases and fixes a bunch of bugs related to partially undefined vecs. List of operations directly affected by this patch: * `<<`, `<<|`, `@shlExact`, `@shlWithOverflow` * `>>`, `@shrExact` * `&`, `|`, `~`, `^` and their atomic rmw + reduce pendants * `@truncate`, `@bitReverse`, `@byteSwap`
Diffstat (limited to 'src/codegen/c.zig')
-rw-r--r--src/codegen/c.zig10
1 files changed, 5 insertions, 5 deletions
diff --git a/src/codegen/c.zig b/src/codegen/c.zig
index 48ca1a07d8..295eecea29 100644
--- a/src/codegen/c.zig
+++ b/src/codegen/c.zig
@@ -1012,7 +1012,7 @@ pub const DeclGen = struct {
};
const ty = val.typeOf(zcu);
- if (val.isUndefDeep(zcu)) return dg.renderUndefValue(w, ty, location);
+ if (val.isUndef(zcu)) return dg.renderUndefValue(w, ty, location);
const ctype = try dg.ctypeFromType(ty, location.toCTypeKind());
switch (ip.indexToKey(val.toIntern())) {
// types, not values
@@ -4216,7 +4216,7 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
const ptr_val = try f.resolveInst(bin_op.lhs);
const src_ty = f.typeOf(bin_op.rhs);
- const val_is_undef = if (try f.air.value(bin_op.rhs, pt)) |v| v.isUndefDeep(zcu) else false;
+ const val_is_undef = if (try f.air.value(bin_op.rhs, pt)) |v| v.isUndef(zcu) else false;
const w = &f.object.code.writer;
if (val_is_undef) {
@@ -4942,7 +4942,7 @@ fn airDbgVar(f: *Function, inst: Air.Inst.Index) !CValue {
const tag = f.air.instructions.items(.tag)[@intFromEnum(inst)];
const pl_op = f.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
const name: Air.NullTerminatedString = @enumFromInt(pl_op.payload);
- const operand_is_undef = if (try f.air.value(pl_op.operand, pt)) |v| v.isUndefDeep(zcu) else false;
+ const operand_is_undef = if (try f.air.value(pl_op.operand, pt)) |v| v.isUndef(zcu) else false;
if (!operand_is_undef) _ = try f.resolveInst(pl_op.operand);
try reap(f, inst, &.{pl_op.operand});
@@ -7117,7 +7117,7 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
const value = try f.resolveInst(bin_op.rhs);
const elem_ty = f.typeOf(bin_op.rhs);
const elem_abi_size = elem_ty.abiSize(zcu);
- const val_is_undef = if (try f.air.value(bin_op.rhs, pt)) |val| val.isUndefDeep(zcu) else false;
+ const val_is_undef = if (try f.air.value(bin_op.rhs, pt)) |val| val.isUndef(zcu) else false;
const w = &f.object.code.writer;
if (val_is_undef) {
@@ -8338,7 +8338,7 @@ fn formatIntLiteral(data: FormatIntLiteralContext, w: *std.io.Writer) std.io.Wri
defer allocator.free(undef_limbs);
var int_buf: Value.BigIntSpace = undefined;
- const int = if (data.val.isUndefDeep(zcu)) blk: {
+ const int = if (data.val.isUndef(zcu)) blk: {
undef_limbs = allocator.alloc(BigIntLimb, BigInt.calcTwosCompLimbCount(data.int_info.bits)) catch return error.WriteFailed;
@memset(undef_limbs, undefPattern(BigIntLimb));