authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-23 19:58:13-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-23 19:58:13-07:00
logaca42c62598523b92de7a51d3d84f2f2e5146536
tree18914e86afb1bfcbbd940198087e2c30782eecea
parent74ccd0c40b6871093f52769d342c1316e9ded0c0

Sema: fix comptime elem_ptr compare fixed address


2 files changed, 30 insertions(+), 5 deletions(-)

src/value.zig+28-2
...@@ -1008,7 +1008,7 @@ pub const Value = extern union {...@@ -1008,7 +1008,7 @@ pub const Value = extern union {
1008 space: *BigIntSpace,1008 space: *BigIntSpace,
1009 target: Target,1009 target: Target,
1010 sema_kit: ?Module.WipAnalysis,1010 sema_kit: ?Module.WipAnalysis,
1011 ) !BigIntConst {1011 ) Module.CompileError!BigIntConst {
1012 switch (val.tag()) {1012 switch (val.tag()) {
1013 .zero,1013 .zero,
1014 .bool_false,1014 .bool_false,
...@@ -1035,6 +1035,14 @@ pub const Value = extern union {...@@ -1035,6 +1035,14 @@ pub const Value = extern union {
1035 return BigIntMutable.init(&space.limbs, x).toConst();1035 return BigIntMutable.init(&space.limbs, x).toConst();
1036 },1036 },
10371037
1038 .elem_ptr => {
1039 const elem_ptr = val.castTag(.elem_ptr).?.data;
1040 const array_addr = (try elem_ptr.array_ptr.getUnsignedIntAdvanced(target, sema_kit)).?;
1041 const elem_size = elem_ptr.elem_ty.abiSize(target);
1042 const new_addr = array_addr + elem_size * elem_ptr.index;
1043 return BigIntMutable.init(&space.limbs, new_addr).toConst();
1044 },
1045
1038 else => unreachable,1046 else => unreachable,
1039 }1047 }
1040 }1048 }
...@@ -1815,7 +1823,10 @@ pub const Value = extern union {...@@ -1815,7 +1823,10 @@ pub const Value = extern union {
1815 return orderAgainstZeroAdvanced(lhs, null) catch unreachable;1823 return orderAgainstZeroAdvanced(lhs, null) catch unreachable;
1816 }1824 }
18171825
1818 pub fn orderAgainstZeroAdvanced(lhs: Value, sema_kit: ?Module.WipAnalysis) !std.math.Order {1826 pub fn orderAgainstZeroAdvanced(
1827 lhs: Value,
1828 sema_kit: ?Module.WipAnalysis,
1829 ) Module.CompileError!std.math.Order {
1819 return switch (lhs.tag()) {1830 return switch (lhs.tag()) {
1820 .zero,1831 .zero,
1821 .bool_false,1832 .bool_false,
...@@ -1851,6 +1862,21 @@ pub const Value = extern union {...@@ -1851,6 +1862,21 @@ pub const Value = extern union {
1851 .float_80 => std.math.order(lhs.castTag(.float_80).?.data, 0),1862 .float_80 => std.math.order(lhs.castTag(.float_80).?.data, 0),
1852 .float_128 => std.math.order(lhs.castTag(.float_128).?.data, 0),1863 .float_128 => std.math.order(lhs.castTag(.float_128).?.data, 0),
18531864
1865 .elem_ptr => {
1866 const elem_ptr = lhs.castTag(.elem_ptr).?.data;
1867 switch (try elem_ptr.array_ptr.orderAgainstZeroAdvanced(sema_kit)) {
1868 .lt => unreachable,
1869 .gt => return .gt,
1870 .eq => {
1871 if (elem_ptr.index == 0) {
1872 return .eq;
1873 } else {
1874 return .gt;
1875 }
1876 },
1877 }
1878 },
1879
1854 else => unreachable,1880 else => unreachable,
1855 };1881 };
1856 }1882 }
test/behavior/pointers.zig+2-3
...@@ -366,11 +366,10 @@ test "pointer sentinel with +inf" {...@@ -366,11 +366,10 @@ test "pointer sentinel with +inf" {
366}366}
367367
368test "pointer to array at fixed address" {368test "pointer to array at fixed address" {
369 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO369 const array = @intToPtr(*volatile [2]u32, 0x10);
370
371 const array = @intToPtr(*volatile [1]u32, 0x10);
372 // Silly check just to reference `array`370 // Silly check just to reference `array`
373 try expect(@ptrToInt(&array[0]) == 0x10);371 try expect(@ptrToInt(&array[0]) == 0x10);
372 try expect(@ptrToInt(&array[1]) == 0x14);
374}373}
375374
376test "pointer arithmetic affects the alignment" {375test "pointer arithmetic affects the alignment" {