[Ecls-list] FFI: missing declaration garbles return value
Michael Wildpaner
mike at rainbow.studorg.tuwien.ac.at
Sat Feb 11 11:54:00 UTC 2006
Hi,
when declaring C foreign functions with the FFI, no function signature of
the called function is fed to the C compiler.
A similar issue was already discussed under the subject "floats and uffi"
in September.
If the calling convention of a particular platform does not coincide with
the default declaration of a function, especially the default return type
of 'int', garbage is produced.
To reproduce on Linux i386 with gcc:
foo.c:
double returns_double(void) {
return 1.0;
}
use-foo.lisp:
(ffi:load-foreign-library "libfoo.so")
(ffi:def-function "returns_double" () :returning :double)
(format t "value is ~A~&" (returns-double))
(quit)
When testing with:
gcc -Wall -g -O2 -fPIC -fstrict-aliasing -c foo.c -o foo.o
gcc -o libfoo.so foo.o -shared -lm
LD_LIBRARY_PATH=$PWD
export LD_LIBRARY
ecl -c use-foo.c -h use-foo.h -compile use-foo
ecl -load use-foo
the following output is produced:
value is 1.35607592d8
instead of the expected 1.0, obviously garbage and not a rounding error.
The generated use-foo.c contains the following line:
value0=make_longfloat(returns_double()); NVALUES=1;
The implicit declaration of 'int returns_double()' makes the compiler
generate a implicit cast of the return value from int to double. As the
return value is already a double, it is trashed when calling
make_longfloat (correctly declared as taking a double).
As a stop-gap measure, I will resort to (c-lines "#include \"foo.h\") to
manually pre-declare the functions.
Do you think it would be feasible to make def-function auto-declare the
functions it calls? The information on parameter types seems to be there.
Best wishes, Mike
--
If you don't breathe, there is no air. DI Michael Wildpaner
If you don't walk, there is no earth. Ph.D. Student
If you don't speak, there is no world.
-- Navajo (Dineh) wisdom
More information about the ecl-devel
mailing list