Commit aca42c6259

Andrew Kelley <andrew@ziglang.org>
2022-03-24 03:58:13
Sema: fix comptime elem_ptr compare fixed address
1 parent 74ccd0c
Changed files (2)
src
test
behavior
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,
         };
     }
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" {