[MLton-user] _ffi and Windows stdcall symbols

Stephen Weeks MLton-user@mlton.org
Sun, 20 Jul 2003 12:47:30 -0700


For other readers of MLton-user, Mike is attempting to port mosmlgl,
http://www.home.gil.com.au/~mthomas/, a Moscow ML binding to OpenGL,
so that it works with MLton.

> Windows stdcall library entry points may be a problem as shown below.
> Presumably either a different specifier is needed or the syntax of _ffi
> should be extended - possibly to include a header file to obtain a
> prototype.

I think the easiest fix is to allow the specificaion of attributes
like cdecl and stdcall for imported functions.  I've placed an
experimental release, 20030720, at http://www.mlton.org/experimental
that does just that.

> Also on a related but more speculative note, maybe now is the best
> time to rationalise the naming of _ffi and _export, perhaps to
> reflect whatever standards might potentially be introduced in the
> forthcoming (unspecified ETA, of course) SML/NJ compiler.

Yeah, I think _import and _export are more uniform.  So I've changed
_ffi to _import in the new release as well.  The old style _ffi
(without attributes) will still work for a while.

Given the rate at which SML/NJ moves these days, I'm not optimistic
that we'll be able to merge our FFIs, but I'll mention it to them.

> The point about the possibility of specifying a header is meant to
> be a completely separate suggestion for the case where MLton
> compiles via C with gcc as opposed to the native code generator,
> rather than a possible solution to the addition of the stdcall
> calling convention to the already existing ccall default for _ffi.

There's no need to specify header files for either the native or C
codegen.  The type of the function plus the attributes is enough to
completely specify the calling convention.  For the C codegen, MLton
generates the C prototypes and includes the __attribute__ ((stdcall))
when needed.

With 20030720, the following program now compiles on Cygwin.

	val glClear = _import "glClear" stdcall: word -> unit;
	val _ = glClear 0w0

Try it out and let us know of any more problems.