[MLton] Question on Investigating Segfaults in Compiler

Stephen Weeks MLton@mlton.org
Thu, 8 Jul 2004 10:48:38 -0700


> I decided to take a stab at generating a MLton
> executable  free of the Cygwin compatibility layer. 
...
> 6.  The make process then segfaults while trying to
> build the basis library.
...
> Any recommendations on how to backtrack from here?

When porting to a new platform, it is always best to get all (or as
many as possible) of the regressions working before moving to a self
compile.  It is easiest to do this by modifying and rebuilding the
compiler on a working machine and then "cross" running the
regressions.  I sent a mail about this a couple of years ago

	http://www.mlton.org/pipermail/mlton/2002-October/013110.html

Here's an update on the steps for the new, easier, way of doing things
that doesn't require a gcc cross compiler.   It looks like you got a
lot of these.  Let us know how it goes with the cross regressions once
you get there.


1. Port scripts (bin/).
   * In platform, add a new case to handle the output of uname.
   * In upgrade-basis,
     - add new stubs in MLton.Platform.OS.
     - add a new case to set $os.

2. Port the runtime system (runtime/ include/).
   The result of this is that you should be able to successfully run
   make in the runtime directory on the target machine.  Hopefully,
   this job will be made easier by all of the #ifdefs that have a
   default clause with an #error macro.   
   * Fill in all the #ifdefs either by adding an "|| defined
     (__MINGW__)" to an existing clause or by adding an new "#elif
     (defined (__MINGW__))" clause.  Please leave the #else #error in
     place for future ports.   
   * Add a new MLton_Platform_OS_host constant in basis-constants.h.
     These constants are alphabetized, so you may need to change other
     ones.  You will adjust the corresponding constants when porting
     the basis library. 
   * Compiling and installing the GnuMP varies from platform to
     platform.  You may need to modify the header files that get
     included into IntInf.h.
   * Add to the #ifdef in include/x86-main.h.

3. Port the basis library (basis-library/).
   * In misc/primitive.sml,
     - add a new variant (MinGW) to the MLton.Platform.OS.t datatype.
     - modify the constants that define host to match with
       MLton_Platform_OS_host.
   * In mlton/platform.{sig,sml} add a new variant (MinGW).
   * Look at all the uses of MLton.Platform in the basis library code and see
     if you need to do anything special.  If in doubt, leave it alone
     and wait to see what happens when you run the regression tests.

4. Port the compiler.  (Assuming we're staying on X86 here)
   - In lib/mlton-stubs/{mlton.sml,platform.sig}, add a new variant.
   - In mlton/main/main.fun, add code to set linkWithGmp.

Now, we want to run the regressions by generating the C and assembly
on a working machine, and compiling, linking, and running on the
target machine.

1. Remake the compiler on a working machine.
2. Use bin/add-cross to add support to the compiler for the new
   target.
3. Run the regression tests with the cross-compiler.  To cross-compile
   all the tests, do

	bin/regression -cross i386-mingw

  This will create all the executables.  Then, copy bin/regression and
  the regression directory to the target machine, and do

	bin/regression -run-only

  This should run all the tests. 

Once you've got all the regressions working, you can build MLton for
the new target.  As with the regressions, the idea for bootstrapping
is to generate the C/asm on a working machine, copy it to the target
machine, and then compile and link there.  Here's a rough sequence of
steps.

1. On a working machine, with the newly rebuilt compiler, in the mlton
   directory, do:

	mlton -stop g -target i386-mingw mlton.cm

2. Copy to the target machine.

3. On the target machine, move the libraries to the right place.
   That is, in build/lib, do:

	rm -rf self target-map
	mv i386-mingw self

4. On the target machine, compile and link mlton.  That is, in the
   mlton directory, do something like:

	gcc -c -Ibuild/lib/include \
		-O1 -w \
		mlton/mlton.*.[cS]
	gcc -o build/lib/mlton-compile \
		-Lbuild/lib/self \
		-L/usr/local/lib \
		mlton.*.o \
		-lmlton -lgmp -lgdtoa -lm

5. At this point, mlton should be working and you can finish the rest
   of a usual make on the target machine.

	make script world targetmap tools install

There are other details to get right, like making sure that the tools
directories were clean so that the tools are rebuilt on the new
platform, but hopefully this structure works.  Once you've got a
compiler on the target machine, you should test it by running all the
regressions normally (i.e. without the -cross flag) and by running a
couple rounds of self compiles.