Sema: fix comptime elem_ptr compare fixed address

This commit is contained in:
Andrew Kelley 2022-03-23 19:58:13 -07:00
parent 74ccd0c40b
commit aca42c6259
2 changed files with 30 additions and 5 deletions

View File

@ -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,
};
}

View File

@ -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" {