feeley at iro.umontreal.ca
Thu Oct 8 16:14:20 EDT 2009
On 2009-10-08, at 1:11 PM, Michele Zaffalon wrote:
> Sorry, I didn't express myself clearly. What can I do: I am just a
> (define gsl-func2
> (c-lambda ( (function (int) int) int) int "gsl_func"))
> the first argument of gsl-func2 is a function f defined on the
> Scheme side.
> The C interface now produces a C function helper_func that is passed
> to gsl_func: this helper_func does nothing else that calling the
> Scheme function f on the parameter, just in the same way *f* was used.
> What I want to say in my broken way is: if it works with the global
> *f*, which is not yet defined, why can't it work with f?
> Or in other words, the Scheme f with signature (funtion (int) int) is
> magically converted to the C function supplied gsl_func and this C
> function returns f(x).
I'm not sure this will help but, here goes...
The representation of a "function" is different for Gambit an C. In
C, a function "value" is normally represented as the address of the
machine code that implements this function. The C compiler generates
the "call" machine instruction to call functions represented this
way. On the Gambit side, a function value is represented as the
address of the "closure object" which contains the environment of the
closure and the code pointer. You can't use the "call" machine
instruction to call such functions. Even in a Scheme system where
closures are represented as a piece of code (this is possible, in fact
the first versions of Gambit used this representation) you also have
the problem that the parameter passing protocol may be different in C
and Scheme, and it probably has to be to support "rest parameters".
So... when a Scheme function is passed to the C world, as a C
function, there is a real conversion that must occur because the
underlying representations and parameter passing protocol are
different in the two worlds.
Does that clarify things?
More information about the Gambit-list