SML# Document Version 4.0.0
14 SML# feature: separate compilation

14.8 Top-level execution

Execution of an SML# program is done by evaluating the top-level declarations. If a program consists of a single file then this model is realized by generating the code for each declaration and concatenate them in the order of declarations. However, a separately compiled program consisting of multiple files, we have to decide the order of execution of declarations. SML# decides the order based on the following policy.

  1. 1.

    The top-level declarations corresponding to the top-level .smi file, i.e. the file give as a command line parameter to smlsharp command, are executed after the declarations corresponding to any other .smi files. In what follows, we refer to this top-level as link top-level.

  2. 2.

    If a variable provided by A.sml is used in B.sml, the top-level of A.sml is executed before executing that of B.sml.

  3. 3.

    If A.sml is not reachable from the link top-level through the provide-use relationship, the top-level of A.sml is not executed (the compiler does not link A.o into the executable file).

Execution order of declarations respects the dependency of references of names. Note that the reference relation is the actual relation generated by occurrences of long-ids in a program, and not the relation generated by _require declarations in .smi files.

There are two exceptions. One is the link top-level, whose top-level is always executed. Another is the require declaration of the following form:

_require smiFilePath init

This forces the compiler to link the object file corresponding to smiFilePath and generate code executing its toplevel. This form is only for special purpose; it had better not to abuse it.