[clean-list] foreign export for linux?

John van Groningen johnvg at cs.ru.nl
Thu Aug 23 16:21:49 MEST 2007


A patch that extends the foreign export of Clean 2.2 on linux (IA32)
is available at:

http://clean.cs.ru.nl/download/Clean22/linux/Clean2.2_foreignexport_patch.tar.gz


To install the patch:

- replace the files:

   _startup.o, _startupProfile.o and _startupTrace.o

   in

   clean/lib/stdenv/Clean System Files

   with the new files in the .tar.gz file.

- replace the files:

   cg and cocl

   in

   clean/lib/exe/

   with the new files in the .tar.gz file.

The src directory of the .tar.gz file contains the modified source files.



Clean functions using integers, reals, strings, unboxed arrays of integers
or reals and tuples can be called from c using foreign export, for example:

	implementation module f;
	import StdEnv;

	foreign export f;

	f :: !Int !Int -> Int;
	f a b = sum [a..b];

	call_f :: !Int !Int -> Int;
	call_f a b = code {
		ccall f "GII:I"
	};

	Start = call_f 1 20;

	definition module f;

	f :: !Int !Int -> Int;

	will call the function f using the c calling convention.

Functions exported using foreign export can only be called from c if
the c code is called from Clean using a ccall with a 'G' as first
character of the string. htoclean does not yet generate these 'G's.

Real's are passed to/from c as type double. String's are passed as
CleanString, as defined in doc/Examples/Clean.h, so as a pointer
to an integer representing the length in characters of the string,
followed by the characters (no extra '\0' at the end).

If a string is passed from c to Clean, a copy is made in the Clean heap,
and this copy is used. If a string is passed from Clean to c, the c
function is passed a pointer to a string inside Clean's heap. This pointer
should not be used any more after the c function returns, because the
garbage collector may move or deallocate the string. The c programmer
is responsible for releasing any memory allocated for these strings
in c, because the foreign export interface does not call functions that
free memory, like free or LocalFree.

If an array is passed from Clean to c, the c function is passed a pointer to
an array inside Clean's heap. This pointer should not be used any more after
the c function returns, because the garbage collector may move or deallocate
the array.

To pass an array from c to Clean, the int (4 bytes) at (byte) offset -8,
should contain the number of elements of the array. A copy of this array is
made in the Clean heap when the c function returns, and this copy is used.

The c programmer is responsible for releasing any memory allocated
for these arrays in c, because the foreign export interface does not call
functions that free memory, like free or LocalFree.

If the result is a tuple, the c function yields void, and for each
element of the result tuple an extra argument is passed to the c function
(at the end) with a pointer that contains the address where the result should
be stored.
For example for a (!Int,!Real) result, arguments of type *int and *double
are added.

See also doc/CallingCFromClean.html and doc/Examples.

Kind regards,

John van Groningen




More information about the clean-list mailing list