<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="generator" content="AsciiDoc 8.6.10">
<title>ImplementSuffix</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 20201002</a>
</div>
</div>
<div id="header">
<h1>ImplementSuffix</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p><a href="ImplementSuffix">ImplementSuffix</a> is a pass for the <a href="SXML">SXML</a>
<a href="IntermediateLanguage">IntermediateLanguage</a>, invoked from <a href="SXMLSimplify">SXMLSimplify</a>.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_description">Description</h2>
<div class="sectionbody">
<div class="paragraph"><p>This pass implements the <span class="monospaced">TopLevel_setSuffix</span> primitive, which
installs a function to exit the program.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_implementation">Implementation</h2>
<div class="sectionbody">
<div class="ulist"><ul>
<li>
<p>
<a href="https://github.com/MLton/mlton/blob/master/mlton/xml/implement-suffix.fun"><span class="monospaced">implement-suffix.fun</span></a>
</p>
</li>
</ul></div>
</div>
</div>
<div class="sect1">
<h2 id="_details_and_notes">Details and Notes</h2>
<div class="sectionbody">
<div class="paragraph"><p><a href="ImplementSuffix">ImplementSuffix</a> works by introducing a new <span class="monospaced">ref</span> cell to contain
the function of type <span class="monospaced">unit -&gt; unit</span> that should be called on program
exit.</p></div>
<div class="ulist"><ul>
<li>
<p>
The following code (appropriately alpha-converted) is appended to the beginning of the <a href="SXML">SXML</a> program:
</p>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="k">val</span><span class="w"> </span><span class="n">z_0</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">  </span><span class="k">fn</span><span class="w"> </span><span class="n">a_0</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"></span>
<span class="w">  </span><span class="k">let</span><span class="w"></span>
<span class="w">    </span><span class="k">val</span><span class="w"> </span><span class="n">x_0</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">      </span><span class="s">&quot;toplevel suffix not installed&quot;</span><span class="w"></span>
<span class="w">    </span><span class="k">val</span><span class="w"> </span><span class="n">x_1</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">      </span><span class="n">MLton_bug</span><span class="w"> </span><span class="p">(</span><span class="n">x_0</span><span class="p">)</span><span class="w"></span>
<span class="w">  </span><span class="k">in</span><span class="w"></span>
<span class="w">    </span><span class="n">x_1</span><span class="w"></span>
<span class="w">  </span><span class="k">end</span><span class="w"></span>
<span class="k">val</span><span class="w"> </span><span class="n">topLevelSuffixCell</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">  </span><span class="n">Ref_ref</span><span class="w"> </span><span class="p">(</span><span class="n">z_0</span><span class="p">)</span><span class="w"></span>
</pre></div></div></div>
</li>
<li>
<p>
Any occurrence of
</p>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="k">val</span><span class="w"> </span><span class="n">x_0</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">  </span><span class="n">TopLevel_setSuffix</span><span class="w"> </span><span class="p">(</span><span class="n">f_0</span><span class="p">)</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>is rewritten to</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="k">val</span><span class="w"> </span><span class="n">x_0</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">  </span><span class="n">Ref_assign</span><span class="w"> </span><span class="p">(</span><span class="n">topLevelSuffixCell</span><span class="p">,</span><span class="w"> </span><span class="n">f_0</span><span class="p">)</span><span class="w"></span>
</pre></div></div></div>
</li>
<li>
<p>
The following code (appropriately alpha-converted) is appended to the end of the <a href="SXML">SXML</a> program:
</p>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="k">val</span><span class="w"> </span><span class="n">f_0</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">  </span><span class="n">Ref_deref</span><span class="w"> </span><span class="p">(</span><span class="n">topLevelSuffixCell</span><span class="p">)</span><span class="w"></span>
<span class="k">val</span><span class="w"> </span><span class="n">z_0</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">  </span><span class="p">()</span><span class="w"></span>
<span class="k">val</span><span class="w"> </span><span class="n">x_0</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">  </span><span class="n">f_0</span><span class="w"> </span><span class="n">z_0</span><span class="w"></span>
</pre></div></div></div>
</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>
