[MLton-devel] cvs commit: x86 codegen support for calling SML from C

Matthew Fluet MLton@mlton.org
Fri, 28 Mar 2003 21:13:13 -0800


fluet       03/03/28 21:13:13

  Modified:    include  x86codegen.h
  Log:
  After some thought and experiments, it turned out that it suffices to
  completely embed the call to and return from SML in inline assembly in
  the MLton_callFromC function.  The call to SML works just like in
  main; the return from SML looks like a C call to Thread_returnFromC
  from the SML/codegen point of view, but is implemented by putting the
  Thread_returnFromC in the MLton_callFromC inline assembly.  I use
  pusha and popa to push and pop all the general purpose registers,
  which lets gcc believe that nothing funny is going on in the
  assembly.
  
  It works with Stephen's previous example:
  
  [fluet@localhost test]$ mlton.cvs.HEAD -keep g -native true -verbose 1 z.sml f.c
  MLton MLTONVERSION (built Fri Mar 28 19:35:26 2003 on localhost.localdomain)
  MLton starting
     Compile SML starting
        pre codegen starting
        pre codegen finished in 6.75 + 0.00 (0% GC)
        x86 code gen starting
        x86 code gen finished in 1.21 + 0.00 (0% GC)
     Compile SML finished in 7.96 + 0.00 (0% GC)
     Compile C starting
        gcc -S -I/home/fluet/mlton/mlton.cvs.HEAD/build/bin/../lib/self/include \
            -O1 -o /tmp/fileZAlaBY.s z.c
     Compile C finished in 0.28 + 0.00 (0% GC)
     Assemble starting
        gcc -c -o /tmp/file1sb0vi.o /tmp/fileZAlaBY.s
        gcc -c -o /tmp/file3uReD8.o z.0.S
     Assemble finished in 0.11 + 0.00 (0% GC)
     Link starting
        gcc -o z /tmp/file1sb0vi.o /tmp/file3uReD8.o f.c \
            -L/home/fluet/mlton/mlton.cvs.HEAD/build/bin/../lib/self -lmlton \
            -lm /usr/lib/libgmp.a
     Link finished in 0.17 + 0.00 (0% GC)
  MLton finished in 8.52 + 0.00 (0% GC)
  [fluet@localhost test]$ ./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

Revision  Changes    Path
1.26      +36 -2     mlton/include/x86codegen.h

Index: x86codegen.h
===================================================================
RCS file: /cvsroot/mlton/mlton/include/x86codegen.h,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -r1.25 -r1.26
--- x86codegen.h	18 Jan 2003 19:01:10 -0000	1.25
+++ x86codegen.h	29 Mar 2003 05:13:13 -0000	1.26
@@ -27,6 +27,11 @@
 word switchTemp;
 word threadTemp;
 
+#ifndef DEBUG_X86CODEGEN
+#define DEBUG_X86CODEGEN FALSE
+#endif
+
+
 #define Locals(c, d, i, p, u)						\
 	char localuchar[c];						\
 	double localdouble[d];				       		\
@@ -35,6 +40,35 @@
 	uint localuint[u]
 
 #define Main(cs, mg, mfs, mlw, mmc, ps, ml, reserveEsp)			\
+void MLton_callFromC () {						\
+	pointer jump;							\
+	GC_state s;							\
+									\
+	if (DEBUG_X86CODEGEN)						\
+		fprintf (stderr, "MLton_callFromC() starting\n");	\
+	s = &gcState;							\
+	s->savedThread = s->currentThread;				\
+	/* Return to the C Handler thread. */				\
+	GC_switchToThread (s, s->callFromCHandler);			\
+	jump = *(pointer*)(s->stackTop - WORD_SIZE);			\
+	if (reserveEsp)							\
+		__asm__ __volatile__					\
+		("pusha;movl %%esp,%0;movl %1,%%eax;movl %2,%%ebp;movl %3,%%edi;jmp *%%eax;.global Thread_returnToC;Thread_returnToC:;movl %0,%%esp;popa" \
+		: "=m" (c_stackP)					\
+		: "m" (jump), "m" (gcState.stackTop), "m" (gcState.frontier) \
+		);							\
+	else								\
+		__asm__ __volatile__ 					\
+		("pusha;movl %%esp,%0;movl %1,%%eax;movl %2,%%ebp;movl %3,%%esp;jmp *%%eax;.global Thread_returnToC;Thread_returnToC:;movl %0,%%esp;popa" \
+		: "=m" (c_stackP) 					\
+ 		: "m" (jump), "m" (gcState.stackTop), "m" (gcState.frontier) \
+		); \
+	GC_switchToThread (s, s->savedThread);				\
+	s->savedThread = BOGUS_THREAD;					\
+	if (DEBUG_X86CODEGEN)						\
+		fprintf (stderr, "Thread_returnToC done");		\
+	return;								\
+}									\
 int main (int argc, char **argv) {					\
 	pointer jump;  							\
 	extern pointer ml;						\
@@ -51,13 +85,13 @@
 		("movl %%esp,%0;movl %1,%%eax;movl %2,%%ebp;movl %3,%%edi;jmp *%%eax" \
 		: "=m" (c_stackP)					\
 		: "m" (jump), "m" (gcState.stackTop), "m" (gcState.frontier) \
-		: "%ebp", "%edi");					\
+		);							\
 	else								\
 		__asm__ __volatile__ 					\
 		("movl %%esp,%0;movl %1,%%eax;movl %2,%%ebp;movl %3,%%esp;jmp *%%eax" \
 		: "=m" (c_stackP) 					\
  		: "m" (jump), "m" (gcState.stackTop), "m" (gcState.frontier) \
-		: "%ebp", "%esp");					\
+		);							\
 	return 1;							\
 }
 





-------------------------------------------------------
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