Skip to content

Casting: Implement cptr_to_i64 and i64_to_cptr #1736

Closed
@certik

Description

@certik

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.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions