mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 06:13:07 +00:00
We've got a big one here! This commit reworks how we represent pointers in the InternPool, and rewrites the logic for loading and storing from them at comptime. Firstly, the pointer representation. Previously, pointers were represented in a highly structured manner: pointers to fields, array elements, etc, were explicitly represented. This works well for simple cases, but is quite difficult to handle in the cases of unusual reinterpretations, pointer casts, offsets, etc. Therefore, pointers are now represented in a more "flat" manner. For types without well-defined layouts -- such as comptime-only types, automatic-layout aggregates, and so on -- we still use this "hierarchical" structure. However, for types with well-defined layouts, we use a byte offset associated with the pointer. This allows the comptime pointer access logic to deal with reinterpreted pointers far more gracefully, because the "base address" of a pointer -- for instance a `field` -- is a single value which pointer accesses cannot exceed since the parent has undefined layout. This strategy is also more useful to most backends -- see the updated logic in `codegen.zig` and `codegen/llvm.zig`. For backends which do prefer a chain of field and elements accesses for lowering pointer values, such as SPIR-V, there is a helpful function in `Value` which creates a strategy to derive a pointer value using ideally only field and element accesses. This is actually more correct than the previous logic, since it correctly handles pointer casts which, after the dust has settled, end up referring exactly to an aggregate field or array element. In terms of the pointer access code, it has been rewritten from the ground up. The old logic had become rather a mess of special cases being added whenever bugs were hit, and was still riddled with bugs. The new logic was written to handle the "difficult" cases correctly, the most notable of which is restructuring of a comptime-only array (for instance, converting a `[3][2]comptime_int` to a `[2][3]comptime_int`. Currently, the logic for loading and storing work somewhat differently, but a future change will likely improve the loading logic to bring it more in line with the store strategy. As far as I can tell, the rewrite has fixed all bugs exposed by #19414. As a part of this, the comptime bitcast logic has also been rewritten. Previously, bitcasts simply worked by serializing the entire value into an in-memory buffer, then deserializing it. This strategy has two key weaknesses: pointers, and undefined values. Representations of these values at comptime cannot be easily serialized/deserialized whilst preserving data, which means many bitcasts would become runtime-known if pointers were involved, or would turn `undefined` values into `0xAA`. The new logic works by "flattening" the datastructure to be cast into a sequence of bit-packed atomic values, and then "unflattening" it; using serialization when necessary, but with special handling for `undefined` values and for pointers which align in virtual memory. The resulting code is definitely slower -- more on this later -- but it is correct. The pointer access and bitcast logic required some helper functions and types which are not generally useful elsewhere, so I opted to split them into separate files `Sema/comptime_ptr_access.zig` and `Sema/bitcast.zig`, with simple re-exports in `Sema.zig` for their small public APIs. Whilst working on this branch, I caught various unrelated bugs with transitive Sema errors, and with the handling of `undefined` values. These bugs have been fixed, and corresponding behavior test added. In terms of performance, I do anticipate that this commit will regress performance somewhat, because the new pointer access and bitcast logic is necessarily more complex. I have not yet taken performance measurements, but will do shortly, and post the results in this PR. If the performance regression is severe, I will do work to to optimize the new logic before merge. Resolves: #19452 Resolves: #19460
243 lines
7.4 KiB
Zig
243 lines
7.4 KiB
Zig
const builtin = @import("builtin");
|
|
const std = @import("std");
|
|
const expect = std.testing.expect;
|
|
const expectEqual = std.testing.expectEqual;
|
|
const maxInt = std.math.maxInt;
|
|
|
|
test "@intCast i32 to u7" {
|
|
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
|
|
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
|
|
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
|
|
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
|
|
|
|
var x: u128 = maxInt(u128);
|
|
var y: i32 = 120;
|
|
_ = .{ &x, &y };
|
|
const z = x >> @as(u7, @intCast(y));
|
|
try expect(z == 0xff);
|
|
}
|
|
|
|
test "coerce i8 to i32 and @intCast back" {
|
|
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
|
|
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
|
|
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
|
|
|
|
var x: i8 = -5;
|
|
var y: i32 = -5;
|
|
_ = .{ &x, &y };
|
|
try expect(y == x);
|
|
|
|
var x2: i32 = -5;
|
|
var y2: i8 = -5;
|
|
_ = .{ &x2, &y2 };
|
|
try expect(y2 == @as(i8, @intCast(x2)));
|
|
}
|
|
|
|
test "coerce non byte-sized integers accross 32bits boundary" {
|
|
{
|
|
var v: u21 = 6417;
|
|
_ = &v;
|
|
const a: u32 = v;
|
|
const b: u64 = v;
|
|
const c: u64 = a;
|
|
var w: u64 = 0x1234567812345678;
|
|
_ = &w;
|
|
const d: u21 = @truncate(w);
|
|
const e: u60 = d;
|
|
try expectEqual(@as(u32, 6417), a);
|
|
try expectEqual(@as(u64, 6417), b);
|
|
try expectEqual(@as(u64, 6417), c);
|
|
try expectEqual(@as(u21, 0x145678), d);
|
|
try expectEqual(@as(u60, 0x145678), e);
|
|
}
|
|
|
|
{
|
|
var v: u10 = 234;
|
|
_ = &v;
|
|
const a: u32 = v;
|
|
const b: u64 = v;
|
|
const c: u64 = a;
|
|
var w: u64 = 0x1234567812345678;
|
|
_ = &w;
|
|
const d: u10 = @truncate(w);
|
|
const e: u60 = d;
|
|
try expectEqual(@as(u32, 234), a);
|
|
try expectEqual(@as(u64, 234), b);
|
|
try expectEqual(@as(u64, 234), c);
|
|
try expectEqual(@as(u21, 0x278), d);
|
|
try expectEqual(@as(u60, 0x278), e);
|
|
}
|
|
{
|
|
var v: u7 = 11;
|
|
_ = &v;
|
|
const a: u32 = v;
|
|
const b: u64 = v;
|
|
const c: u64 = a;
|
|
var w: u64 = 0x1234567812345678;
|
|
_ = &w;
|
|
const d: u7 = @truncate(w);
|
|
const e: u60 = d;
|
|
try expectEqual(@as(u32, 11), a);
|
|
try expectEqual(@as(u64, 11), b);
|
|
try expectEqual(@as(u64, 11), c);
|
|
try expectEqual(@as(u21, 0x78), d);
|
|
try expectEqual(@as(u60, 0x78), e);
|
|
}
|
|
|
|
{
|
|
var v: i21 = -6417;
|
|
_ = &v;
|
|
const a: i32 = v;
|
|
const b: i64 = v;
|
|
const c: i64 = a;
|
|
var w: i64 = -12345;
|
|
_ = &w;
|
|
const d: i21 = @intCast(w);
|
|
const e: i60 = d;
|
|
try expectEqual(@as(i32, -6417), a);
|
|
try expectEqual(@as(i64, -6417), b);
|
|
try expectEqual(@as(i64, -6417), c);
|
|
try expectEqual(@as(i21, -12345), d);
|
|
try expectEqual(@as(i60, -12345), e);
|
|
}
|
|
|
|
{
|
|
var v: i10 = -234;
|
|
_ = &v;
|
|
const a: i32 = v;
|
|
const b: i64 = v;
|
|
const c: i64 = a;
|
|
var w: i64 = -456;
|
|
_ = &w;
|
|
const d: i10 = @intCast(w);
|
|
const e: i60 = d;
|
|
try expectEqual(@as(i32, -234), a);
|
|
try expectEqual(@as(i64, -234), b);
|
|
try expectEqual(@as(i64, -234), c);
|
|
try expectEqual(@as(i10, -456), d);
|
|
try expectEqual(@as(i60, -456), e);
|
|
}
|
|
{
|
|
var v: i7 = -11;
|
|
_ = &v;
|
|
const a: i32 = v;
|
|
const b: i64 = v;
|
|
const c: i64 = a;
|
|
var w: i64 = -42;
|
|
_ = &w;
|
|
const d: i7 = @intCast(w);
|
|
const e: i60 = d;
|
|
try expectEqual(@as(i32, -11), a);
|
|
try expectEqual(@as(i64, -11), b);
|
|
try expectEqual(@as(i64, -11), c);
|
|
try expectEqual(@as(i7, -42), d);
|
|
try expectEqual(@as(i60, -42), e);
|
|
}
|
|
}
|
|
|
|
const Piece = packed struct {
|
|
color: Color,
|
|
type: Type,
|
|
|
|
const Type = enum(u3) { KING, QUEEN, BISHOP, KNIGHT, ROOK, PAWN };
|
|
const Color = enum(u1) { WHITE, BLACK };
|
|
|
|
fn charToPiece(c: u8) !@This() {
|
|
return .{
|
|
.type = try charToPieceType(c),
|
|
.color = if (std.ascii.isUpper(c)) Color.WHITE else Color.BLACK,
|
|
};
|
|
}
|
|
|
|
fn charToPieceType(c: u8) !Type {
|
|
return switch (std.ascii.toLower(c)) {
|
|
'p' => .PAWN,
|
|
'k' => .KING,
|
|
'q' => .QUEEN,
|
|
'b' => .BISHOP,
|
|
'n' => .KNIGHT,
|
|
'r' => .ROOK,
|
|
else => error.UnexpectedCharError,
|
|
};
|
|
}
|
|
};
|
|
|
|
test "load non byte-sized optional value" {
|
|
// Originally reported at https://github.com/ziglang/zig/issues/14200
|
|
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
|
|
|
|
// note: this bug is triggered by the == operator, expectEqual will hide it
|
|
const opt: ?Piece = try Piece.charToPiece('p');
|
|
try expect(opt.?.type == .PAWN);
|
|
try expect(opt.?.color == .BLACK);
|
|
|
|
var p: Piece = undefined;
|
|
@as(*u8, @ptrCast(&p)).* = 0b11111011;
|
|
try expect(p.type == .PAWN);
|
|
try expect(p.color == .BLACK);
|
|
}
|
|
|
|
test "load non byte-sized value in struct" {
|
|
if (builtin.cpu.arch.endian() != .little) return error.SkipZigTest; // packed struct TODO
|
|
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
|
|
|
|
// note: this bug is triggered by the == operator, expectEqual will hide it
|
|
// using ptrCast not to depend on unitialised memory state
|
|
|
|
var struct0: struct {
|
|
p: Piece,
|
|
int: u8,
|
|
} = undefined;
|
|
@as(*u8, @ptrCast(&struct0.p)).* = 0b11111011;
|
|
try expect(struct0.p.type == .PAWN);
|
|
try expect(struct0.p.color == .BLACK);
|
|
|
|
var struct1: packed struct {
|
|
p0: Piece,
|
|
p1: Piece,
|
|
pad: u1,
|
|
p2: Piece,
|
|
} = undefined;
|
|
@as(*u8, @ptrCast(&struct1.p0)).* = 0b11111011;
|
|
struct1.p1 = try Piece.charToPiece('p');
|
|
struct1.p2 = try Piece.charToPiece('p');
|
|
try expect(struct1.p0.type == .PAWN);
|
|
try expect(struct1.p0.color == .BLACK);
|
|
try expect(struct1.p1.type == .PAWN);
|
|
try expect(struct1.p1.color == .BLACK);
|
|
try expect(struct1.p2.type == .PAWN);
|
|
try expect(struct1.p2.color == .BLACK);
|
|
}
|
|
|
|
test "load non byte-sized value in union" {
|
|
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
|
|
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
|
|
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest;
|
|
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
|
|
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
|
|
|
|
// note: this bug is triggered by the == operator, expectEqual will hide it
|
|
// using ptrCast not to depend on unitialised memory state
|
|
|
|
var union0: packed union {
|
|
p: Piece,
|
|
int: u8,
|
|
} = .{ .int = 0 };
|
|
union0.int = 0b11111011;
|
|
try expect(union0.p.type == .PAWN);
|
|
try expect(union0.p.color == .BLACK);
|
|
|
|
var union1: union {
|
|
p: Piece,
|
|
int: u8,
|
|
} = .{ .p = .{ .color = .WHITE, .type = .KING } };
|
|
@as(*u8, @ptrCast(&union1.p)).* = 0b11111011;
|
|
try expect(union1.p.type == .PAWN);
|
|
try expect(union1.p.color == .BLACK);
|
|
|
|
var pieces: [3]Piece = undefined;
|
|
@as(*u8, @ptrCast(&pieces[1])).* = 0b11111011;
|
|
try expect(pieces[1].type == .PAWN);
|
|
try expect(pieces[1].color == .BLACK);
|
|
}
|