Import Debian changes 20180207-1
[hcoop/debian/mlton.git] / doc / guide / localhost / PortingMLton
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <meta name="generator" content="AsciiDoc 8.6.9">
6 <title>PortingMLton</title>
7 <link rel="stylesheet" href="./asciidoc.css" type="text/css">
8 <link rel="stylesheet" href="./pygments.css" type="text/css">
9
10
11 <script type="text/javascript" src="./asciidoc.js"></script>
12 <script type="text/javascript">
13 /*<![CDATA[*/
14 asciidoc.install();
15 /*]]>*/
16 </script>
17 <link rel="stylesheet" href="./mlton.css" type="text/css">
18 </head>
19 <body class="article">
20 <div id="banner">
21 <div id="banner-home">
22 <a href="./Home">MLton 20180207</a>
23 </div>
24 </div>
25 <div id="header">
26 <h1>PortingMLton</h1>
27 </div>
28 <div id="content">
29 <div id="preamble">
30 <div class="sectionbody">
31 <div class="paragraph"><p>Porting MLton to a new target platform (architecture or OS) involves
32 the following steps.</p></div>
33 <div class="olist arabic"><ol class="arabic">
34 <li>
35 <p>
36 Make the necessary changes to the scripts, runtime system,
37 <a href="BasisLibrary"> Basis Library</a> implementation, and compiler.
38 </p>
39 </li>
40 <li>
41 <p>
42 Get the regressions working using a cross compiler.
43 </p>
44 </li>
45 <li>
46 <p>
47 <a href="CrossCompiling"> Cross compile</a> MLton and bootstrap on the target.
48 </p>
49 </li>
50 </ol></div>
51 <div class="paragraph"><p>MLton has a native code generator only for AMD64 and X86, so, if you
52 are porting to another architecture, you must use the C code
53 generator. These notes do not cover building a new native code
54 generator.</p></div>
55 <div class="paragraph"><p>Some of the following steps will not be necessary if MLton already
56 supports the architecture or operating system you are porting to.</p></div>
57 </div>
58 </div>
59 <div class="sect1">
60 <h2 id="_what_code_to_change">What code to change</h2>
61 <div class="sectionbody">
62 <div class="ulist"><ul>
63 <li>
64 <p>
65 Scripts.
66 </p>
67 <div class="openblock">
68 <div class="content">
69 <div class="ulist"><ul>
70 <li>
71 <p>
72 In <span class="monospaced">bin/platform</span>, add new cases to define <span class="monospaced">$HOST_OS</span> and <span class="monospaced">$HOST_ARCH</span>.
73 </p>
74 </li>
75 </ul></div>
76 </div></div>
77 </li>
78 <li>
79 <p>
80 Runtime system.
81 </p>
82 <div class="openblock">
83 <div class="content">
84 <div class="paragraph"><p>The goal of this step is to be able to successfully run <span class="monospaced">make</span> in the
85 <span class="monospaced">runtime</span> directory on the target machine.</p></div>
86 <div class="ulist"><ul>
87 <li>
88 <p>
89 In <span class="monospaced">platform.h</span>, add a new case to include <span class="monospaced">platform/&lt;arch&gt;.h</span> and <span class="monospaced">platform/&lt;os&gt;.h</span>.
90 </p>
91 </li>
92 <li>
93 <p>
94 In <span class="monospaced">platform/&lt;arch&gt;.h</span>:
95 </p>
96 <div class="ulist"><ul>
97 <li>
98 <p>
99 define <span class="monospaced">MLton_Platform_Arch_host</span>.
100 </p>
101 </li>
102 </ul></div>
103 </li>
104 <li>
105 <p>
106 In <span class="monospaced">platform/&lt;os&gt;.h</span>:
107 </p>
108 <div class="ulist"><ul>
109 <li>
110 <p>
111 include platform-specific includes.
112 </p>
113 </li>
114 <li>
115 <p>
116 define <span class="monospaced">MLton_Platform_OS_host</span>.
117 </p>
118 </li>
119 <li>
120 <p>
121 define all of the <span class="monospaced">HAS_*</span> macros.
122 </p>
123 </li>
124 </ul></div>
125 </li>
126 <li>
127 <p>
128 In <span class="monospaced">platform/&lt;os&gt;.c</span> implement any platform-dependent functions that the runtime needs.
129 </p>
130 </li>
131 <li>
132 <p>
133 Add rounding mode control to <span class="monospaced">basis/Real/IEEEReal.c</span> for the new arch (if not <span class="monospaced">HAS_FEROUND</span>)
134 </p>
135 </li>
136 <li>
137 <p>
138 Compile and install the <a href="GnuMP">GnuMP</a>. This varies from platform to platform. In <span class="monospaced">platform/&lt;os&gt;.h</span>, you need to include the appropriate <span class="monospaced">gmp.h</span>.
139 </p>
140 </li>
141 </ul></div>
142 </div></div>
143 </li>
144 <li>
145 <p>
146 Basis Library implementation (<span class="monospaced">basis-library/*</span>)
147 </p>
148 <div class="openblock">
149 <div class="content">
150 <div class="ulist"><ul>
151 <li>
152 <p>
153 In <span class="monospaced">primitive/prim-mlton.sml</span>:
154 </p>
155 <div class="ulist"><ul>
156 <li>
157 <p>
158 Add a new variant to the <span class="monospaced">MLton.Platform.Arch.t</span> datatype.
159 </p>
160 </li>
161 <li>
162 <p>
163 modify the constants that define <span class="monospaced">MLton.Platform.Arch.host</span> to match with <span class="monospaced">MLton_Platform_Arch_host</span>, as set in <span class="monospaced">runtime/platform/&lt;arch&gt;.h</span>.
164 </p>
165 </li>
166 <li>
167 <p>
168 Add a new variant to the <span class="monospaced">MLton.Platform.OS.t</span> datatype.
169 </p>
170 </li>
171 <li>
172 <p>
173 modify the constants that define <span class="monospaced">MLton.Platform.OS.host</span> to match with <span class="monospaced">MLton_Platform_OS_host</span>, as set in <span class="monospaced">runtime/platform/&lt;os&gt;.h</span>.
174 </p>
175 </li>
176 </ul></div>
177 </li>
178 <li>
179 <p>
180 In <span class="monospaced">mlton/platform.{sig,sml}</span> add a new variant.
181 </p>
182 </li>
183 <li>
184 <p>
185 In <span class="monospaced">sml-nj/sml-nj.sml</span>, modify <span class="monospaced">getOSKind</span>.
186 </p>
187 </li>
188 <li>
189 <p>
190 Look at all the uses of <span class="monospaced">MLton.Platform</span> 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.
191 </p>
192 <div class="listingblock">
193 <div class="content monospaced">
194 <pre>find basis-library -type f | xargs grep 'MLton\.Platform'</pre>
195 </div></div>
196 <div class="paragraph"><p>If in doubt, leave the code alone and wait to see what happens when you run the regression tests.</p></div>
197 </li>
198 </ul></div>
199 </div></div>
200 </li>
201 <li>
202 <p>
203 Compiler.
204 </p>
205 <div class="openblock">
206 <div class="content">
207 <div class="ulist"><ul>
208 <li>
209 <p>
210 In <span class="monospaced">lib/stubs/mlton-stubs/platform.sig</span> add any new variants, as was done in the Basis Library.
211 </p>
212 </li>
213 <li>
214 <p>
215 In <span class="monospaced">lib/stubs/mlton-stubs/mlton.sml</span> add any new variants in <span class="monospaced">MLton.Platform</span>, as was done in the Basis Library.
216 </p>
217 </li>
218 </ul></div>
219 </div></div>
220 </li>
221 </ul></div>
222 <div class="paragraph"><p>The string used to identify a particular architecture or operating
223 system must be the same (except for possibly case of letters) in the
224 scripts, runtime, Basis Library implementation, and compiler (stubs).
225 In <span class="monospaced">mlton/main/main.fun</span>, MLton itself uses the conversions to and
226 from strings:</p></div>
227 <div class="listingblock">
228 <div class="content monospaced">
229 <pre>MLton.Platform.{Arch,OS}.{from,to}String</pre>
230 </div></div>
231 <div class="paragraph"><p>If the there is a mismatch, you may see the error message
232 <span class="monospaced">strange arch</span> or <span class="monospaced">strange os</span>.</p></div>
233 </div>
234 </div>
235 <div class="sect1">
236 <h2 id="_running_the_regressions_with_a_cross_compiler">Running the regressions with a cross compiler</h2>
237 <div class="sectionbody">
238 <div class="paragraph"><p>When porting to a new platform, it is always best to get all (or as
239 many as possible) of the regressions working before moving to a self
240 compile. It is easiest to do this by modifying and rebuilding the
241 compiler on a working machine and then running the regressions with a
242 cross compiler. It is not easy to build a gcc cross compiler, so we
243 recommend generating the C and assembly on a working machine (using
244 MLton&#8217;s <span class="monospaced">-target</span> and <span class="monospaced">-stop g</span> flags, copying the generated files to
245 the target machine, then compiling and linking there.</p></div>
246 <div class="olist arabic"><ol class="arabic">
247 <li>
248 <p>
249 Remake the compiler on a working machine.
250 </p>
251 </li>
252 <li>
253 <p>
254 Use <span class="monospaced">bin/add-cross</span> to add support for the new target. In particular, this should create <span class="monospaced">build/lib/mlton/targets/&lt;target&gt;/</span> with the platform-specific necessary cross-compilation information.
255 </p>
256 </li>
257 <li>
258 <p>
259 Run the regression tests with the cross-compiler. To cross-compile all the tests, do
260 </p>
261 <div class="listingblock">
262 <div class="content monospaced">
263 <pre>bin/regression -cross &lt;target&gt;</pre>
264 </div></div>
265 <div class="paragraph"><p>This will create all the executables. Then, copy <span class="monospaced">bin/regression</span> and
266 the <span class="monospaced">regression</span> directory to the target machine, and do</p></div>
267 <div class="listingblock">
268 <div class="content monospaced">
269 <pre>bin/regression -run-only &lt;target&gt;</pre>
270 </div></div>
271 <div class="paragraph"><p>This should run all the tests.</p></div>
272 </li>
273 </ol></div>
274 <div class="paragraph"><p>Repeat this step, interleaved with appropriate compiler modifications,
275 until all the regressions pass.</p></div>
276 </div>
277 </div>
278 <div class="sect1">
279 <h2 id="_bootstrap">Bootstrap</h2>
280 <div class="sectionbody">
281 <div class="paragraph"><p>Once you&#8217;ve got all the regressions working, you can build MLton for
282 the new target. As with the regressions, the idea for bootstrapping
283 is to generate the C and assembly on a working machine, copy it to the
284 target machine, and then compile and link there. Here&#8217;s the sequence
285 of steps.</p></div>
286 <div class="olist arabic"><ol class="arabic">
287 <li>
288 <p>
289 On a working machine, with the newly rebuilt compiler, in the <span class="monospaced">mlton</span> directory, do:
290 </p>
291 <div class="listingblock">
292 <div class="content monospaced">
293 <pre>mlton -stop g -target &lt;target&gt; mlton.mlb</pre>
294 </div></div>
295 </li>
296 <li>
297 <p>
298 Copy to the target machine.
299 </p>
300 </li>
301 <li>
302 <p>
303 On the target machine, move the libraries to the right place. That is, in <span class="monospaced">build/lib/mlton/targets</span>, do:
304 </p>
305 <div class="listingblock">
306 <div class="content monospaced">
307 <pre>rm -rf self
308 mv &lt;target&gt; self</pre>
309 </div></div>
310 <div class="paragraph"><p>Also make sure you have all the header files in build/lib/mlton/include. You can copy them from a host machine that has run <span class="monospaced">make runtime</span>.</p></div>
311 </li>
312 <li>
313 <p>
314 On the target machine, compile and link MLton. That is, in the mlton directory, do something like:
315 </p>
316 <div class="listingblock">
317 <div class="content monospaced">
318 <pre>gcc -c -Ibuild/lib/mlton/include -Ibuild/lib/mlton/targets/self/include -O1 -w mlton/mlton.*.[cs]
319 gcc -o build/lib/mlton/mlton-compile \
320 -Lbuild/lib/mlton/targets/self \
321 -L/usr/local/lib \
322 mlton.*.o \
323 -lmlton -lgmp -lgdtoa -lm</pre>
324 </div></div>
325 </li>
326 <li>
327 <p>
328 At this point, MLton should be working and you can finish the rest of a usual make on the target machine.
329 </p>
330 <div class="listingblock">
331 <div class="content monospaced">
332 <pre>make basis-no-check script mlbpathmap constants libraries tools</pre>
333 </div></div>
334 </li>
335 <li>
336 <p>
337 Making the last tool, mlyacc, will fail, because mlyacc cannot bootstrap its own yacc.grm.* files. On the host machine, run <span class="monospaced">make -C mlyacc src/yacc.grm.sml</span>. Then copy both files to the target machine, and compile mlyacc, making sure to supply the path to your newly compile mllex: <span class="monospaced">make -C mlyacc MLLEX=mllex/mllex</span>.
338 </p>
339 </li>
340 </ol></div>
341 <div class="paragraph"><p>There are other details to get right, like making sure that the tools
342 directories were clean so that the tools are rebuilt on the new
343 platform, but hopefully this structure works. Once you&#8217;ve got a
344 compiler on the target machine, you should test it by running all the
345 regressions normally (i.e. without the <span class="monospaced">-cross</span> flag) and by running a
346 couple rounds of self compiles.</p></div>
347 </div>
348 </div>
349 <div class="sect1">
350 <h2 id="_also_see">Also see</h2>
351 <div class="sectionbody">
352 <div class="paragraph"><p>The above description is based on the following emails sent to the
353 MLton list.</p></div>
354 <div class="ulist"><ul>
355 <li>
356 <p>
357 <a href="http://www.mlton.org/pipermail/mlton/2002-October/013110.html">http://www.mlton.org/pipermail/mlton/2002-October/013110.html</a>
358 </p>
359 </li>
360 <li>
361 <p>
362 <a href="http://www.mlton.org/pipermail/mlton/2004-July/016029.html">http://www.mlton.org/pipermail/mlton/2004-July/016029.html</a>
363 </p>
364 </li>
365 </ul></div>
366 </div>
367 </div>
368 </div>
369 <div id="footnotes"><hr></div>
370 <div id="footer">
371 <div id="footer-text">
372 </div>
373 <div id="footer-badges">
374 </div>
375 </div>
376 </body>
377 </html>