Skip to content

Casting: Implement cptr_to_i64 and i64_to_cptr #1736

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

Closed
Tracked by #1600
certik opened this issue Apr 21, 2023 · 0 comments · Fixed by #1848
Closed
Tracked by #1600

Casting: Implement cptr_to_i64 and i64_to_cptr #1736

certik opened this issue Apr 21, 2023 · 0 comments · Fixed by #1848
Assignees

Comments

@certik
Copy link
Contributor

certik commented Apr 21, 2023

We need some native LPython way to do the following casts:

@ccall
def cptr_to_i64(p: CPtr) -> i64:
    pass

@ccall
def i64_to_cptr(p: i64) -> CPtr:
    pass

def increment_p_i16(p: CPtr, n: i32) -> CPtr:
    q: i64 = cptr_to_i64(p)
    q += i64(n * 2)
    return i64_to_cptr(q)

Currently they are implemented using:

uint64_t cptr_to_i64(void *p) {
    return (uint64_t)p;
}

void* i64_to_cptr(uint64_t p) {
    return (void*)p;
}

Rather, I would like to just do:

from lpython import cptr_to_i64, i64_to_cptr

def increment_p_i16(p: CPtr, n: i32) -> CPtr:
    q: i64 = cptr_to_i64(p)
    q += i64(n * 2)
    return i64_to_cptr(q)

And LPython will insert appropriate casting nodes in ASR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants