[MLton-devel] cvs commit: calling SML from C

Stephen Weeks MLton@mlton.org
Mon, 24 Mar 2003 20:37:44 -0800


Here's a simple example of the new callback mechanism.  z.sml and f.c
are appended below.

% /home/sweeks/mlton/bin/mlton \
	-keep g \
	-native false \
	-verbose 1 \
	z.sml f.c
MLton MLTONVERSION (built Mon Mar 24 19:56:20 2003 on eponym)
MLton starting
   Compile SML starting
      pre codegen starting
      pre codegen finished in 3.41 + 0.00 (0% GC)
      C code gen starting
      C code gen finished in 0.03 + 0.00 (0% GC)
   Compile SML finished in 3.44 + 0.00 (0% GC)
   Compile C starting
      gcc -S -I/home/sweeks/mlton/src/build/bin/../lib/self/include -O1 \
          -malign-functions=5 -malign-jumps=2 -fno-strict-aliasing \
          -fno-strength-reduce -fomit-frame-pointer -fschedule-insns \
          -fschedule-insns2 -malign-loops=2 -mcpu=pentiumpro -w -o \
          /tmp/fileoTezcL.s z.c
   Compile C finished in 1.28 + 0.00 (0% GC)
   Assemble starting
      gcc -c -o /tmp/filecFuzUZ.o /tmp/fileoTezcL.s
   Assemble finished in 0.03 + 0.00 (0% GC)
   Link starting
      gcc -o z /tmp/filecFuzUZ.o f.c \
          -L/home/sweeks/mlton/src/build/bin/../lib/self -lmlton -lm \
          /usr/lib/libgmp.a
   Link finished in 0.10 + 0.00 (0% GC)
MLton finished in 4.85 + 0.00 (0% GC)
% z @MLton --
calling f
f calling SML
C called me
f calling SML again
C called me
f done calling SML
done calling f
calling f
f calling SML
C called me
f calling SML again
C called me
f done calling SML
done calling f


------------------------------------------------------------
(* z.sml *)

val f = _ffi "f": unit -> unit;

val _ = MLton.FFI.handleCallFromC (fn () => print "C called me\n")

val f =
  fn () =>
  (print "calling f\n"
   ; f ()
   ; print "done calling f\n")

val _ = f ()
val _ = f ()
------------------------------------------------------------
/* f.c */
#include <stdio.h>

void MLton_callFromC ();

void f () {
	fprintf (stderr, "f calling SML\n");
	MLton_callFromC ();
	fprintf (stderr, "f calling SML again\n");
	MLton_callFromC ();
	fprintf (stderr, "f done calling SML\n");
}
------------------------------------------------------------


-------------------------------------------------------
This SF.net email is sponsored by:
The Definitive IT and Networking Event. Be There!
NetWorld+Interop Las Vegas 2003 -- Register today!
http://ads.sourceforge.net/cgi-bin/redirect.pl?keyn0001en
_______________________________________________
MLton-devel mailing list
MLton-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mlton-devel