zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit aca42c62598523b92de7a51d3d84f2f2e5146536 (tree)
parent 74ccd0c40b6871093f52769d342c1316e9ded0c0
Author: Andrew Kelley <andrew@ziglang.org>
Date:   Wed, 23 Mar 2022 19:58:13 -0700

Sema: fix comptime elem_ptr compare fixed address

Diffstat:
Msrc/value.zig | 30++++++++++++++++++++++++++++--
Mtest/behavior/pointers.zig | 5++---
2 files changed, 30 insertions(+), 5 deletions(-)

diff --git a/src/value.zig b/src/value.zig @@ -1008,7 +1008,7 @@ pub const Value = extern union { space: *BigIntSpace, target: Target, sema_kit: ?Module.WipAnalysis, - ) !BigIntConst { + ) Module.CompileError!BigIntConst { switch (val.tag()) { .zero, .bool_false, @@ -1035,6 +1035,14 @@ pub const Value = extern union { return BigIntMutable.init(&space.limbs, x).toConst(); }, + .elem_ptr => { + const elem_ptr = val.castTag(.elem_ptr).?.data; + const array_addr = (try elem_ptr.array_ptr.getUnsignedIntAdvanced(target, sema_kit)).?; + const elem_size = elem_ptr.elem_ty.abiSize(target); + const new_addr = array_addr + elem_size * elem_ptr.index; + return BigIntMutable.init(&space.limbs, new_addr).toConst(); + }, + else => unreachable, } } @@ -1815,7 +1823,10 @@ pub const Value = extern union { return orderAgainstZeroAdvanced(lhs, null) catch unreachable; } - pub fn orderAgainstZeroAdvanced(lhs: Value, sema_kit: ?Module.WipAnalysis) !std.math.Order { + pub fn orderAgainstZeroAdvanced( + lhs: Value, + sema_kit: ?Module.WipAnalysis, + ) Module.CompileError!std.math.Order { return switch (lhs.tag()) { .zero, .bool_false, @@ -1851,6 +1862,21 @@ pub const Value = extern union { .float_80 => std.math.order(lhs.castTag(.float_80).?.data, 0), .float_128 => std.math.order(lhs.castTag(.float_128).?.data, 0), + .elem_ptr => { + const elem_ptr = lhs.castTag(.elem_ptr).?.data; + switch (try elem_ptr.array_ptr.orderAgainstZeroAdvanced(sema_kit)) { + .lt => unreachable, + .gt => return .gt, + .eq => { + if (elem_ptr.index == 0) { + return .eq; + } else { + return .gt; + } + }, + } + }, + else => unreachable, }; } diff --git a/test/behavior/pointers.zig b/test/behavior/pointers.zig @@ -366,11 +366,10 @@ test "pointer sentinel with +inf" { } test "pointer to array at fixed address" { - if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO - - const array = @intToPtr(*volatile [1]u32, 0x10); + const array = @intToPtr(*volatile [2]u32, 0x10); // Silly check just to reference `array` try expect(@ptrToInt(&array[0]) == 0x10); + try expect(@ptrToInt(&array[1]) == 0x14); } test "pointer arithmetic affects the alignment" {