windelbouwman / ppci

A compiler for ARM, X86, MSP430, xtensa and more implemented in pure Python
https://ppci.readthedocs.io/en/latest/
BSD 2-Clause "Simplified" License
337 stars 36 forks source link

Pass pointer #123

Closed davidbrochart closed 3 years ago

davidbrochart commented 3 years ago

Is it possible to pass a pointer to a C jitted function from Python? I tried naively with this C function signature:

void x(double* ptr);

but I get an error in Python:

m.x(94557793454592)
# ArgumentError: argument 1: <class 'TypeError'>: expected LP_c_double instance instead of int

I also tried to write the pointer directly in the C code:

void x() {
    double* ptr = 94557793454592;
}

but I get this error:

m.x()
# error: 'I' format requires 0 <= number <= 4294967295
davidbrochart commented 3 years ago

The pointer has to be passed from Python using ctypes, e.g.:

import ctypes
a = ctypes.c_double()
ptr = ctypes.pointer(a)
m.x(ptr)

Thanks @windelbouwman for the help!