| 1 |
= PortingMLton |
| 2 |
|
| 3 |
Porting MLton to a new target platform (architecture or OS) involves |
| 4 |
the following steps. |
| 5 |
|
| 6 |
1. Make the necessary changes to the scripts, runtime system, |
| 7 |
<<BasisLibrary#,Basis Library>> implementation, and compiler. |
| 8 |
|
| 9 |
2. Get the regressions working using a cross compiler. |
| 10 |
|
| 11 |
3. Bootstrap MLton on the target. |
| 12 |
|
| 13 |
MLton has a native code generator only for AMD64 and X86, so, if you |
| 14 |
are porting to another architecture, you must use the C code |
| 15 |
generator. These notes do not cover building a new native code |
| 16 |
generator. |
| 17 |
|
| 18 |
Some of the following steps will not be necessary if MLton already |
| 19 |
supports the architecture or operating system being ported to. |
| 20 |
|
| 21 |
|
| 22 |
== What code to change |
| 23 |
|
| 24 |
* Scripts. |
| 25 |
+ |
| 26 |
-- |
| 27 |
* In `bin/platform`, add new cases to define `$HOST_OS` and `$HOST_ARCH`. |
| 28 |
-- |
| 29 |
|
| 30 |
* Runtime system. |
| 31 |
+ |
| 32 |
-- |
| 33 |
The goal of this step is to be able to successfully run `make` in the |
| 34 |
`runtime` directory on the target machine. |
| 35 |
|
| 36 |
* In `platform.h`, add a new case to include `platform/<arch>.h` and `platform/<os>.h`. |
| 37 |
|
| 38 |
* In `platform/<arch>.h`: |
| 39 |
** define `MLton_Platform_Arch_host`. |
| 40 |
|
| 41 |
* In `platform/<os>.h`: |
| 42 |
** include platform-specific includes. |
| 43 |
** define `MLton_Platform_OS_host`. |
| 44 |
** define all of the `HAS_*` macros. |
| 45 |
|
| 46 |
* In `platform/<os>.c` implement any platform-dependent functions that the runtime needs. |
| 47 |
|
| 48 |
* Add rounding mode control to `basis/Real/IEEEReal.c` for the new arch (if not `HAS_FEROUND`) |
| 49 |
|
| 50 |
* Build and install the <<GMP#>> development library. This varies from platform to platform. |
| 51 |
-- |
| 52 |
|
| 53 |
* Basis Library implementation (`basis-library/*`) |
| 54 |
+ |
| 55 |
-- |
| 56 |
* In `primitive/prim-mlton.sml`: |
| 57 |
** Add a new variant to the `MLton.Platform.Arch.t` datatype. |
| 58 |
** modify the constants that define `MLton.Platform.Arch.host` to match with `MLton_Platform_Arch_host`, as set in `runtime/platform/<arch>.h`. |
| 59 |
** Add a new variant to the `MLton.Platform.OS.t` datatype. |
| 60 |
** modify the constants that define `MLton.Platform.OS.host` to match with `MLton_Platform_OS_host`, as set in `runtime/platform/<os>.h`. |
| 61 |
|
| 62 |
* In `mlton/platform.{sig,sml}` add a new variant. |
| 63 |
|
| 64 |
* In `sml-nj/sml-nj.sml`, modify `getOSKind`. |
| 65 |
|
| 66 |
* Look at all the uses of `MLton.Platform` in the Basis Library implementation and see if you need to do anything special. You might use the following command to see where to look. |
| 67 |
+ |
| 68 |
---- |
| 69 |
find basis-library -type f | xargs grep 'MLton\.Platform' |
| 70 |
---- |
| 71 |
+ |
| 72 |
If in doubt, leave the code alone and wait to see what happens when you run the regression tests. |
| 73 |
-- |
| 74 |
|
| 75 |
* Compiler. |
| 76 |
+ |
| 77 |
-- |
| 78 |
* In `lib/stubs/mlton-stubs/platform.sig` add any new variants, as was done in the Basis Library. |
| 79 |
|
| 80 |
* In `lib/stubs/mlton-stubs/mlton.sml` add any new variants in `MLton.Platform`, as was done in the Basis Library. |
| 81 |
-- |
| 82 |
|
| 83 |
The string used to identify a particular architecture or operating |
| 84 |
system must be the same (except for possibly case of letters) in the |
| 85 |
scripts, runtime, Basis Library implementation, and compiler (stubs). |
| 86 |
In `mlton/main/main.fun`, MLton itself uses the conversions to and |
| 87 |
from strings: |
| 88 |
---- |
| 89 |
MLton.Platform.{Arch,OS}.{from,to}String |
| 90 |
---- |
| 91 |
|
| 92 |
If the there is a mismatch, you may see the error message |
| 93 |
`strange arch` or `strange os`. |
| 94 |
|
| 95 |
|
| 96 |
== Running the regressions with a cross compiler |
| 97 |
|
| 98 |
When porting to a new platform, it is always best to get all (or as |
| 99 |
many as possible) of the regressions working before moving to a self |
| 100 |
compile. It is easiest to do this by modifying and rebuilding the |
| 101 |
compiler on a working machine and then running the regressions with a |
| 102 |
cross compiler. It is not easy to build a gcc cross compiler, so we |
| 103 |
recommend generating the C and assembly on a working machine (using |
| 104 |
MLton's `-target` and `-stop g` flags, copying the generated files to |
| 105 |
the target machine, then compiling and linking there. |
| 106 |
|
| 107 |
1. Remake the compiler on a working machine. |
| 108 |
|
| 109 |
2. Use `bin/add-cross` to add support for the new target. In particular, this should create `build/lib/mlton/targets/<target>/` with the platform-specific necessary cross-compilation information. |
| 110 |
|
| 111 |
3. Run the regression tests with the cross-compiler. To cross-compile all the tests, do |
| 112 |
+ |
| 113 |
---- |
| 114 |
bin/regression -cross <target> |
| 115 |
---- |
| 116 |
+ |
| 117 |
This will create all the executables. Then, copy `bin/regression` and |
| 118 |
the `regression` directory to the target machine, and do |
| 119 |
+ |
| 120 |
---- |
| 121 |
bin/regression -run-only <target> |
| 122 |
---- |
| 123 |
+ |
| 124 |
This should run all the tests. |
| 125 |
|
| 126 |
Repeat this step, interleaved with appropriate compiler modifications, |
| 127 |
until all the regressions pass. |
| 128 |
|
| 129 |
|
| 130 |
== Bootstrap |
| 131 |
|
| 132 |
The idea for bootstrapping MLton on a new platform is as follows: |
| 133 |
|
| 134 |
* send the current sources to a remote machine (using ssh) |
| 135 |
* build the MLton runtime system on the remote machine |
| 136 |
* receive the built runtime system from the remote machine as a new target on |
| 137 |
the host machine |
| 138 |
* build bootstrap compiler sources on the host machine for the new target |
| 139 |
* send the boostrap sources to the remote machine |
| 140 |
* build the boostrap compiler on the remote machine using the boostrap |
| 141 |
compiler sources |
| 142 |
* complete the MLton build on the remote machine with the boostrap compiler to |
| 143 |
obtain a boot package |
| 144 |
* build MLton on the remote machine from clean sources using the boot package |
| 145 |
* receive the built binary release from the remote machine |
| 146 |
|
| 147 |
The `remote-bootstrap` goal of the root `Makefile` automates this process; see |
| 148 |
comments in the root `Makefile`. Here is an example bootstrapping on an OpenBSD |
| 149 |
machine (named `thunder`): |
| 150 |
---- |
| 151 |
$ make REMOTE_MACHINE=thunder REMOTE_MAKE=gmake REMOTE_MAKEFLAGS=WITH_GMP_DIR=/usr/local remote-bootstrap |
| 152 |
---- |
| 153 |
|
| 154 |
Once you've got a compiler on the target machine, you should test it by running |
| 155 |
all the regressions normally and by running a couple rounds of self compiles. |
| 156 |
|
| 157 |
|
| 158 |
== Also see |
| 159 |
|
| 160 |
* http://www.mlton.org/pipermail/mlton/2002-October/013110.html |
| 161 |
* http://www.mlton.org/pipermail/mlton/2004-July/016029.html |
| 162 |
* https://github.com/MLton/mlton/issues/296 |