Backport from sid to buster
[hcoop/debian/mlton.git] / doc / guide / src / ImplementSuffix.adoc
1 ImplementSuffix
2 ===============
3
4 <:ImplementSuffix:> is a pass for the <:SXML:>
5 <:IntermediateLanguage:>, invoked from <:SXMLSimplify:>.
6
7 == Description ==
8
9 This pass implements the `TopLevel_setSuffix` primitive, which
10 installs a function to exit the program.
11
12 == Implementation ==
13
14 * <!ViewGitFile(mlton,master,mlton/xml/implement-suffix.fun)>
15
16 == Details and Notes ==
17
18 <:ImplementSuffix:> works by introducing a new `ref` cell to contain
19 the function of type `unit -> unit` that should be called on program
20 exit.
21
22 * The following code (appropriately alpha-converted) is appended to the beginning of the <:SXML:> program:
23 +
24 [source,sml]
25 ----
26 val z_0 =
27 fn a_0 =>
28 let
29 val x_0 =
30 "toplevel suffix not installed"
31 val x_1 =
32 MLton_bug (x_0)
33 in
34 x_1
35 end
36 val topLevelSuffixCell =
37 Ref_ref (z_0)
38 ----
39
40 * Any occurrence of
41 +
42 [source,sml]
43 ----
44 val x_0 =
45 TopLevel_setSuffix (f_0)
46 ----
47 +
48 is rewritten to
49 +
50 [source,sml]
51 ----
52 val x_0 =
53 Ref_assign (topLevelSuffixCell, f_0)
54 ----
55
56 * The following code (appropriately alpha-converted) is appended to the end of the <:SXML:> program:
57 +
58 [source,sml]
59 ----
60 val f_0 =
61 Ref_deref (topLevelSuffixCell)
62 val z_0 =
63 ()
64 val x_0 =
65 f_0 z_0
66 ----