[MLton] Re: MLton digest, Vol 1 #481 - 8 msgs

Jens Axel Søgaard jensaxel@soegaard.net
Sat, 16 Apr 2005 18:46:53 +0200

Stephen Weeks wrote:

 >> Jens Axel Søgaard wrote:

 >> I am a little unsure whether all globals that can't simply be output
 >> as C literals are initialized in the same way as the reals.

 > Currently, all the globals (except for reals) are initialized by
 > running some code.  To turn that into C literals, one would have to
 > interpret that code at compile time, which seems like a pain.  The
 > easiest way to solve (1) is to run the SML program (i.e. PrepFarJump
 > followed by the trampoline).  When one writes a shared library in
 > MLton, the understanding is that all SML code at the toplevel is run
 > when the library is initialized.


 >> to which Weeks added
 >>  > I hope this can be easily achieved using Thread_returnToC, as it is
 >>  > done in SML functions exported via _export.  See the "register"
 >>  > function in basis-library/mlton/thread.sml.
 >> Is this neccessary if one puts the initizalization part of main()
 >> into a separate function?

 > The point here is related to the idea for (1) above.  Right now, the
 > basis library causes code to be added to the end of every program that
 > causes the program to call exit() once all the toplevel code has been
 > executed.  See the call to "setSuffix" in
 > basis-library/mlton/mlton.sml.  It should be simple enough to add a
 > boolean compile-time constant, "isSharedLibrary" or somesuch, and to
 > tweak things so that when isSharedLibrary is true, the suffix returns
 > to the C initialization code instead of calling exit.

Ah - now it makes sense. (Together with the note in the hackers guide
that the code in basis-library is concatened the program).

So presumably, I should change

   val _ =
      (Primitive.TopLevel.setHandler MLtonExn.topLevelHandler
       ; Primitive.TopLevel.setSuffix
         (fn () => MLtonProcess.exit MLtonProcess.Status.success))

to something like

   val _ =  (Primitive.TopLevel.setHandler MLtonExn.topLevelHandler
              ; Primitive.TopLevel.setSuffix
                   (if Control.isSharedLibrary
                    then   (fn () => 0)
                    else   (fn () => MLtonProcess.exit MLtonProcess.Status.success)))


 >> A tangential question: Is it possible for main() to be called
 >> several times (perhaps due to multiple threads)? Otherwise the
 >> if in:
 >> 	if (gcState.isOriginal) {					\
 >> 		real_Init();						\
 >> 		PrepFarJump(mc, ml);					\
 >> 	} else {							\
 >> 		/* Return to the saved world */				\
 >> 		nextFun = *(int*)(gcState.stackTop - WORD_SIZE);	\
 >> 		cont.nextChunk = nextChunks[nextFun];			\
 >> 	}								\
 >> seems unneccesary.

 > It is not possible for main to be called several times.  The "if
 > (gcState.isOriginal)" is to distinguish between when the executable is
 > called with a saved world and when it isn't.

Ok - I couldn't find an explanation in the docs. Now that I found the
explanation in the list of features - I see worlds are what in Scheme
jargon are called heap images.

Jens Axel Søgaard