SML# - NativeCodeGeneration Diff

  • Added parts are displayed like this.
  • Deleted parts are displayed like this.

SML# 0.40 has a native code generator. SML# 0.40 supports Linux and
Mac OS X on x86.

To enable native code generation, specify --native option and source
code files to SML# compiler command.

$ smlsharp --native foo.sml

SML# compiler compiles the source files to x86 code and generates an
executable file named "a.sme" by default.

To change the name of the executable, specify -o option with its name.

$ smlsharp --native -o foo foo.sml

To generate a linkable object file instead of an executable file,
specify -c option.

$ smlsharp --native -c -o foo.o foo.sml

The generated object file can be linked with C libraries and/or native
object files by standard linker (ld command or gcc command.) To link
SML# object file to an executable file by linker, it is needed to link
with the SML# runtime library (libsmlsharp, libsmlsharp_entry) and its
dependent libraries. For example:

$ gcc -o foo foo.o -lsmlsharp_entry -lsmlsharp -ldl -lm -lgmp

libsmlsharp_entry only holds a main function which just invokes the toplevel
of the SML# program. This main function is almost equivalent to the following:

int main(int argc, char **argv)
{
     void sml_init(int argc, char **argv);
     void *smlsharp_main(void);
     void sml_finish(void);
     void *exn;
     sml_init(argc, argv);
     exn = smlsharp_main();
     sml_finish();
     return (exn == NULL);
}

You may write your own main function and link it with SML# object files
instead of linking with libsmlsharp_entry.

By specifying --xswitchTrace=yes --xtraceFileLoad=yes option, you will
see what is going on. For example:

$ smlsharp --xswitchTrace=yes --xtraceFileLoad=yes --native -o foo foo.sml
set control option: switchTrace = yes
set control option: traceFileLoad = yes
libpath: /usr/local/lib/smlsharp
libpath: .
source: /tmp/foo.sml
gcc  -c /var/tmp/tmp.0.FwctHt.s -o /var/tmp/tmp.0.FwctHt.o
rm -f /var/tmp/tmp.0.FwctHt.s
rm -f /var/tmp/tmp.0.FwctHt
gcc -L/sw/lib -L/usr/local/lib/smlsharp /usr/local/lib/smlsharp/ntprelu
de.o /var/tmp/tmp.0.FwctHt.o -lsmlsharp_entry -lsmlsharp -ldl -lgmp -lm
  -o foo
rm -f /var/tmp/tmp.0.FwctHt.o