File: PortingMLton

package info (click to toggle)
mlton 20130715-3
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 60,900 kB
  • ctags: 69,386
  • sloc: xml: 34,418; ansic: 17,399; lisp: 2,879; makefile: 1,605; sh: 1,254; pascal: 256; python: 143; asm: 97
file content (371 lines) | stat: -rw-r--r-- 11,962 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="generator" content="AsciiDoc 8.6.8">
<title>PortingMLton</title>
<link rel="stylesheet" href="./asciidoc.css" type="text/css">
<link rel="stylesheet" href="./pygments.css" type="text/css">


<script type="text/javascript" src="./asciidoc.js"></script>
<script type="text/javascript">
/*<![CDATA[*/
asciidoc.install();
/*]]>*/
</script>
<link rel="stylesheet" href="./mlton.css" type="text/css"/>
</head>
<body class="article">
<div id="banner">
<div id="banner-home">
<a href="./Home">MLton 20130715</a>
</div>
</div>
<div id="header">
<h1>PortingMLton</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p>Porting MLton to a new target platform (architecture or OS) involves
the following steps.</p></div>
<div class="olist arabic"><ol class="arabic">
<li>
<p>
Make the necessary changes to the scripts, runtime system,
<a href="BasisLibrary"> Basis Library</a> implementation, and compiler.
</p>
</li>
<li>
<p>
Get the regressions working using a cross compiler.
</p>
</li>
<li>
<p>
<a href="CrossCompiling"> Cross compile</a> MLton and bootstrap on the target.
</p>
</li>
</ol></div>
<div class="paragraph"><p>MLton has a native code generator only for AMD64 and X86, so, if you
are porting to another architecture, you must use the C code
generator.  These notes do not cover building a new native code
generator.</p></div>
<div class="paragraph"><p>Some of the following steps will not be necessary if MLton already
supports the architecture or operating system you are porting to.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_what_code_to_change">What code to change</h2>
<div class="sectionbody">
<div class="ulist"><ul>
<li>
<p>
Scripts.
</p>
<div class="openblock">
<div class="content">
<div class="ulist"><ul>
<li>
<p>
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>.
</p>
</li>
</ul></div>
</div></div>
</li>
<li>
<p>
Runtime system.
</p>
<div class="openblock">
<div class="content">
<div class="paragraph"><p>The goal of this step is to be able to successfully run <span class="monospaced">make</span> in the
<span class="monospaced">runtime</span> directory on the target machine.</p></div>
<div class="ulist"><ul>
<li>
<p>
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>.
</p>
</li>
<li>
<p>
In <span class="monospaced">platform/&lt;arch&gt;.h</span>:
</p>
<div class="ulist"><ul>
<li>
<p>
define <span class="monospaced">MLton_Platform_Arch_host</span>.
</p>
</li>
</ul></div>
</li>
<li>
<p>
In <span class="monospaced">platform/&lt;os&gt;.h</span>:
</p>
<div class="ulist"><ul>
<li>
<p>
include platform-specific includes.
</p>
</li>
<li>
<p>
define <span class="monospaced">MLton_Platform_OS_host</span>.
</p>
</li>
<li>
<p>
define all of the <span class="monospaced">HAS_*</span> macros.
</p>
</li>
</ul></div>
</li>
<li>
<p>
In <span class="monospaced">platform/&lt;os&gt;.c</span> implement any platform-dependent functions that the runtime needs.
</p>
</li>
<li>
<p>
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>)
</p>
</li>
<li>
<p>
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>.
</p>
</li>
</ul></div>
</div></div>
</li>
<li>
<p>
Basis Library implementation (<span class="monospaced">basis-library/*</span>)
</p>
<div class="openblock">
<div class="content">
<div class="ulist"><ul>
<li>
<p>
In <span class="monospaced">primitive/prim-mlton.sml</span>:
</p>
<div class="ulist"><ul>
<li>
<p>
Add a new variant to the <span class="monospaced">MLton.Platform.Arch.t</span> datatype.
</p>
</li>
<li>
<p>
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>.
</p>
</li>
<li>
<p>
Add a new variant to the <span class="monospaced">MLton.Platform.OS.t</span> datatype.
</p>
</li>
<li>
<p>
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>.
</p>
</li>
</ul></div>
</li>
<li>
<p>
In <span class="monospaced">mlton/platform.{sig,sml}</span> add a new variant.
</p>
</li>
<li>
<p>
In <span class="monospaced">sml-nj/sml-nj.sml</span>, modify <span class="monospaced">getOSKind</span>.
</p>
</li>
<li>
<p>
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.
</p>
<div class="listingblock">
<div class="content monospaced">
<pre>find basis-library -type f | xargs grep 'MLton\.Platform'</pre>
</div></div>
<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>
</li>
</ul></div>
</div></div>
</li>
<li>
<p>
Compiler.
</p>
<div class="openblock">
<div class="content">
<div class="ulist"><ul>
<li>
<p>
In <span class="monospaced">lib/stubs/mlton-stubs/platform.sig</span> add any new variants, as was done in the Basis Library.
</p>
</li>
<li>
<p>
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.
</p>
</li>
</ul></div>
</div></div>
</li>
</ul></div>
<div class="paragraph"><p>The string used to identify a particular architecture or operating
system must be the same (except for possibly case of letters) in the
scripts, runtime, Basis Library implementation, and compiler (stubs).
In <span class="monospaced">mlton/main/main.fun</span>, MLton itself uses the conversions to and
from strings:</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>MLton.Platform.{Arch,OS}.{from,to}String</pre>
</div></div>
<div class="paragraph"><p>If the there is a mismatch, you may see the error message
<span class="monospaced">strange arch</span> or <span class="monospaced">strange os</span>.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_running_the_regressions_with_a_cross_compiler">Running the regressions with a cross compiler</h2>
<div class="sectionbody">
<div class="paragraph"><p>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 running the regressions with a
cross compiler.  It is not easy to build a gcc cross compiler, so we
recommend generating the C and assembly on a working machine (using
MLton&#8217;s <span class="monospaced">-target</span> and <span class="monospaced">-stop g</span> flags, copying the generated files to
the target machine, then compiling and linking there.</p></div>
<div class="olist arabic"><ol class="arabic">
<li>
<p>
Remake the compiler on a working machine.
</p>
</li>
<li>
<p>
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/targets/&lt;target&gt;/</span> with the platform-specific necessary cross-compilation information.
</p>
</li>
<li>
<p>
Run the regression tests with the cross-compiler.  To cross-compile all the tests, do
</p>
<div class="listingblock">
<div class="content monospaced">
<pre>bin/regression -cross &lt;target&gt;</pre>
</div></div>
<div class="paragraph"><p>This will create all the executables.  Then, copy <span class="monospaced">bin/regression</span> and
the <span class="monospaced">regression</span> directory to the target machine, and do</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>bin/regression -run-only &lt;target&gt;</pre>
</div></div>
<div class="paragraph"><p>This should run all the tests.</p></div>
</li>
</ol></div>
<div class="paragraph"><p>Repeat this step, interleaved with appropriate compiler modifications,
until all the regressions pass.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_bootstrap">Bootstrap</h2>
<div class="sectionbody">
<div class="paragraph"><p>Once you&#8217;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 and assembly on a working machine, copy it to the
target machine, and then compile and link there.  Here&#8217;s the sequence
of steps.</p></div>
<div class="olist arabic"><ol class="arabic">
<li>
<p>
On a working machine, with the newly rebuilt compiler, in the <span class="monospaced">mlton</span> directory, do:
</p>
<div class="listingblock">
<div class="content monospaced">
<pre>mlton -stop g -target &lt;target&gt; mlton.mlb</pre>
</div></div>
</li>
<li>
<p>
Copy to the target machine.
</p>
</li>
<li>
<p>
On the target machine, move the libraries to the right place. That is, in <span class="monospaced">build/lib/targets</span>, do:
</p>
<div class="listingblock">
<div class="content monospaced">
<pre>rm -rf self
mv &lt;target&gt; self</pre>
</div></div>
</li>
<li>
<p>
On the target machine, compile and link MLton.  That is, in the  mlton directory, do something like:
</p>
<div class="listingblock">
<div class="content monospaced">
<pre>gcc -c -Ibuild/lib/include -Ibuild/lib/targets/self/include -O1 -w mlton/mlton.*.[cs]
gcc -o build/lib/mlton-compile \
        -Lbuild/lib/targets/self \
        -L/usr/local/lib \
        mlton.*.o \
        -lmlton -lgmp -lgdtoa -lm</pre>
</div></div>
</li>
<li>
<p>
At this point, MLton should be working and you can finish the rest of a usual make on the target machine.
</p>
<div class="listingblock">
<div class="content monospaced">
<pre>make basis-no-check script mlbpathmap targetmap constants libraries tools</pre>
</div></div>
</li>
</ol></div>
<div class="paragraph"><p>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&#8217;ve got a
compiler on the target machine, you should test it by running all the
regressions normally (i.e. without the <span class="monospaced">-cross</span> flag) and by running a
couple rounds of self compiles.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_also_see">Also see</h2>
<div class="sectionbody">
<div class="paragraph"><p>The above description is based on the following emails sent to the
MLton list.</p></div>
<div class="ulist"><ul>
<li>
<p>
<a href="http://www.mlton.org/pipermail/mlton/2002-October/013110.html">http://www.mlton.org/pipermail/mlton/2002-October/013110.html</a>
</p>
</li>
<li>
<p>
<a href="http://www.mlton.org/pipermail/mlton/2004-July/016029.html">http://www.mlton.org/pipermail/mlton/2004-July/016029.html</a>
</p>
</li>
</ul></div>
</div>
</div>
</div>
<div id="footnotes"><hr></div>
<div id="footer">
<div id="footer-text">
</div>
<div id="footer-badges">
</div>
</div>
</body>
</html>