-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support pointer subtraction #20089
support pointer subtraction #20089
Conversation
A maintainer will have to repush the "update zig1.wasm" commit. |
I don't think a dedicated AIR instruction is needed (unless for safety checks in the backend?) but it should do something at comptime since that is mentioned as a reason against just using |
It now works at comptime as well if it knows the byte offset. |
I think it needs to check that the pointers point to the same object for the operation to make sense at comptime. Otherwise it'll have to fall back to doing it at runtime. |
Good point. Could also add some basic "safety" for comptime at least, checking overflow. |
I'm not sure if the problem is comptime {
var a: struct { a: u32, b: u32 } = undefined;
const p1 = &a.a;
const p2 = &a.b;
@compileLog(p2 - p1); // runtime because p1 and p2 have different base addresses
}
comptime {
var a: [64][64]u8 = undefined;
const p1 = &a[0][12];
const p2 = &a[15][3];
@compileLog(p2 - p1); // comptime because p1 and p2 are offset from the same comptime alloc
} |
Passes now |
But it results in a wrong value since pub fn main() void {
var a: struct { a: u32, b: u32 } = undefined;
const p1 = &a.a;
const p2 = &a.b;
std.debug.print("{}\n", .{@intFromPtr(p2) - @intFromPtr(p1)}); // 4
} |
Well that's true of course. I considered using The other option would be to change So instead I would only allow |
482d013
to
470e9d6
Compare
I reverted the |
Fixes #1738
I don't know if this should introduce a dedicated AIR instruction instead of emitting two
int_from_ptr
and asub_wrap
. This is also missing safety just like the existing pointer arithmetic but maybe that can be done as part of #1918? Otherwise, if those two things aren't a blocking issue I think this is more or less complete. It seems to work at least.I also put an emphasis on introducing a clear distinction between "pointer-integer arithmetic" and "pointer subtraction" (pointer-pointer arithmetic) because both of those are "pointer arithmetic" so "pointer arithmetic" on its own becomes ambiguous.