[www-releases] r257950 - Add 3.7.1 docs

Tom Stellard via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 15 15:13:20 PST 2016


Added: www-releases/trunk/3.7.1/docs/tutorial/OCamlLangImpl5.html
URL: http://llvm.org/viewvc/llvm-project/www-releases/trunk/3.7.1/docs/tutorial/OCamlLangImpl5.html?rev=257950&view=auto
==============================================================================
--- www-releases/trunk/3.7.1/docs/tutorial/OCamlLangImpl5.html (added)
+++ www-releases/trunk/3.7.1/docs/tutorial/OCamlLangImpl5.html Fri Jan 15 17:13:16 2016
@@ -0,0 +1,1392 @@
+
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+
+
+<html xmlns="http://www.w3.org/1999/xhtml">
+  <head>
+    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+    
+    <title>5. Kaleidoscope: Extending the Language: Control Flow — LLVM 3.7 documentation</title>
+    
+    <link rel="stylesheet" href="../_static/llvm-theme.css" type="text/css" />
+    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
+    
+    <script type="text/javascript">
+      var DOCUMENTATION_OPTIONS = {
+        URL_ROOT:    '../',
+        VERSION:     '3.7',
+        COLLAPSE_INDEX: false,
+        FILE_SUFFIX: '.html',
+        HAS_SOURCE:  true
+      };
+    </script>
+    <script type="text/javascript" src="../_static/jquery.js"></script>
+    <script type="text/javascript" src="../_static/underscore.js"></script>
+    <script type="text/javascript" src="../_static/doctools.js"></script>
+    <link rel="top" title="LLVM 3.7 documentation" href="../index.html" />
+    <link rel="up" title="LLVM Tutorial: Table of Contents" href="index.html" />
+    <link rel="next" title="6. Kaleidoscope: Extending the Language: User-defined Operators" href="OCamlLangImpl6.html" />
+    <link rel="prev" title="4. Kaleidoscope: Adding JIT and Optimizer Support" href="OCamlLangImpl4.html" />
+<style type="text/css">
+  table.right { float: right; margin-left: 20px; }
+  table.right td { border: 1px solid #ccc; }
+</style>
+
+  </head>
+  <body>
+<div class="logo">
+  <a href="../index.html">
+    <img src="../_static/logo.png"
+         alt="LLVM Logo" width="250" height="88"/></a>
+</div>
+
+    <div class="related">
+      <h3>Navigation</h3>
+      <ul>
+        <li class="right" style="margin-right: 10px">
+          <a href="../genindex.html" title="General Index"
+             accesskey="I">index</a></li>
+        <li class="right" >
+          <a href="OCamlLangImpl6.html" title="6. Kaleidoscope: Extending the Language: User-defined Operators"
+             accesskey="N">next</a> |</li>
+        <li class="right" >
+          <a href="OCamlLangImpl4.html" title="4. Kaleidoscope: Adding JIT and Optimizer Support"
+             accesskey="P">previous</a> |</li>
+  <li><a href="http://llvm.org/">LLVM Home</a> | </li>
+  <li><a href="../index.html">Documentation</a>»</li>
+
+          <li><a href="index.html" accesskey="U">LLVM Tutorial: Table of Contents</a> »</li> 
+      </ul>
+    </div>
+
+
+    <div class="document">
+      <div class="documentwrapper">
+          <div class="body">
+            
+  <div class="section" id="kaleidoscope-extending-the-language-control-flow">
+<h1>5. Kaleidoscope: Extending the Language: Control Flow<a class="headerlink" href="#kaleidoscope-extending-the-language-control-flow" title="Permalink to this headline">¶</a></h1>
+<div class="contents local topic" id="contents">
+<ul class="simple">
+<li><a class="reference internal" href="#chapter-5-introduction" id="id1">Chapter 5 Introduction</a></li>
+<li><a class="reference internal" href="#if-then-else" id="id2">If/Then/Else</a><ul>
+<li><a class="reference internal" href="#lexer-extensions-for-if-then-else" id="id3">Lexer Extensions for If/Then/Else</a></li>
+<li><a class="reference internal" href="#ast-extensions-for-if-then-else" id="id4">AST Extensions for If/Then/Else</a></li>
+<li><a class="reference internal" href="#parser-extensions-for-if-then-else" id="id5">Parser Extensions for If/Then/Else</a></li>
+<li><a class="reference internal" href="#llvm-ir-for-if-then-else" id="id6">LLVM IR for If/Then/Else</a></li>
+<li><a class="reference internal" href="#code-generation-for-if-then-else" id="id7">Code Generation for If/Then/Else</a></li>
+</ul>
+</li>
+<li><a class="reference internal" href="#for-loop-expression" id="id8">‘for’ Loop Expression</a><ul>
+<li><a class="reference internal" href="#lexer-extensions-for-the-for-loop" id="id9">Lexer Extensions for the ‘for’ Loop</a></li>
+<li><a class="reference internal" href="#ast-extensions-for-the-for-loop" id="id10">AST Extensions for the ‘for’ Loop</a></li>
+<li><a class="reference internal" href="#parser-extensions-for-the-for-loop" id="id11">Parser Extensions for the ‘for’ Loop</a></li>
+<li><a class="reference internal" href="#llvm-ir-for-the-for-loop" id="id12">LLVM IR for the ‘for’ Loop</a></li>
+<li><a class="reference internal" href="#code-generation-for-the-for-loop" id="id13">Code Generation for the ‘for’ Loop</a></li>
+</ul>
+</li>
+<li><a class="reference internal" href="#full-code-listing" id="id14">Full Code Listing</a></li>
+</ul>
+</div>
+<div class="section" id="chapter-5-introduction">
+<h2><a class="toc-backref" href="#id1">5.1. Chapter 5 Introduction</a><a class="headerlink" href="#chapter-5-introduction" title="Permalink to this headline">¶</a></h2>
+<p>Welcome to Chapter 5 of the “<a class="reference external" href="index.html">Implementing a language with
+LLVM</a>” tutorial. Parts 1-4 described the implementation of
+the simple Kaleidoscope language and included support for generating
+LLVM IR, followed by optimizations and a JIT compiler. Unfortunately, as
+presented, Kaleidoscope is mostly useless: it has no control flow other
+than call and return. This means that you can’t have conditional
+branches in the code, significantly limiting its power. In this episode
+of “build that compiler”, we’ll extend Kaleidoscope to have an
+if/then/else expression plus a simple ‘for’ loop.</p>
+</div>
+<div class="section" id="if-then-else">
+<h2><a class="toc-backref" href="#id2">5.2. If/Then/Else</a><a class="headerlink" href="#if-then-else" title="Permalink to this headline">¶</a></h2>
+<p>Extending Kaleidoscope to support if/then/else is quite straightforward.
+It basically requires adding lexer support for this “new” concept to the
+lexer, parser, AST, and LLVM code emitter. This example is nice, because
+it shows how easy it is to “grow” a language over time, incrementally
+extending it as new ideas are discovered.</p>
+<p>Before we get going on “how” we add this extension, lets talk about
+“what” we want. The basic idea is that we want to be able to write this
+sort of thing:</p>
+<div class="highlight-python"><div class="highlight"><pre>def fib(x)
+  if x < 3 then
+    1
+  else
+    fib(x-1)+fib(x-2);
+</pre></div>
+</div>
+<p>In Kaleidoscope, every construct is an expression: there are no
+statements. As such, the if/then/else expression needs to return a value
+like any other. Since we’re using a mostly functional form, we’ll have
+it evaluate its conditional, then return the ‘then’ or ‘else’ value
+based on how the condition was resolved. This is very similar to the C
+”?:” expression.</p>
+<p>The semantics of the if/then/else expression is that it evaluates the
+condition to a boolean equality value: 0.0 is considered to be false and
+everything else is considered to be true. If the condition is true, the
+first subexpression is evaluated and returned, if the condition is
+false, the second subexpression is evaluated and returned. Since
+Kaleidoscope allows side-effects, this behavior is important to nail
+down.</p>
+<p>Now that we know what we “want”, lets break this down into its
+constituent pieces.</p>
+<div class="section" id="lexer-extensions-for-if-then-else">
+<h3><a class="toc-backref" href="#id3">5.2.1. Lexer Extensions for If/Then/Else</a><a class="headerlink" href="#lexer-extensions-for-if-then-else" title="Permalink to this headline">¶</a></h3>
+<p>The lexer extensions are straightforward. First we add new variants for
+the relevant tokens:</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="c">(* control *)</span>
+<span class="o">|</span> <span class="nc">If</span> <span class="o">|</span> <span class="nc">Then</span> <span class="o">|</span> <span class="nc">Else</span> <span class="o">|</span> <span class="nc">For</span> <span class="o">|</span> <span class="nc">In</span>
+</pre></div>
+</div>
+<p>Once we have that, we recognize the new keywords in the lexer. This is
+pretty simple stuff:</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="o">...</span>
+<span class="k">match</span> <span class="nn">Buffer</span><span class="p">.</span><span class="n">contents</span> <span class="n">buffer</span> <span class="k">with</span>
+<span class="o">|</span> <span class="s2">"def"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Def</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+<span class="o">|</span> <span class="s2">"extern"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Extern</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+<span class="o">|</span> <span class="s2">"if"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">If</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+<span class="o">|</span> <span class="s2">"then"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Then</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+<span class="o">|</span> <span class="s2">"else"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Else</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+<span class="o">|</span> <span class="s2">"for"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">For</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+<span class="o">|</span> <span class="s2">"in"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">In</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+<span class="o">|</span> <span class="n">id</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Ident</span> <span class="n">id</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+</pre></div>
+</div>
+</div>
+<div class="section" id="ast-extensions-for-if-then-else">
+<h3><a class="toc-backref" href="#id4">5.2.2. AST Extensions for If/Then/Else</a><a class="headerlink" href="#ast-extensions-for-if-then-else" title="Permalink to this headline">¶</a></h3>
+<p>To represent the new expression we add a new AST variant for it:</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="k">type</span> <span class="n">expr</span> <span class="o">=</span>
+  <span class="o">...</span>
+  <span class="c">(* variant for if/then/else. *)</span>
+  <span class="o">|</span> <span class="nc">If</span> <span class="k">of</span> <span class="n">expr</span> <span class="o">*</span> <span class="n">expr</span> <span class="o">*</span> <span class="n">expr</span>
+</pre></div>
+</div>
+<p>The AST variant just has pointers to the various subexpressions.</p>
+</div>
+<div class="section" id="parser-extensions-for-if-then-else">
+<h3><a class="toc-backref" href="#id5">5.2.3. Parser Extensions for If/Then/Else</a><a class="headerlink" href="#parser-extensions-for-if-then-else" title="Permalink to this headline">¶</a></h3>
+<p>Now that we have the relevant tokens coming from the lexer and we have
+the AST node to build, our parsing logic is relatively straightforward.
+First we define a new parsing function:</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="k">let</span> <span class="k">rec</span> <span class="n">parse_primary</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="o">...</span>
+  <span class="c">(* ifexpr ::= 'if' expr 'then' expr 'else' expr *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">If</span><span class="o">;</span> <span class="n">c</span><span class="o">=</span><span class="n">parse_expr</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Then</span> <span class="o">??</span> <span class="s2">"expected 'then'"</span><span class="o">;</span> <span class="n">t</span><span class="o">=</span><span class="n">parse_expr</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Else</span> <span class="o">??</span> <span class="s2">"expected 'else'"</span><span class="o">;</span> <span class="n">e</span><span class="o">=</span><span class="n">parse_expr</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="nn">Ast</span><span class="p">.</span><span class="nc">If</span> <span class="o">(</span><span class="n">c</span><span class="o">,</span> <span class="n">t</span><span class="o">,</span> <span class="n">e</span><span class="o">)</span>
+</pre></div>
+</div>
+<p>Next we hook it up as a primary expression:</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="k">let</span> <span class="k">rec</span> <span class="n">parse_primary</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="o">...</span>
+  <span class="c">(* ifexpr ::= 'if' expr 'then' expr 'else' expr *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">If</span><span class="o">;</span> <span class="n">c</span><span class="o">=</span><span class="n">parse_expr</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Then</span> <span class="o">??</span> <span class="s2">"expected 'then'"</span><span class="o">;</span> <span class="n">t</span><span class="o">=</span><span class="n">parse_expr</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Else</span> <span class="o">??</span> <span class="s2">"expected 'else'"</span><span class="o">;</span> <span class="n">e</span><span class="o">=</span><span class="n">parse_expr</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="nn">Ast</span><span class="p">.</span><span class="nc">If</span> <span class="o">(</span><span class="n">c</span><span class="o">,</span> <span class="n">t</span><span class="o">,</span> <span class="n">e</span><span class="o">)</span>
+</pre></div>
+</div>
+</div>
+<div class="section" id="llvm-ir-for-if-then-else">
+<h3><a class="toc-backref" href="#id6">5.2.4. LLVM IR for If/Then/Else</a><a class="headerlink" href="#llvm-ir-for-if-then-else" title="Permalink to this headline">¶</a></h3>
+<p>Now that we have it parsing and building the AST, the final piece is
+adding LLVM code generation support. This is the most interesting part
+of the if/then/else example, because this is where it starts to
+introduce new concepts. All of the code above has been thoroughly
+described in previous chapters.</p>
+<p>To motivate the code we want to produce, lets take a look at a simple
+example. Consider:</p>
+<div class="highlight-python"><div class="highlight"><pre>extern foo();
+extern bar();
+def baz(x) if x then foo() else bar();
+</pre></div>
+</div>
+<p>If you disable optimizations, the code you’ll (soon) get from
+Kaleidoscope looks like this:</p>
+<div class="highlight-llvm"><div class="highlight"><pre><span class="k">declare</span> <span class="kt">double</span> <span class="vg">@foo</span><span class="p">()</span>
+
+<span class="k">declare</span> <span class="kt">double</span> <span class="vg">@bar</span><span class="p">()</span>
+
+<span class="k">define</span> <span class="kt">double</span> <span class="vg">@baz</span><span class="p">(</span><span class="kt">double</span> <span class="nv">%x</span><span class="p">)</span> <span class="p">{</span>
+<span class="nl">entry:</span>
+  <span class="nv">%ifcond</span> <span class="p">=</span> <span class="k">fcmp</span> <span class="k">one</span> <span class="kt">double</span> <span class="nv">%x</span><span class="p">,</span> <span class="m">0.000000e+00</span>
+  <span class="k">br</span> <span class="k">i1</span> <span class="nv">%ifcond</span><span class="p">,</span> <span class="kt">label</span> <span class="nv">%then</span><span class="p">,</span> <span class="kt">label</span> <span class="nv">%else</span>
+
+<span class="nl">then:</span>    <span class="c">; preds = %entry</span>
+  <span class="nv">%calltmp</span> <span class="p">=</span> <span class="k">call</span> <span class="kt">double</span> <span class="vg">@foo</span><span class="p">()</span>
+  <span class="k">br</span> <span class="kt">label</span> <span class="nv">%ifcont</span>
+
+<span class="nl">else:</span>    <span class="c">; preds = %entry</span>
+  <span class="nv">%calltmp1</span> <span class="p">=</span> <span class="k">call</span> <span class="kt">double</span> <span class="vg">@bar</span><span class="p">()</span>
+  <span class="k">br</span> <span class="kt">label</span> <span class="nv">%ifcont</span>
+
+<span class="nl">ifcont:</span>    <span class="c">; preds = %else, %then</span>
+  <span class="nv">%iftmp</span> <span class="p">=</span> <span class="k">phi</span> <span class="kt">double</span> <span class="p">[</span> <span class="nv">%calltmp</span><span class="p">,</span> <span class="nv">%then</span> <span class="p">],</span> <span class="p">[</span> <span class="nv">%calltmp1</span><span class="p">,</span> <span class="nv">%else</span> <span class="p">]</span>
+  <span class="k">ret</span> <span class="kt">double</span> <span class="nv">%iftmp</span>
+<span class="p">}</span>
+</pre></div>
+</div>
+<p>To visualize the control flow graph, you can use a nifty feature of the
+LLVM ‘<a class="reference external" href="http://llvm.org/cmds/opt.html">opt</a>‘ tool. If you put this LLVM
+IR into “t.ll” and run “<tt class="docutils literal"><span class="pre">llvm-as</span> <span class="pre"><</span> <span class="pre">t.ll</span> <span class="pre">|</span> <span class="pre">opt</span> <span class="pre">-analyze</span> <span class="pre">-view-cfg</span></tt>”, <a class="reference external" href="../ProgrammersManual.html#ViewGraph">a
+window will pop up</a> and you’ll
+see this graph:</p>
+<div class="figure align-center">
+<img alt="Example CFG" src="../_images/LangImpl5-cfg.png" />
+<p class="caption">Example CFG</p>
+</div>
+<p>Another way to get this is to call
+“<tt class="docutils literal"><span class="pre">Llvm_analysis.view_function_cfg</span> <span class="pre">f</span></tt>” or
+“<tt class="docutils literal"><span class="pre">Llvm_analysis.view_function_cfg_only</span> <span class="pre">f</span></tt>” (where <tt class="docutils literal"><span class="pre">f</span></tt> is a
+“<tt class="docutils literal"><span class="pre">Function</span></tt>”) either by inserting actual calls into the code and
+recompiling or by calling these in the debugger. LLVM has many nice
+features for visualizing various graphs.</p>
+<p>Getting back to the generated code, it is fairly simple: the entry block
+evaluates the conditional expression (“x” in our case here) and compares
+the result to 0.0 with the “<tt class="docutils literal"><span class="pre">fcmp</span> <span class="pre">one</span></tt>” instruction (‘one’ is “Ordered
+and Not Equal”). Based on the result of this expression, the code jumps
+to either the “then” or “else” blocks, which contain the expressions for
+the true/false cases.</p>
+<p>Once the then/else blocks are finished executing, they both branch back
+to the ‘ifcont’ block to execute the code that happens after the
+if/then/else. In this case the only thing left to do is to return to the
+caller of the function. The question then becomes: how does the code
+know which expression to return?</p>
+<p>The answer to this question involves an important SSA operation: the
+<a class="reference external" href="http://en.wikipedia.org/wiki/Static_single_assignment_form">Phi
+operation</a>.
+If you’re not familiar with SSA, <a class="reference external" href="http://en.wikipedia.org/wiki/Static_single_assignment_form">the wikipedia
+article</a>
+is a good introduction and there are various other introductions to it
+available on your favorite search engine. The short version is that
+“execution” of the Phi operation requires “remembering” which block
+control came from. The Phi operation takes on the value corresponding to
+the input control block. In this case, if control comes in from the
+“then” block, it gets the value of “calltmp”. If control comes from the
+“else” block, it gets the value of “calltmp1”.</p>
+<p>At this point, you are probably starting to think “Oh no! This means my
+simple and elegant front-end will have to start generating SSA form in
+order to use LLVM!”. Fortunately, this is not the case, and we strongly
+advise <em>not</em> implementing an SSA construction algorithm in your
+front-end unless there is an amazingly good reason to do so. In
+practice, there are two sorts of values that float around in code
+written for your average imperative programming language that might need
+Phi nodes:</p>
+<ol class="arabic simple">
+<li>Code that involves user variables: <tt class="docutils literal"><span class="pre">x</span> <span class="pre">=</span> <span class="pre">1;</span> <span class="pre">x</span> <span class="pre">=</span> <span class="pre">x</span> <span class="pre">+</span> <span class="pre">1;</span></tt></li>
+<li>Values that are implicit in the structure of your AST, such as the
+Phi node in this case.</li>
+</ol>
+<p>In <a class="reference external" href="OCamlLangImpl7.html">Chapter 7</a> of this tutorial (“mutable
+variables”), we’ll talk about #1 in depth. For now, just believe me that
+you don’t need SSA construction to handle this case. For #2, you have
+the choice of using the techniques that we will describe for #1, or you
+can insert Phi nodes directly, if convenient. In this case, it is really
+really easy to generate the Phi node, so we choose to do it directly.</p>
+<p>Okay, enough of the motivation and overview, lets generate code!</p>
+</div>
+<div class="section" id="code-generation-for-if-then-else">
+<h3><a class="toc-backref" href="#id7">5.2.5. Code Generation for If/Then/Else</a><a class="headerlink" href="#code-generation-for-if-then-else" title="Permalink to this headline">¶</a></h3>
+<p>In order to generate code for this, we implement the <tt class="docutils literal"><span class="pre">Codegen</span></tt> method
+for <tt class="docutils literal"><span class="pre">IfExprAST</span></tt>:</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="k">let</span> <span class="k">rec</span> <span class="n">codegen_expr</span> <span class="o">=</span> <span class="k">function</span>
+  <span class="o">...</span>
+  <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">If</span> <span class="o">(</span><span class="n">cond</span><span class="o">,</span> <span class="n">then_</span><span class="o">,</span> <span class="n">else_</span><span class="o">)</span> <span class="o">-></span>
+      <span class="k">let</span> <span class="n">cond</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">cond</span> <span class="k">in</span>
+
+      <span class="c">(* Convert condition to a bool by comparing equal to 0.0 *)</span>
+      <span class="k">let</span> <span class="n">zero</span> <span class="o">=</span> <span class="n">const_float</span> <span class="n">double_type</span> <span class="mi">0</span><span class="o">.</span><span class="mi">0</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">cond_val</span> <span class="o">=</span> <span class="n">build_fcmp</span> <span class="nn">Fcmp</span><span class="p">.</span><span class="nc">One</span> <span class="n">cond</span> <span class="n">zero</span> <span class="s2">"ifcond"</span> <span class="n">builder</span> <span class="k">in</span>
+</pre></div>
+</div>
+<p>This code is straightforward and similar to what we saw before. We emit
+the expression for the condition, then compare that value to zero to get
+a truth value as a 1-bit (bool) value.</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="c">(* Grab the first block so that we might later add the conditional branch</span>
+<span class="c"> * to it at the end of the function. *)</span>
+<span class="k">let</span> <span class="n">start_bb</span> <span class="o">=</span> <span class="n">insertion_block</span> <span class="n">builder</span> <span class="k">in</span>
+<span class="k">let</span> <span class="n">the_function</span> <span class="o">=</span> <span class="n">block_parent</span> <span class="n">start_bb</span> <span class="k">in</span>
+
+<span class="k">let</span> <span class="n">then_bb</span> <span class="o">=</span> <span class="n">append_block</span> <span class="n">context</span> <span class="s2">"then"</span> <span class="n">the_function</span> <span class="k">in</span>
+<span class="n">position_at_end</span> <span class="n">then_bb</span> <span class="n">builder</span><span class="o">;</span>
+</pre></div>
+</div>
+<p>As opposed to the <a class="reference external" href="LangImpl5.html">C++ tutorial</a>, we have to build our
+basic blocks bottom up since we can’t have dangling BasicBlocks. We
+start off by saving a pointer to the first block (which might not be the
+entry block), which we’ll need to build a conditional branch later. We
+do this by asking the <tt class="docutils literal"><span class="pre">builder</span></tt> for the current BasicBlock. The fourth
+line gets the current Function object that is being built. It gets this
+by the <tt class="docutils literal"><span class="pre">start_bb</span></tt> for its “parent” (the function it is currently
+embedded into).</p>
+<p>Once it has that, it creates one block. It is automatically appended
+into the function’s list of blocks.</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="c">(* Emit 'then' value. *)</span>
+<span class="n">position_at_end</span> <span class="n">then_bb</span> <span class="n">builder</span><span class="o">;</span>
+<span class="k">let</span> <span class="n">then_val</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">then_</span> <span class="k">in</span>
+
+<span class="c">(* Codegen of 'then' can change the current block, update then_bb for the</span>
+<span class="c"> * phi. We create a new name because one is used for the phi node, and the</span>
+<span class="c"> * other is used for the conditional branch. *)</span>
+<span class="k">let</span> <span class="n">new_then_bb</span> <span class="o">=</span> <span class="n">insertion_block</span> <span class="n">builder</span> <span class="k">in</span>
+</pre></div>
+</div>
+<p>We move the builder to start inserting into the “then” block. Strictly
+speaking, this call moves the insertion point to be at the end of the
+specified block. However, since the “then” block is empty, it also
+starts out by inserting at the beginning of the block. :)</p>
+<p>Once the insertion point is set, we recursively codegen the “then”
+expression from the AST.</p>
+<p>The final line here is quite subtle, but is very important. The basic
+issue is that when we create the Phi node in the merge block, we need to
+set up the block/value pairs that indicate how the Phi will work.
+Importantly, the Phi node expects to have an entry for each predecessor
+of the block in the CFG. Why then, are we getting the current block when
+we just set it to ThenBB 5 lines above? The problem is that the “Then”
+expression may actually itself change the block that the Builder is
+emitting into if, for example, it contains a nested “if/then/else”
+expression. Because calling Codegen recursively could arbitrarily change
+the notion of the current block, we are required to get an up-to-date
+value for code that will set up the Phi node.</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="c">(* Emit 'else' value. *)</span>
+<span class="k">let</span> <span class="n">else_bb</span> <span class="o">=</span> <span class="n">append_block</span> <span class="n">context</span> <span class="s2">"else"</span> <span class="n">the_function</span> <span class="k">in</span>
+<span class="n">position_at_end</span> <span class="n">else_bb</span> <span class="n">builder</span><span class="o">;</span>
+<span class="k">let</span> <span class="n">else_val</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">else_</span> <span class="k">in</span>
+
+<span class="c">(* Codegen of 'else' can change the current block, update else_bb for the</span>
+<span class="c"> * phi. *)</span>
+<span class="k">let</span> <span class="n">new_else_bb</span> <span class="o">=</span> <span class="n">insertion_block</span> <span class="n">builder</span> <span class="k">in</span>
+</pre></div>
+</div>
+<p>Code generation for the ‘else’ block is basically identical to codegen
+for the ‘then’ block.</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="c">(* Emit merge block. *)</span>
+<span class="k">let</span> <span class="n">merge_bb</span> <span class="o">=</span> <span class="n">append_block</span> <span class="n">context</span> <span class="s2">"ifcont"</span> <span class="n">the_function</span> <span class="k">in</span>
+<span class="n">position_at_end</span> <span class="n">merge_bb</span> <span class="n">builder</span><span class="o">;</span>
+<span class="k">let</span> <span class="n">incoming</span> <span class="o">=</span> <span class="o">[(</span><span class="n">then_val</span><span class="o">,</span> <span class="n">new_then_bb</span><span class="o">);</span> <span class="o">(</span><span class="n">else_val</span><span class="o">,</span> <span class="n">new_else_bb</span><span class="o">)]</span> <span class="k">in</span>
+<span class="k">let</span> <span class="n">phi</span> <span class="o">=</span> <span class="n">build_phi</span> <span class="n">incoming</span> <span class="s2">"iftmp"</span> <span class="n">builder</span> <span class="k">in</span>
+</pre></div>
+</div>
+<p>The first two lines here are now familiar: the first adds the “merge”
+block to the Function object. The second changes the insertion
+point so that newly created code will go into the “merge” block. Once
+that is done, we need to create the PHI node and set up the block/value
+pairs for the PHI.</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="c">(* Return to the start block to add the conditional branch. *)</span>
+<span class="n">position_at_end</span> <span class="n">start_bb</span> <span class="n">builder</span><span class="o">;</span>
+<span class="n">ignore</span> <span class="o">(</span><span class="n">build_cond_br</span> <span class="n">cond_val</span> <span class="n">then_bb</span> <span class="n">else_bb</span> <span class="n">builder</span><span class="o">);</span>
+</pre></div>
+</div>
+<p>Once the blocks are created, we can emit the conditional branch that
+chooses between them. Note that creating new blocks does not implicitly
+affect the IRBuilder, so it is still inserting into the block that the
+condition went into. This is why we needed to save the “start” block.</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="c">(* Set a unconditional branch at the end of the 'then' block and the</span>
+<span class="c"> * 'else' block to the 'merge' block. *)</span>
+<span class="n">position_at_end</span> <span class="n">new_then_bb</span> <span class="n">builder</span><span class="o">;</span> <span class="n">ignore</span> <span class="o">(</span><span class="n">build_br</span> <span class="n">merge_bb</span> <span class="n">builder</span><span class="o">);</span>
+<span class="n">position_at_end</span> <span class="n">new_else_bb</span> <span class="n">builder</span><span class="o">;</span> <span class="n">ignore</span> <span class="o">(</span><span class="n">build_br</span> <span class="n">merge_bb</span> <span class="n">builder</span><span class="o">);</span>
+
+<span class="c">(* Finally, set the builder to the end of the merge block. *)</span>
+<span class="n">position_at_end</span> <span class="n">merge_bb</span> <span class="n">builder</span><span class="o">;</span>
+
+<span class="n">phi</span>
+</pre></div>
+</div>
+<p>To finish off the blocks, we create an unconditional branch to the merge
+block. One interesting (and very important) aspect of the LLVM IR is
+that it <a class="reference external" href="../LangRef.html#functionstructure">requires all basic blocks to be
+“terminated”</a> with a <a class="reference external" href="../LangRef.html#terminators">control flow
+instruction</a> such as return or branch.
+This means that all control flow, <em>including fall throughs</em> must be made
+explicit in the LLVM IR. If you violate this rule, the verifier will
+emit an error.</p>
+<p>Finally, the CodeGen function returns the phi node as the value computed
+by the if/then/else expression. In our example above, this returned
+value will feed into the code for the top-level function, which will
+create the return instruction.</p>
+<p>Overall, we now have the ability to execute conditional code in
+Kaleidoscope. With this extension, Kaleidoscope is a fairly complete
+language that can calculate a wide variety of numeric functions. Next up
+we’ll add another useful expression that is familiar from non-functional
+languages...</p>
+</div>
+</div>
+<div class="section" id="for-loop-expression">
+<h2><a class="toc-backref" href="#id8">5.3. ‘for’ Loop Expression</a><a class="headerlink" href="#for-loop-expression" title="Permalink to this headline">¶</a></h2>
+<p>Now that we know how to add basic control flow constructs to the
+language, we have the tools to add more powerful things. Lets add
+something more aggressive, a ‘for’ expression:</p>
+<div class="highlight-python"><div class="highlight"><pre>extern putchard(char);
+def printstar(n)
+  for i = 1, i < n, 1.0 in
+    putchard(42);  # ascii 42 = '*'
+
+# print 100 '*' characters
+printstar(100);
+</pre></div>
+</div>
+<p>This expression defines a new variable (“i” in this case) which iterates
+from a starting value, while the condition (“i < n” in this case) is
+true, incrementing by an optional step value (“1.0” in this case). If
+the step value is omitted, it defaults to 1.0. While the loop is true,
+it executes its body expression. Because we don’t have anything better
+to return, we’ll just define the loop as always returning 0.0. In the
+future when we have mutable variables, it will get more useful.</p>
+<p>As before, lets talk about the changes that we need to Kaleidoscope to
+support this.</p>
+<div class="section" id="lexer-extensions-for-the-for-loop">
+<h3><a class="toc-backref" href="#id9">5.3.1. Lexer Extensions for the ‘for’ Loop</a><a class="headerlink" href="#lexer-extensions-for-the-for-loop" title="Permalink to this headline">¶</a></h3>
+<p>The lexer extensions are the same sort of thing as for if/then/else:</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="o">...</span> <span class="k">in</span> <span class="nn">Token</span><span class="p">.</span><span class="n">token</span> <span class="o">...</span>
+<span class="c">(* control *)</span>
+<span class="o">|</span> <span class="nc">If</span> <span class="o">|</span> <span class="nc">Then</span> <span class="o">|</span> <span class="nc">Else</span>
+<span class="o">|</span> <span class="nc">For</span> <span class="o">|</span> <span class="nn">In</span>
+
+<span class="p">...</span> <span class="n">in</span> <span class="nn">Lexer</span><span class="p">.</span><span class="n">lex_ident</span><span class="o">...</span>
+    <span class="k">match</span> <span class="nn">Buffer</span><span class="p">.</span><span class="n">contents</span> <span class="n">buffer</span> <span class="k">with</span>
+    <span class="o">|</span> <span class="s2">"def"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Def</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+    <span class="o">|</span> <span class="s2">"extern"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Extern</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+    <span class="o">|</span> <span class="s2">"if"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">If</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+    <span class="o">|</span> <span class="s2">"then"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Then</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+    <span class="o">|</span> <span class="s2">"else"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Else</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+    <span class="o">|</span> <span class="s2">"for"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">For</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+    <span class="o">|</span> <span class="s2">"in"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">In</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+    <span class="o">|</span> <span class="n">id</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Ident</span> <span class="n">id</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+</pre></div>
+</div>
+</div>
+<div class="section" id="ast-extensions-for-the-for-loop">
+<h3><a class="toc-backref" href="#id10">5.3.2. AST Extensions for the ‘for’ Loop</a><a class="headerlink" href="#ast-extensions-for-the-for-loop" title="Permalink to this headline">¶</a></h3>
+<p>The AST variant is just as simple. It basically boils down to capturing
+the variable name and the constituent expressions in the node.</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="k">type</span> <span class="n">expr</span> <span class="o">=</span>
+  <span class="o">...</span>
+  <span class="c">(* variant for for/in. *)</span>
+  <span class="o">|</span> <span class="nc">For</span> <span class="k">of</span> <span class="kt">string</span> <span class="o">*</span> <span class="n">expr</span> <span class="o">*</span> <span class="n">expr</span> <span class="o">*</span> <span class="n">expr</span> <span class="n">option</span> <span class="o">*</span> <span class="n">expr</span>
+</pre></div>
+</div>
+</div>
+<div class="section" id="parser-extensions-for-the-for-loop">
+<h3><a class="toc-backref" href="#id11">5.3.3. Parser Extensions for the ‘for’ Loop</a><a class="headerlink" href="#parser-extensions-for-the-for-loop" title="Permalink to this headline">¶</a></h3>
+<p>The parser code is also fairly standard. The only interesting thing here
+is handling of the optional step value. The parser code handles it by
+checking to see if the second comma is present. If not, it sets the step
+value to null in the AST node:</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="k">let</span> <span class="k">rec</span> <span class="n">parse_primary</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="o">...</span>
+  <span class="c">(* forexpr</span>
+<span class="c">        ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">For</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Ident</span> <span class="n">id</span> <span class="o">??</span> <span class="s2">"expected identifier after for"</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">'='</span> <span class="o">??</span> <span class="s2">"expected '=' after for"</span><span class="o">;</span>
+       <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="k">begin</span> <span class="n">parser</span>
+        <span class="o">|</span> <span class="o">[<</span>
+             <span class="n">start</span><span class="o">=</span><span class="n">parse_expr</span><span class="o">;</span>
+             <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">','</span> <span class="o">??</span> <span class="s2">"expected ',' after for"</span><span class="o">;</span>
+             <span class="n">end_</span><span class="o">=</span><span class="n">parse_expr</span><span class="o">;</span>
+             <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span>
+            <span class="k">let</span> <span class="n">step</span> <span class="o">=</span>
+              <span class="k">begin</span> <span class="n">parser</span>
+              <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">','</span><span class="o">;</span> <span class="n">step</span><span class="o">=</span><span class="n">parse_expr</span> <span class="o">>]</span> <span class="o">-></span> <span class="nc">Some</span> <span class="n">step</span>
+              <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span> <span class="nc">None</span>
+              <span class="k">end</span> <span class="n">stream</span>
+            <span class="k">in</span>
+            <span class="k">begin</span> <span class="n">parser</span>
+            <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">In</span><span class="o">;</span> <span class="n">body</span><span class="o">=</span><span class="n">parse_expr</span> <span class="o">>]</span> <span class="o">-></span>
+                <span class="nn">Ast</span><span class="p">.</span><span class="nc">For</span> <span class="o">(</span><span class="n">id</span><span class="o">,</span> <span class="n">start</span><span class="o">,</span> <span class="n">end_</span><span class="o">,</span> <span class="n">step</span><span class="o">,</span> <span class="n">body</span><span class="o">)</span>
+            <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span>
+                <span class="k">raise</span> <span class="o">(</span><span class="nn">Stream</span><span class="p">.</span><span class="nc">Error</span> <span class="s2">"expected 'in' after for"</span><span class="o">)</span>
+            <span class="k">end</span> <span class="n">stream</span>
+        <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span>
+            <span class="k">raise</span> <span class="o">(</span><span class="nn">Stream</span><span class="p">.</span><span class="nc">Error</span> <span class="s2">"expected '=' after for"</span><span class="o">)</span>
+      <span class="k">end</span> <span class="n">stream</span>
+</pre></div>
+</div>
+</div>
+<div class="section" id="llvm-ir-for-the-for-loop">
+<h3><a class="toc-backref" href="#id12">5.3.4. LLVM IR for the ‘for’ Loop</a><a class="headerlink" href="#llvm-ir-for-the-for-loop" title="Permalink to this headline">¶</a></h3>
+<p>Now we get to the good part: the LLVM IR we want to generate for this
+thing. With the simple example above, we get this LLVM IR (note that
+this dump is generated with optimizations disabled for clarity):</p>
+<div class="highlight-llvm"><div class="highlight"><pre><span class="k">declare</span> <span class="kt">double</span> <span class="vg">@putchard</span><span class="p">(</span><span class="kt">double</span><span class="p">)</span>
+
+<span class="k">define</span> <span class="kt">double</span> <span class="vg">@printstar</span><span class="p">(</span><span class="kt">double</span> <span class="nv">%n</span><span class="p">)</span> <span class="p">{</span>
+<span class="nl">entry:</span>
+        <span class="c">; initial value = 1.0 (inlined into phi)</span>
+  <span class="k">br</span> <span class="kt">label</span> <span class="nv">%loop</span>
+
+<span class="nl">loop:</span>    <span class="c">; preds = %loop, %entry</span>
+  <span class="nv">%i</span> <span class="p">=</span> <span class="k">phi</span> <span class="kt">double</span> <span class="p">[</span> <span class="m">1.000000e+00</span><span class="p">,</span> <span class="nv">%entry</span> <span class="p">],</span> <span class="p">[</span> <span class="nv">%nextvar</span><span class="p">,</span> <span class="nv">%loop</span> <span class="p">]</span>
+        <span class="c">; body</span>
+  <span class="nv">%calltmp</span> <span class="p">=</span> <span class="k">call</span> <span class="kt">double</span> <span class="vg">@putchard</span><span class="p">(</span><span class="kt">double</span> <span class="m">4.200000e+01</span><span class="p">)</span>
+        <span class="c">; increment</span>
+  <span class="nv">%nextvar</span> <span class="p">=</span> <span class="k">fadd</span> <span class="kt">double</span> <span class="nv">%i</span><span class="p">,</span> <span class="m">1.000000e+00</span>
+
+        <span class="c">; termination test</span>
+  <span class="nv">%cmptmp</span> <span class="p">=</span> <span class="k">fcmp</span> <span class="k">ult</span> <span class="kt">double</span> <span class="nv">%i</span><span class="p">,</span> <span class="nv">%n</span>
+  <span class="nv">%booltmp</span> <span class="p">=</span> <span class="k">uitofp</span> <span class="k">i1</span> <span class="nv">%cmptmp</span> <span class="k">to</span> <span class="kt">double</span>
+  <span class="nv">%loopcond</span> <span class="p">=</span> <span class="k">fcmp</span> <span class="k">one</span> <span class="kt">double</span> <span class="nv">%booltmp</span><span class="p">,</span> <span class="m">0.000000e+00</span>
+  <span class="k">br</span> <span class="k">i1</span> <span class="nv">%loopcond</span><span class="p">,</span> <span class="kt">label</span> <span class="nv">%loop</span><span class="p">,</span> <span class="kt">label</span> <span class="nv">%afterloop</span>
+
+<span class="nl">afterloop:</span>    <span class="c">; preds = %loop</span>
+        <span class="c">; loop always returns 0.0</span>
+  <span class="k">ret</span> <span class="kt">double</span> <span class="m">0.000000e+00</span>
+<span class="p">}</span>
+</pre></div>
+</div>
+<p>This loop contains all the same constructs we saw before: a phi node,
+several expressions, and some basic blocks. Lets see how this fits
+together.</p>
+</div>
+<div class="section" id="code-generation-for-the-for-loop">
+<h3><a class="toc-backref" href="#id13">5.3.5. Code Generation for the ‘for’ Loop</a><a class="headerlink" href="#code-generation-for-the-for-loop" title="Permalink to this headline">¶</a></h3>
+<p>The first part of Codegen is very simple: we just output the start
+expression for the loop value:</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="k">let</span> <span class="k">rec</span> <span class="n">codegen_expr</span> <span class="o">=</span> <span class="k">function</span>
+  <span class="o">...</span>
+  <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">For</span> <span class="o">(</span><span class="n">var_name</span><span class="o">,</span> <span class="n">start</span><span class="o">,</span> <span class="n">end_</span><span class="o">,</span> <span class="n">step</span><span class="o">,</span> <span class="n">body</span><span class="o">)</span> <span class="o">-></span>
+      <span class="c">(* Emit the start code first, without 'variable' in scope. *)</span>
+      <span class="k">let</span> <span class="n">start_val</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">start</span> <span class="k">in</span>
+</pre></div>
+</div>
+<p>With this out of the way, the next step is to set up the LLVM basic
+block for the start of the loop body. In the case above, the whole loop
+body is one block, but remember that the body code itself could consist
+of multiple blocks (e.g. if it contains an if/then/else or a for/in
+expression).</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="c">(* Make the new basic block for the loop header, inserting after current</span>
+<span class="c"> * block. *)</span>
+<span class="k">let</span> <span class="n">preheader_bb</span> <span class="o">=</span> <span class="n">insertion_block</span> <span class="n">builder</span> <span class="k">in</span>
+<span class="k">let</span> <span class="n">the_function</span> <span class="o">=</span> <span class="n">block_parent</span> <span class="n">preheader_bb</span> <span class="k">in</span>
+<span class="k">let</span> <span class="n">loop_bb</span> <span class="o">=</span> <span class="n">append_block</span> <span class="n">context</span> <span class="s2">"loop"</span> <span class="n">the_function</span> <span class="k">in</span>
+
+<span class="c">(* Insert an explicit fall through from the current block to the</span>
+<span class="c"> * loop_bb. *)</span>
+<span class="n">ignore</span> <span class="o">(</span><span class="n">build_br</span> <span class="n">loop_bb</span> <span class="n">builder</span><span class="o">);</span>
+</pre></div>
+</div>
+<p>This code is similar to what we saw for if/then/else. Because we will
+need it to create the Phi node, we remember the block that falls through
+into the loop. Once we have that, we create the actual block that starts
+the loop and create an unconditional branch for the fall-through between
+the two blocks.</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="c">(* Start insertion in loop_bb. *)</span>
+<span class="n">position_at_end</span> <span class="n">loop_bb</span> <span class="n">builder</span><span class="o">;</span>
+
+<span class="c">(* Start the PHI node with an entry for start. *)</span>
+<span class="k">let</span> <span class="n">variable</span> <span class="o">=</span> <span class="n">build_phi</span> <span class="o">[(</span><span class="n">start_val</span><span class="o">,</span> <span class="n">preheader_bb</span><span class="o">)]</span> <span class="n">var_name</span> <span class="n">builder</span> <span class="k">in</span>
+</pre></div>
+</div>
+<p>Now that the “preheader” for the loop is set up, we switch to emitting
+code for the loop body. To begin with, we move the insertion point and
+create the PHI node for the loop induction variable. Since we already
+know the incoming value for the starting value, we add it to the Phi
+node. Note that the Phi will eventually get a second value for the
+backedge, but we can’t set it up yet (because it doesn’t exist!).</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="c">(* Within the loop, the variable is defined equal to the PHI node. If it</span>
+<span class="c"> * shadows an existing variable, we have to restore it, so save it</span>
+<span class="c"> * now. *)</span>
+<span class="k">let</span> <span class="n">old_val</span> <span class="o">=</span>
+  <span class="k">try</span> <span class="nc">Some</span> <span class="o">(</span><span class="nn">Hashtbl</span><span class="p">.</span><span class="n">find</span> <span class="n">named_values</span> <span class="n">var_name</span><span class="o">)</span> <span class="k">with</span> <span class="nc">Not_found</span> <span class="o">-></span> <span class="nc">None</span>
+<span class="k">in</span>
+<span class="nn">Hashtbl</span><span class="p">.</span><span class="n">add</span> <span class="n">named_values</span> <span class="n">var_name</span> <span class="n">variable</span><span class="o">;</span>
+
+<span class="c">(* Emit the body of the loop.  This, like any other expr, can change the</span>
+<span class="c"> * current BB.  Note that we ignore the value computed by the body, but</span>
+<span class="c"> * don't allow an error *)</span>
+<span class="n">ignore</span> <span class="o">(</span><span class="n">codegen_expr</span> <span class="n">body</span><span class="o">);</span>
+</pre></div>
+</div>
+<p>Now the code starts to get more interesting. Our ‘for’ loop introduces a
+new variable to the symbol table. This means that our symbol table can
+now contain either function arguments or loop variables. To handle this,
+before we codegen the body of the loop, we add the loop variable as the
+current value for its name. Note that it is possible that there is a
+variable of the same name in the outer scope. It would be easy to make
+this an error (emit an error and return null if there is already an
+entry for VarName) but we choose to allow shadowing of variables. In
+order to handle this correctly, we remember the Value that we are
+potentially shadowing in <tt class="docutils literal"><span class="pre">old_val</span></tt> (which will be None if there is no
+shadowed variable).</p>
+<p>Once the loop variable is set into the symbol table, the code
+recursively codegen’s the body. This allows the body to use the loop
+variable: any references to it will naturally find it in the symbol
+table.</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="c">(* Emit the step value. *)</span>
+<span class="k">let</span> <span class="n">step_val</span> <span class="o">=</span>
+  <span class="k">match</span> <span class="n">step</span> <span class="k">with</span>
+  <span class="o">|</span> <span class="nc">Some</span> <span class="n">step</span> <span class="o">-></span> <span class="n">codegen_expr</span> <span class="n">step</span>
+  <span class="c">(* If not specified, use 1.0. *)</span>
+  <span class="o">|</span> <span class="nc">None</span> <span class="o">-></span> <span class="n">const_float</span> <span class="n">double_type</span> <span class="mi">1</span><span class="o">.</span><span class="mi">0</span>
+<span class="k">in</span>
+
+<span class="k">let</span> <span class="n">next_var</span> <span class="o">=</span> <span class="n">build_add</span> <span class="n">variable</span> <span class="n">step_val</span> <span class="s2">"nextvar"</span> <span class="n">builder</span> <span class="k">in</span>
+</pre></div>
+</div>
+<p>Now that the body is emitted, we compute the next value of the iteration
+variable by adding the step value, or 1.0 if it isn’t present.
+‘<tt class="docutils literal"><span class="pre">next_var</span></tt>‘ will be the value of the loop variable on the next
+iteration of the loop.</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="c">(* Compute the end condition. *)</span>
+<span class="k">let</span> <span class="n">end_cond</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">end_</span> <span class="k">in</span>
+
+<span class="c">(* Convert condition to a bool by comparing equal to 0.0. *)</span>
+<span class="k">let</span> <span class="n">zero</span> <span class="o">=</span> <span class="n">const_float</span> <span class="n">double_type</span> <span class="mi">0</span><span class="o">.</span><span class="mi">0</span> <span class="k">in</span>
+<span class="k">let</span> <span class="n">end_cond</span> <span class="o">=</span> <span class="n">build_fcmp</span> <span class="nn">Fcmp</span><span class="p">.</span><span class="nc">One</span> <span class="n">end_cond</span> <span class="n">zero</span> <span class="s2">"loopcond"</span> <span class="n">builder</span> <span class="k">in</span>
+</pre></div>
+</div>
+<p>Finally, we evaluate the exit value of the loop, to determine whether
+the loop should exit. This mirrors the condition evaluation for the
+if/then/else statement.</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="c">(* Create the "after loop" block and insert it. *)</span>
+<span class="k">let</span> <span class="n">loop_end_bb</span> <span class="o">=</span> <span class="n">insertion_block</span> <span class="n">builder</span> <span class="k">in</span>
+<span class="k">let</span> <span class="n">after_bb</span> <span class="o">=</span> <span class="n">append_block</span> <span class="n">context</span> <span class="s2">"afterloop"</span> <span class="n">the_function</span> <span class="k">in</span>
+
+<span class="c">(* Insert the conditional branch into the end of loop_end_bb. *)</span>
+<span class="n">ignore</span> <span class="o">(</span><span class="n">build_cond_br</span> <span class="n">end_cond</span> <span class="n">loop_bb</span> <span class="n">after_bb</span> <span class="n">builder</span><span class="o">);</span>
+
+<span class="c">(* Any new code will be inserted in after_bb. *)</span>
+<span class="n">position_at_end</span> <span class="n">after_bb</span> <span class="n">builder</span><span class="o">;</span>
+</pre></div>
+</div>
+<p>With the code for the body of the loop complete, we just need to finish
+up the control flow for it. This code remembers the end block (for the
+phi node), then creates the block for the loop exit (“afterloop”). Based
+on the value of the exit condition, it creates a conditional branch that
+chooses between executing the loop again and exiting the loop. Any
+future code is emitted in the “afterloop” block, so it sets the
+insertion position to it.</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="c">(* Add a new entry to the PHI node for the backedge. *)</span>
+<span class="n">add_incoming</span> <span class="o">(</span><span class="n">next_var</span><span class="o">,</span> <span class="n">loop_end_bb</span><span class="o">)</span> <span class="n">variable</span><span class="o">;</span>
+
+<span class="c">(* Restore the unshadowed variable. *)</span>
+<span class="k">begin</span> <span class="k">match</span> <span class="n">old_val</span> <span class="k">with</span>
+<span class="o">|</span> <span class="nc">Some</span> <span class="n">old_val</span> <span class="o">-></span> <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">add</span> <span class="n">named_values</span> <span class="n">var_name</span> <span class="n">old_val</span>
+<span class="o">|</span> <span class="nc">None</span> <span class="o">-></span> <span class="bp">()</span>
+<span class="k">end</span><span class="o">;</span>
+
+<span class="c">(* for expr always returns 0.0. *)</span>
+<span class="n">const_null</span> <span class="n">double_type</span>
+</pre></div>
+</div>
+<p>The final code handles various cleanups: now that we have the
+“<tt class="docutils literal"><span class="pre">next_var</span></tt>” value, we can add the incoming value to the loop PHI
+node. After that, we remove the loop variable from the symbol table, so
+that it isn’t in scope after the for loop. Finally, code generation of
+the for loop always returns 0.0, so that is what we return from
+<tt class="docutils literal"><span class="pre">Codegen.codegen_expr</span></tt>.</p>
+<p>With this, we conclude the “adding control flow to Kaleidoscope” chapter
+of the tutorial. In this chapter we added two control flow constructs,
+and used them to motivate a couple of aspects of the LLVM IR that are
+important for front-end implementors to know. In the next chapter of our
+saga, we will get a bit crazier and add <a class="reference external" href="OCamlLangImpl6.html">user-defined
+operators</a> to our poor innocent language.</p>
+</div>
+</div>
+<div class="section" id="full-code-listing">
+<h2><a class="toc-backref" href="#id14">5.4. Full Code Listing</a><a class="headerlink" href="#full-code-listing" title="Permalink to this headline">¶</a></h2>
+<p>Here is the complete code listing for our running example, enhanced with
+the if/then/else and for expressions.. To build this example, use:</p>
+<div class="highlight-bash"><div class="highlight"><pre><span class="c"># Compile</span>
+ocamlbuild toy.byte
+<span class="c"># Run</span>
+./toy.byte
+</pre></div>
+</div>
+<p>Here is the code:</p>
+<dl class="docutils">
+<dt>_tags:</dt>
+<dd><div class="first last highlight-python"><div class="highlight"><pre><{lexer,parser}.ml>: use_camlp4, pp(camlp4of)
+<*.{byte,native}>: g++, use_llvm, use_llvm_analysis
+<*.{byte,native}>: use_llvm_executionengine, use_llvm_target
+<*.{byte,native}>: use_llvm_scalar_opts, use_bindings
+</pre></div>
+</div>
+</dd>
+<dt>myocamlbuild.ml:</dt>
+<dd><div class="first last highlight-ocaml"><div class="highlight"><pre><span class="k">open</span> <span class="nc">Ocamlbuild_plugin</span><span class="o">;;</span>
+
+<span class="n">ocaml_lib</span> <span class="o">~</span><span class="n">extern</span><span class="o">:</span><span class="bp">true</span> <span class="s2">"llvm"</span><span class="o">;;</span>
+<span class="n">ocaml_lib</span> <span class="o">~</span><span class="n">extern</span><span class="o">:</span><span class="bp">true</span> <span class="s2">"llvm_analysis"</span><span class="o">;;</span>
+<span class="n">ocaml_lib</span> <span class="o">~</span><span class="n">extern</span><span class="o">:</span><span class="bp">true</span> <span class="s2">"llvm_executionengine"</span><span class="o">;;</span>
+<span class="n">ocaml_lib</span> <span class="o">~</span><span class="n">extern</span><span class="o">:</span><span class="bp">true</span> <span class="s2">"llvm_target"</span><span class="o">;;</span>
+<span class="n">ocaml_lib</span> <span class="o">~</span><span class="n">extern</span><span class="o">:</span><span class="bp">true</span> <span class="s2">"llvm_scalar_opts"</span><span class="o">;;</span>
+
+<span class="n">flag</span> <span class="o">[</span><span class="s2">"link"</span><span class="o">;</span> <span class="s2">"ocaml"</span><span class="o">;</span> <span class="s2">"g++"</span><span class="o">]</span> <span class="o">(</span><span class="nc">S</span><span class="o">[</span><span class="nc">A</span><span class="s2">"-cc"</span><span class="o">;</span> <span class="nc">A</span><span class="s2">"g++"</span><span class="o">]);;</span>
+<span class="n">dep</span> <span class="o">[</span><span class="s2">"link"</span><span class="o">;</span> <span class="s2">"ocaml"</span><span class="o">;</span> <span class="s2">"use_bindings"</span><span class="o">]</span> <span class="o">[</span><span class="s2">"bindings.o"</span><span class="o">];;</span>
+</pre></div>
+</div>
+</dd>
+<dt>token.ml:</dt>
+<dd><div class="first last highlight-ocaml"><div class="highlight"><pre><span class="c">(*===----------------------------------------------------------------------===</span>
+<span class="c"> * Lexer Tokens</span>
+<span class="c"> *===----------------------------------------------------------------------===*)</span>
+
+<span class="c">(* The lexer returns these 'Kwd' if it is an unknown character, otherwise one of</span>
+<span class="c"> * these others for known things. *)</span>
+<span class="k">type</span> <span class="n">token</span> <span class="o">=</span>
+  <span class="c">(* commands *)</span>
+  <span class="o">|</span> <span class="nc">Def</span> <span class="o">|</span> <span class="nc">Extern</span>
+
+  <span class="c">(* primary *)</span>
+  <span class="o">|</span> <span class="nc">Ident</span> <span class="k">of</span> <span class="kt">string</span> <span class="o">|</span> <span class="nc">Number</span> <span class="k">of</span> <span class="kt">float</span>
+
+  <span class="c">(* unknown *)</span>
+  <span class="o">|</span> <span class="nc">Kwd</span> <span class="k">of</span> <span class="kt">char</span>
+
+  <span class="c">(* control *)</span>
+  <span class="o">|</span> <span class="nc">If</span> <span class="o">|</span> <span class="nc">Then</span> <span class="o">|</span> <span class="nc">Else</span>
+  <span class="o">|</span> <span class="nc">For</span> <span class="o">|</span> <span class="nc">In</span>
+</pre></div>
+</div>
+</dd>
+<dt>lexer.ml:</dt>
+<dd><div class="first last highlight-ocaml"><div class="highlight"><pre><span class="c">(*===----------------------------------------------------------------------===</span>
+<span class="c"> * Lexer</span>
+<span class="c"> *===----------------------------------------------------------------------===*)</span>
+
+<span class="k">let</span> <span class="k">rec</span> <span class="n">lex</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="c">(* Skip any whitespace. *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span> <span class="o">(</span><span class="sc">' '</span> <span class="o">|</span> <span class="sc">'\n'</span> <span class="o">|</span> <span class="sc">'\r'</span> <span class="o">|</span> <span class="sc">'\t'</span><span class="o">);</span> <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">lex</span> <span class="n">stream</span>
+
+  <span class="c">(* identifier: [a-zA-Z][a-zA-Z0-9] *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span> <span class="o">(</span><span class="sc">'A'</span> <span class="o">..</span> <span class="sc">'Z'</span> <span class="o">|</span> <span class="sc">'a'</span> <span class="o">..</span> <span class="sc">'z'</span> <span class="k">as</span> <span class="n">c</span><span class="o">);</span> <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="k">let</span> <span class="n">buffer</span> <span class="o">=</span> <span class="nn">Buffer</span><span class="p">.</span><span class="n">create</span> <span class="mi">1</span> <span class="k">in</span>
+      <span class="nn">Buffer</span><span class="p">.</span><span class="n">add_char</span> <span class="n">buffer</span> <span class="n">c</span><span class="o">;</span>
+      <span class="n">lex_ident</span> <span class="n">buffer</span> <span class="n">stream</span>
+
+  <span class="c">(* number: [0-9.]+ *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span> <span class="o">(</span><span class="sc">'0'</span> <span class="o">..</span> <span class="sc">'9'</span> <span class="k">as</span> <span class="n">c</span><span class="o">);</span> <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="k">let</span> <span class="n">buffer</span> <span class="o">=</span> <span class="nn">Buffer</span><span class="p">.</span><span class="n">create</span> <span class="mi">1</span> <span class="k">in</span>
+      <span class="nn">Buffer</span><span class="p">.</span><span class="n">add_char</span> <span class="n">buffer</span> <span class="n">c</span><span class="o">;</span>
+      <span class="n">lex_number</span> <span class="n">buffer</span> <span class="n">stream</span>
+
+  <span class="c">(* Comment until end of line. *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span> <span class="o">(</span><span class="sc">'#'</span><span class="o">);</span> <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="n">lex_comment</span> <span class="n">stream</span>
+
+  <span class="c">(* Otherwise, just return the character as its ascii value. *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="n">c</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="n">c</span><span class="o">;</span> <span class="n">lex</span> <span class="n">stream</span> <span class="o">>]</span>
+
+  <span class="c">(* end of stream. *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span> <span class="o">[<</span> <span class="o">>]</span>
+
+<span class="ow">and</span> <span class="n">lex_number</span> <span class="n">buffer</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span> <span class="o">(</span><span class="sc">'0'</span> <span class="o">..</span> <span class="sc">'9'</span> <span class="o">|</span> <span class="sc">'.'</span> <span class="k">as</span> <span class="n">c</span><span class="o">);</span> <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="nn">Buffer</span><span class="p">.</span><span class="n">add_char</span> <span class="n">buffer</span> <span class="n">c</span><span class="o">;</span>
+      <span class="n">lex_number</span> <span class="n">buffer</span> <span class="n">stream</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="n">stream</span><span class="o">=</span><span class="n">lex</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Number</span> <span class="o">(</span><span class="n">float_of_string</span> <span class="o">(</span><span class="nn">Buffer</span><span class="p">.</span><span class="n">contents</span> <span class="n">buffer</span><span class="o">));</span> <span class="n">stream</span> <span class="o">>]</span>
+
+<span class="ow">and</span> <span class="n">lex_ident</span> <span class="n">buffer</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span> <span class="o">(</span><span class="sc">'A'</span> <span class="o">..</span> <span class="sc">'Z'</span> <span class="o">|</span> <span class="sc">'a'</span> <span class="o">..</span> <span class="sc">'z'</span> <span class="o">|</span> <span class="sc">'0'</span> <span class="o">..</span> <span class="sc">'9'</span> <span class="k">as</span> <span class="n">c</span><span class="o">);</span> <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="nn">Buffer</span><span class="p">.</span><span class="n">add_char</span> <span class="n">buffer</span> <span class="n">c</span><span class="o">;</span>
+      <span class="n">lex_ident</span> <span class="n">buffer</span> <span class="n">stream</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="n">stream</span><span class="o">=</span><span class="n">lex</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="k">match</span> <span class="nn">Buffer</span><span class="p">.</span><span class="n">contents</span> <span class="n">buffer</span> <span class="k">with</span>
+      <span class="o">|</span> <span class="s2">"def"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Def</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+      <span class="o">|</span> <span class="s2">"extern"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Extern</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+      <span class="o">|</span> <span class="s2">"if"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">If</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+      <span class="o">|</span> <span class="s2">"then"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Then</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+      <span class="o">|</span> <span class="s2">"else"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Else</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+      <span class="o">|</span> <span class="s2">"for"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">For</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+      <span class="o">|</span> <span class="s2">"in"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">In</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+      <span class="o">|</span> <span class="n">id</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Ident</span> <span class="n">id</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+
+<span class="ow">and</span> <span class="n">lex_comment</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span> <span class="o">(</span><span class="sc">'\n'</span><span class="o">);</span> <span class="n">stream</span><span class="o">=</span><span class="n">lex</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">stream</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="n">c</span><span class="o">;</span> <span class="n">e</span><span class="o">=</span><span class="n">lex_comment</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">e</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span> <span class="o">[<</span> <span class="o">>]</span>
+</pre></div>
+</div>
+</dd>
+<dt>ast.ml:</dt>
+<dd><div class="first last highlight-ocaml"><div class="highlight"><pre><span class="c">(*===----------------------------------------------------------------------===</span>
+<span class="c"> * Abstract Syntax Tree (aka Parse Tree)</span>
+<span class="c"> *===----------------------------------------------------------------------===*)</span>
+
+<span class="c">(* expr - Base type for all expression nodes. *)</span>
+<span class="k">type</span> <span class="n">expr</span> <span class="o">=</span>
+  <span class="c">(* variant for numeric literals like "1.0". *)</span>
+  <span class="o">|</span> <span class="nc">Number</span> <span class="k">of</span> <span class="kt">float</span>
+
+  <span class="c">(* variant for referencing a variable, like "a". *)</span>
+  <span class="o">|</span> <span class="nc">Variable</span> <span class="k">of</span> <span class="kt">string</span>
+
+  <span class="c">(* variant for a binary operator. *)</span>
+  <span class="o">|</span> <span class="nc">Binary</span> <span class="k">of</span> <span class="kt">char</span> <span class="o">*</span> <span class="n">expr</span> <span class="o">*</span> <span class="n">expr</span>
+
+  <span class="c">(* variant for function calls. *)</span>
+  <span class="o">|</span> <span class="nc">Call</span> <span class="k">of</span> <span class="kt">string</span> <span class="o">*</span> <span class="n">expr</span> <span class="kt">array</span>
+
+  <span class="c">(* variant for if/then/else. *)</span>
+  <span class="o">|</span> <span class="nc">If</span> <span class="k">of</span> <span class="n">expr</span> <span class="o">*</span> <span class="n">expr</span> <span class="o">*</span> <span class="n">expr</span>
+
+  <span class="c">(* variant for for/in. *)</span>
+  <span class="o">|</span> <span class="nc">For</span> <span class="k">of</span> <span class="kt">string</span> <span class="o">*</span> <span class="n">expr</span> <span class="o">*</span> <span class="n">expr</span> <span class="o">*</span> <span class="n">expr</span> <span class="n">option</span> <span class="o">*</span> <span class="n">expr</span>
+
+<span class="c">(* proto - This type represents the "prototype" for a function, which captures</span>
+<span class="c"> * its name, and its argument names (thus implicitly the number of arguments the</span>
+<span class="c"> * function takes). *)</span>
+<span class="k">type</span> <span class="n">proto</span> <span class="o">=</span> <span class="nc">Prototype</span> <span class="k">of</span> <span class="kt">string</span> <span class="o">*</span> <span class="kt">string</span> <span class="kt">array</span>
+
+<span class="c">(* func - This type represents a function definition itself. *)</span>
+<span class="k">type</span> <span class="n">func</span> <span class="o">=</span> <span class="nc">Function</span> <span class="k">of</span> <span class="n">proto</span> <span class="o">*</span> <span class="n">expr</span>
+</pre></div>
+</div>
+</dd>
+<dt>parser.ml:</dt>
+<dd><div class="first last highlight-ocaml"><div class="highlight"><pre><span class="c">(*===---------------------------------------------------------------------===</span>
+<span class="c"> * Parser</span>
+<span class="c"> *===---------------------------------------------------------------------===*)</span>
+
+<span class="c">(* binop_precedence - This holds the precedence for each binary operator that is</span>
+<span class="c"> * defined *)</span>
+<span class="k">let</span> <span class="n">binop_precedence</span><span class="o">:(</span><span class="kt">char</span><span class="o">,</span> <span class="kt">int</span><span class="o">)</span> <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">t</span> <span class="o">=</span> <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">create</span> <span class="mi">10</span>
+
+<span class="c">(* precedence - Get the precedence of the pending binary operator token. *)</span>
+<span class="k">let</span> <span class="n">precedence</span> <span class="n">c</span> <span class="o">=</span> <span class="k">try</span> <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">find</span> <span class="n">binop_precedence</span> <span class="n">c</span> <span class="k">with</span> <span class="nc">Not_found</span> <span class="o">-></span> <span class="o">-</span><span class="mi">1</span>
+
+<span class="c">(* primary</span>
+<span class="c"> *   ::= identifier</span>
+<span class="c"> *   ::= numberexpr</span>
+<span class="c"> *   ::= parenexpr</span>
+<span class="c"> *   ::= ifexpr</span>
+<span class="c"> *   ::= forexpr *)</span>
+<span class="k">let</span> <span class="k">rec</span> <span class="n">parse_primary</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="c">(* numberexpr ::= number *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Number</span> <span class="n">n</span> <span class="o">>]</span> <span class="o">-></span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Number</span> <span class="n">n</span>
+
+  <span class="c">(* parenexpr ::= '(' expression ')' *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">'('</span><span class="o">;</span> <span class="n">e</span><span class="o">=</span><span class="n">parse_expr</span><span class="o">;</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">')'</span> <span class="o">??</span> <span class="s2">"expected ')'"</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">e</span>
+
+  <span class="c">(* identifierexpr</span>
+<span class="c">   *   ::= identifier</span>
+<span class="c">   *   ::= identifier '(' argumentexpr ')' *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Ident</span> <span class="n">id</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="k">let</span> <span class="k">rec</span> <span class="n">parse_args</span> <span class="n">accumulator</span> <span class="o">=</span> <span class="n">parser</span>
+        <span class="o">|</span> <span class="o">[<</span> <span class="n">e</span><span class="o">=</span><span class="n">parse_expr</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span>
+            <span class="k">begin</span> <span class="n">parser</span>
+              <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">','</span><span class="o">;</span> <span class="n">e</span><span class="o">=</span><span class="n">parse_args</span> <span class="o">(</span><span class="n">e</span> <span class="o">::</span> <span class="n">accumulator</span><span class="o">)</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">e</span>
+              <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">e</span> <span class="o">::</span> <span class="n">accumulator</span>
+            <span class="k">end</span> <span class="n">stream</span>
+        <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">accumulator</span>
+      <span class="k">in</span>
+      <span class="k">let</span> <span class="k">rec</span> <span class="n">parse_ident</span> <span class="n">id</span> <span class="o">=</span> <span class="n">parser</span>
+        <span class="c">(* Call. *)</span>
+        <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">'('</span><span class="o">;</span>
+             <span class="n">args</span><span class="o">=</span><span class="n">parse_args</span> <span class="bp">[]</span><span class="o">;</span>
+             <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">')'</span> <span class="o">??</span> <span class="s2">"expected ')'"</span><span class="o">>]</span> <span class="o">-></span>
+            <span class="nn">Ast</span><span class="p">.</span><span class="nc">Call</span> <span class="o">(</span><span class="n">id</span><span class="o">,</span> <span class="nn">Array</span><span class="p">.</span><span class="n">of_list</span> <span class="o">(</span><span class="nn">List</span><span class="p">.</span><span class="n">rev</span> <span class="n">args</span><span class="o">))</span>
+
+        <span class="c">(* Simple variable ref. *)</span>
+        <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Variable</span> <span class="n">id</span>
+      <span class="k">in</span>
+      <span class="n">parse_ident</span> <span class="n">id</span> <span class="n">stream</span>
+
+  <span class="c">(* ifexpr ::= 'if' expr 'then' expr 'else' expr *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">If</span><span class="o">;</span> <span class="n">c</span><span class="o">=</span><span class="n">parse_expr</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Then</span> <span class="o">??</span> <span class="s2">"expected 'then'"</span><span class="o">;</span> <span class="n">t</span><span class="o">=</span><span class="n">parse_expr</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Else</span> <span class="o">??</span> <span class="s2">"expected 'else'"</span><span class="o">;</span> <span class="n">e</span><span class="o">=</span><span class="n">parse_expr</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="nn">Ast</span><span class="p">.</span><span class="nc">If</span> <span class="o">(</span><span class="n">c</span><span class="o">,</span> <span class="n">t</span><span class="o">,</span> <span class="n">e</span><span class="o">)</span>
+
+  <span class="c">(* forexpr</span>
+<span class="c">        ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">For</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Ident</span> <span class="n">id</span> <span class="o">??</span> <span class="s2">"expected identifier after for"</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">'='</span> <span class="o">??</span> <span class="s2">"expected '=' after for"</span><span class="o">;</span>
+       <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="k">begin</span> <span class="n">parser</span>
+        <span class="o">|</span> <span class="o">[<</span>
+             <span class="n">start</span><span class="o">=</span><span class="n">parse_expr</span><span class="o">;</span>
+             <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">','</span> <span class="o">??</span> <span class="s2">"expected ',' after for"</span><span class="o">;</span>
+             <span class="n">end_</span><span class="o">=</span><span class="n">parse_expr</span><span class="o">;</span>
+             <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span>
+            <span class="k">let</span> <span class="n">step</span> <span class="o">=</span>
+              <span class="k">begin</span> <span class="n">parser</span>
+              <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">','</span><span class="o">;</span> <span class="n">step</span><span class="o">=</span><span class="n">parse_expr</span> <span class="o">>]</span> <span class="o">-></span> <span class="nc">Some</span> <span class="n">step</span>
+              <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span> <span class="nc">None</span>
+              <span class="k">end</span> <span class="n">stream</span>
+            <span class="k">in</span>
+            <span class="k">begin</span> <span class="n">parser</span>
+            <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">In</span><span class="o">;</span> <span class="n">body</span><span class="o">=</span><span class="n">parse_expr</span> <span class="o">>]</span> <span class="o">-></span>
+                <span class="nn">Ast</span><span class="p">.</span><span class="nc">For</span> <span class="o">(</span><span class="n">id</span><span class="o">,</span> <span class="n">start</span><span class="o">,</span> <span class="n">end_</span><span class="o">,</span> <span class="n">step</span><span class="o">,</span> <span class="n">body</span><span class="o">)</span>
+            <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span>
+                <span class="k">raise</span> <span class="o">(</span><span class="nn">Stream</span><span class="p">.</span><span class="nc">Error</span> <span class="s2">"expected 'in' after for"</span><span class="o">)</span>
+            <span class="k">end</span> <span class="n">stream</span>
+        <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span>
+            <span class="k">raise</span> <span class="o">(</span><span class="nn">Stream</span><span class="p">.</span><span class="nc">Error</span> <span class="s2">"expected '=' after for"</span><span class="o">)</span>
+      <span class="k">end</span> <span class="n">stream</span>
+
+  <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span> <span class="k">raise</span> <span class="o">(</span><span class="nn">Stream</span><span class="p">.</span><span class="nc">Error</span> <span class="s2">"unknown token when expecting an expression."</span><span class="o">)</span>
+
+<span class="c">(* binoprhs</span>
+<span class="c"> *   ::= ('+' primary)* *)</span>
+<span class="ow">and</span> <span class="n">parse_bin_rhs</span> <span class="n">expr_prec</span> <span class="n">lhs</span> <span class="n">stream</span> <span class="o">=</span>
+  <span class="k">match</span> <span class="nn">Stream</span><span class="p">.</span><span class="n">peek</span> <span class="n">stream</span> <span class="k">with</span>
+  <span class="c">(* If this is a binop, find its precedence. *)</span>
+  <span class="o">|</span> <span class="nc">Some</span> <span class="o">(</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="n">c</span><span class="o">)</span> <span class="k">when</span> <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">mem</span> <span class="n">binop_precedence</span> <span class="n">c</span> <span class="o">-></span>
+      <span class="k">let</span> <span class="n">token_prec</span> <span class="o">=</span> <span class="n">precedence</span> <span class="n">c</span> <span class="k">in</span>
+
+      <span class="c">(* If this is a binop that binds at least as tightly as the current binop,</span>
+<span class="c">       * consume it, otherwise we are done. *)</span>
+      <span class="k">if</span> <span class="n">token_prec</span> <span class="o"><</span> <span class="n">expr_prec</span> <span class="k">then</span> <span class="n">lhs</span> <span class="k">else</span> <span class="k">begin</span>
+        <span class="c">(* Eat the binop. *)</span>
+        <span class="nn">Stream</span><span class="p">.</span><span class="n">junk</span> <span class="n">stream</span><span class="o">;</span>
+
+        <span class="c">(* Parse the primary expression after the binary operator. *)</span>
+        <span class="k">let</span> <span class="n">rhs</span> <span class="o">=</span> <span class="n">parse_primary</span> <span class="n">stream</span> <span class="k">in</span>
+
+        <span class="c">(* Okay, we know this is a binop. *)</span>
+        <span class="k">let</span> <span class="n">rhs</span> <span class="o">=</span>
+          <span class="k">match</span> <span class="nn">Stream</span><span class="p">.</span><span class="n">peek</span> <span class="n">stream</span> <span class="k">with</span>
+          <span class="o">|</span> <span class="nc">Some</span> <span class="o">(</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="n">c2</span><span class="o">)</span> <span class="o">-></span>
+              <span class="c">(* If BinOp binds less tightly with rhs than the operator after</span>
+<span class="c">               * rhs, let the pending operator take rhs as its lhs. *)</span>
+              <span class="k">let</span> <span class="n">next_prec</span> <span class="o">=</span> <span class="n">precedence</span> <span class="n">c2</span> <span class="k">in</span>
+              <span class="k">if</span> <span class="n">token_prec</span> <span class="o"><</span> <span class="n">next_prec</span>
+              <span class="k">then</span> <span class="n">parse_bin_rhs</span> <span class="o">(</span><span class="n">token_prec</span> <span class="o">+</span> <span class="mi">1</span><span class="o">)</span> <span class="n">rhs</span> <span class="n">stream</span>
+              <span class="k">else</span> <span class="n">rhs</span>
+          <span class="o">|</span> <span class="o">_</span> <span class="o">-></span> <span class="n">rhs</span>
+        <span class="k">in</span>
+
+        <span class="c">(* Merge lhs/rhs. *)</span>
+        <span class="k">let</span> <span class="n">lhs</span> <span class="o">=</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Binary</span> <span class="o">(</span><span class="n">c</span><span class="o">,</span> <span class="n">lhs</span><span class="o">,</span> <span class="n">rhs</span><span class="o">)</span> <span class="k">in</span>
+        <span class="n">parse_bin_rhs</span> <span class="n">expr_prec</span> <span class="n">lhs</span> <span class="n">stream</span>
+      <span class="k">end</span>
+  <span class="o">|</span> <span class="o">_</span> <span class="o">-></span> <span class="n">lhs</span>
+
+<span class="c">(* expression</span>
+<span class="c"> *   ::= primary binoprhs *)</span>
+<span class="ow">and</span> <span class="n">parse_expr</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="n">lhs</span><span class="o">=</span><span class="n">parse_primary</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">parse_bin_rhs</span> <span class="mi">0</span> <span class="n">lhs</span> <span class="n">stream</span>
+
+<span class="c">(* prototype</span>
+<span class="c"> *   ::= id '(' id* ')' *)</span>
+<span class="k">let</span> <span class="n">parse_prototype</span> <span class="o">=</span>
+  <span class="k">let</span> <span class="k">rec</span> <span class="n">parse_args</span> <span class="n">accumulator</span> <span class="o">=</span> <span class="n">parser</span>
+    <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Ident</span> <span class="n">id</span><span class="o">;</span> <span class="n">e</span><span class="o">=</span><span class="n">parse_args</span> <span class="o">(</span><span class="n">id</span><span class="o">::</span><span class="n">accumulator</span><span class="o">)</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">e</span>
+    <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">accumulator</span>
+  <span class="k">in</span>
+
+  <span class="n">parser</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Ident</span> <span class="n">id</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">'('</span> <span class="o">??</span> <span class="s2">"expected '(' in prototype"</span><span class="o">;</span>
+       <span class="n">args</span><span class="o">=</span><span class="n">parse_args</span> <span class="bp">[]</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">')'</span> <span class="o">??</span> <span class="s2">"expected ')' in prototype"</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="c">(* success. *)</span>
+      <span class="nn">Ast</span><span class="p">.</span><span class="nc">Prototype</span> <span class="o">(</span><span class="n">id</span><span class="o">,</span> <span class="nn">Array</span><span class="p">.</span><span class="n">of_list</span> <span class="o">(</span><span class="nn">List</span><span class="p">.</span><span class="n">rev</span> <span class="n">args</span><span class="o">))</span>
+
+  <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="k">raise</span> <span class="o">(</span><span class="nn">Stream</span><span class="p">.</span><span class="nc">Error</span> <span class="s2">"expected function name in prototype"</span><span class="o">)</span>
+
+<span class="c">(* definition ::= 'def' prototype expression *)</span>
+<span class="k">let</span> <span class="n">parse_definition</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Def</span><span class="o">;</span> <span class="n">p</span><span class="o">=</span><span class="n">parse_prototype</span><span class="o">;</span> <span class="n">e</span><span class="o">=</span><span class="n">parse_expr</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="nn">Ast</span><span class="p">.</span><span class="nc">Function</span> <span class="o">(</span><span class="n">p</span><span class="o">,</span> <span class="n">e</span><span class="o">)</span>
+
+<span class="c">(* toplevelexpr ::= expression *)</span>
+<span class="k">let</span> <span class="n">parse_toplevel</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="n">e</span><span class="o">=</span><span class="n">parse_expr</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="c">(* Make an anonymous proto. *)</span>
+      <span class="nn">Ast</span><span class="p">.</span><span class="nc">Function</span> <span class="o">(</span><span class="nn">Ast</span><span class="p">.</span><span class="nc">Prototype</span> <span class="o">(</span><span class="s2">""</span><span class="o">,</span> <span class="o">[||]),</span> <span class="n">e</span><span class="o">)</span>
+
+<span class="c">(*  external ::= 'extern' prototype *)</span>
+<span class="k">let</span> <span class="n">parse_extern</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Extern</span><span class="o">;</span> <span class="n">e</span><span class="o">=</span><span class="n">parse_prototype</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">e</span>
+</pre></div>
+</div>
+</dd>
+<dt>codegen.ml:</dt>
+<dd><div class="first last highlight-ocaml"><div class="highlight"><pre><span class="c">(*===----------------------------------------------------------------------===</span>
+<span class="c"> * Code Generation</span>
+<span class="c"> *===----------------------------------------------------------------------===*)</span>
+
+<span class="k">open</span> <span class="nc">Llvm</span>
+
+<span class="k">exception</span> <span class="nc">Error</span> <span class="k">of</span> <span class="kt">string</span>
+
+<span class="k">let</span> <span class="n">context</span> <span class="o">=</span> <span class="n">global_context</span> <span class="bp">()</span>
+<span class="k">let</span> <span class="n">the_module</span> <span class="o">=</span> <span class="n">create_module</span> <span class="n">context</span> <span class="s2">"my cool jit"</span>
+<span class="k">let</span> <span class="n">builder</span> <span class="o">=</span> <span class="n">builder</span> <span class="n">context</span>
+<span class="k">let</span> <span class="n">named_values</span><span class="o">:(</span><span class="kt">string</span><span class="o">,</span> <span class="n">llvalue</span><span class="o">)</span> <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">t</span> <span class="o">=</span> <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">create</span> <span class="mi">10</span>
+<span class="k">let</span> <span class="n">double_type</span> <span class="o">=</span> <span class="n">double_type</span> <span class="n">context</span>
+
+<span class="k">let</span> <span class="k">rec</span> <span class="n">codegen_expr</span> <span class="o">=</span> <span class="k">function</span>
+  <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Number</span> <span class="n">n</span> <span class="o">-></span> <span class="n">const_float</span> <span class="n">double_type</span> <span class="n">n</span>
+  <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Variable</span> <span class="n">name</span> <span class="o">-></span>
+      <span class="o">(</span><span class="k">try</span> <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">find</span> <span class="n">named_values</span> <span class="n">name</span> <span class="k">with</span>
+        <span class="o">|</span> <span class="nc">Not_found</span> <span class="o">-></span> <span class="k">raise</span> <span class="o">(</span><span class="nc">Error</span> <span class="s2">"unknown variable name"</span><span class="o">))</span>
+  <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Binary</span> <span class="o">(</span><span class="n">op</span><span class="o">,</span> <span class="n">lhs</span><span class="o">,</span> <span class="n">rhs</span><span class="o">)</span> <span class="o">-></span>
+      <span class="k">let</span> <span class="n">lhs_val</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">lhs</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">rhs_val</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">rhs</span> <span class="k">in</span>
+      <span class="k">begin</span>
+        <span class="k">match</span> <span class="n">op</span> <span class="k">with</span>
+        <span class="o">|</span> <span class="sc">'+'</span> <span class="o">-></span> <span class="n">build_add</span> <span class="n">lhs_val</span> <span class="n">rhs_val</span> <span class="s2">"addtmp"</span> <span class="n">builder</span>
+        <span class="o">|</span> <span class="sc">'-'</span> <span class="o">-></span> <span class="n">build_sub</span> <span class="n">lhs_val</span> <span class="n">rhs_val</span> <span class="s2">"subtmp"</span> <span class="n">builder</span>
+        <span class="o">|</span> <span class="sc">'*'</span> <span class="o">-></span> <span class="n">build_mul</span> <span class="n">lhs_val</span> <span class="n">rhs_val</span> <span class="s2">"multmp"</span> <span class="n">builder</span>
+        <span class="o">|</span> <span class="sc">'<'</span> <span class="o">-></span>
+            <span class="c">(* Convert bool 0/1 to double 0.0 or 1.0 *)</span>
+            <span class="k">let</span> <span class="n">i</span> <span class="o">=</span> <span class="n">build_fcmp</span> <span class="nn">Fcmp</span><span class="p">.</span><span class="nc">Ult</span> <span class="n">lhs_val</span> <span class="n">rhs_val</span> <span class="s2">"cmptmp"</span> <span class="n">builder</span> <span class="k">in</span>
+            <span class="n">build_uitofp</span> <span class="n">i</span> <span class="n">double_type</span> <span class="s2">"booltmp"</span> <span class="n">builder</span>
+        <span class="o">|</span> <span class="o">_</span> <span class="o">-></span> <span class="k">raise</span> <span class="o">(</span><span class="nc">Error</span> <span class="s2">"invalid binary operator"</span><span class="o">)</span>
+      <span class="k">end</span>
+  <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Call</span> <span class="o">(</span><span class="n">callee</span><span class="o">,</span> <span class="n">args</span><span class="o">)</span> <span class="o">-></span>
+      <span class="c">(* Look up the name in the module table. *)</span>
+      <span class="k">let</span> <span class="n">callee</span> <span class="o">=</span>
+        <span class="k">match</span> <span class="n">lookup_function</span> <span class="n">callee</span> <span class="n">the_module</span> <span class="k">with</span>
+        <span class="o">|</span> <span class="nc">Some</span> <span class="n">callee</span> <span class="o">-></span> <span class="n">callee</span>
+        <span class="o">|</span> <span class="nc">None</span> <span class="o">-></span> <span class="k">raise</span> <span class="o">(</span><span class="nc">Error</span> <span class="s2">"unknown function referenced"</span><span class="o">)</span>
+      <span class="k">in</span>
+      <span class="k">let</span> <span class="n">params</span> <span class="o">=</span> <span class="n">params</span> <span class="n">callee</span> <span class="k">in</span>
+
+      <span class="c">(* If argument mismatch error. *)</span>
+      <span class="k">if</span> <span class="nn">Array</span><span class="p">.</span><span class="n">length</span> <span class="n">params</span> <span class="o">==</span> <span class="nn">Array</span><span class="p">.</span><span class="n">length</span> <span class="n">args</span> <span class="k">then</span> <span class="bp">()</span> <span class="k">else</span>
+        <span class="k">raise</span> <span class="o">(</span><span class="nc">Error</span> <span class="s2">"incorrect # arguments passed"</span><span class="o">);</span>
+      <span class="k">let</span> <span class="n">args</span> <span class="o">=</span> <span class="nn">Array</span><span class="p">.</span><span class="n">map</span> <span class="n">codegen_expr</span> <span class="n">args</span> <span class="k">in</span>
+      <span class="n">build_call</span> <span class="n">callee</span> <span class="n">args</span> <span class="s2">"calltmp"</span> <span class="n">builder</span>
+  <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">If</span> <span class="o">(</span><span class="n">cond</span><span class="o">,</span> <span class="n">then_</span><span class="o">,</span> <span class="n">else_</span><span class="o">)</span> <span class="o">-></span>
+      <span class="k">let</span> <span class="n">cond</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">cond</span> <span class="k">in</span>
+
+      <span class="c">(* Convert condition to a bool by comparing equal to 0.0 *)</span>
+      <span class="k">let</span> <span class="n">zero</span> <span class="o">=</span> <span class="n">const_float</span> <span class="n">double_type</span> <span class="mi">0</span><span class="o">.</span><span class="mi">0</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">cond_val</span> <span class="o">=</span> <span class="n">build_fcmp</span> <span class="nn">Fcmp</span><span class="p">.</span><span class="nc">One</span> <span class="n">cond</span> <span class="n">zero</span> <span class="s2">"ifcond"</span> <span class="n">builder</span> <span class="k">in</span>
+
+      <span class="c">(* Grab the first block so that we might later add the conditional branch</span>
+<span class="c">       * to it at the end of the function. *)</span>
+      <span class="k">let</span> <span class="n">start_bb</span> <span class="o">=</span> <span class="n">insertion_block</span> <span class="n">builder</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">the_function</span> <span class="o">=</span> <span class="n">block_parent</span> <span class="n">start_bb</span> <span class="k">in</span>
+
+      <span class="k">let</span> <span class="n">then_bb</span> <span class="o">=</span> <span class="n">append_block</span> <span class="n">context</span> <span class="s2">"then"</span> <span class="n">the_function</span> <span class="k">in</span>
+
+      <span class="c">(* Emit 'then' value. *)</span>
+      <span class="n">position_at_end</span> <span class="n">then_bb</span> <span class="n">builder</span><span class="o">;</span>
+      <span class="k">let</span> <span class="n">then_val</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">then_</span> <span class="k">in</span>
+
+      <span class="c">(* Codegen of 'then' can change the current block, update then_bb for the</span>
+<span class="c">       * phi. We create a new name because one is used for the phi node, and the</span>
+<span class="c">       * other is used for the conditional branch. *)</span>
+      <span class="k">let</span> <span class="n">new_then_bb</span> <span class="o">=</span> <span class="n">insertion_block</span> <span class="n">builder</span> <span class="k">in</span>
+
+      <span class="c">(* Emit 'else' value. *)</span>
+      <span class="k">let</span> <span class="n">else_bb</span> <span class="o">=</span> <span class="n">append_block</span> <span class="n">context</span> <span class="s2">"else"</span> <span class="n">the_function</span> <span class="k">in</span>
+      <span class="n">position_at_end</span> <span class="n">else_bb</span> <span class="n">builder</span><span class="o">;</span>
+      <span class="k">let</span> <span class="n">else_val</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">else_</span> <span class="k">in</span>
+
+      <span class="c">(* Codegen of 'else' can change the current block, update else_bb for the</span>
+<span class="c">       * phi. *)</span>
+      <span class="k">let</span> <span class="n">new_else_bb</span> <span class="o">=</span> <span class="n">insertion_block</span> <span class="n">builder</span> <span class="k">in</span>
+
+      <span class="c">(* Emit merge block. *)</span>
+      <span class="k">let</span> <span class="n">merge_bb</span> <span class="o">=</span> <span class="n">append_block</span> <span class="n">context</span> <span class="s2">"ifcont"</span> <span class="n">the_function</span> <span class="k">in</span>
+      <span class="n">position_at_end</span> <span class="n">merge_bb</span> <span class="n">builder</span><span class="o">;</span>
+      <span class="k">let</span> <span class="n">incoming</span> <span class="o">=</span> <span class="o">[(</span><span class="n">then_val</span><span class="o">,</span> <span class="n">new_then_bb</span><span class="o">);</span> <span class="o">(</span><span class="n">else_val</span><span class="o">,</span> <span class="n">new_else_bb</span><span class="o">)]</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">phi</span> <span class="o">=</span> <span class="n">build_phi</span> <span class="n">incoming</span> <span class="s2">"iftmp"</span> <span class="n">builder</span> <span class="k">in</span>
+
+      <span class="c">(* Return to the start block to add the conditional branch. *)</span>
+      <span class="n">position_at_end</span> <span class="n">start_bb</span> <span class="n">builder</span><span class="o">;</span>
+      <span class="n">ignore</span> <span class="o">(</span><span class="n">build_cond_br</span> <span class="n">cond_val</span> <span class="n">then_bb</span> <span class="n">else_bb</span> <span class="n">builder</span><span class="o">);</span>
+
+      <span class="c">(* Set a unconditional branch at the end of the 'then' block and the</span>
+<span class="c">       * 'else' block to the 'merge' block. *)</span>
+      <span class="n">position_at_end</span> <span class="n">new_then_bb</span> <span class="n">builder</span><span class="o">;</span> <span class="n">ignore</span> <span class="o">(</span><span class="n">build_br</span> <span class="n">merge_bb</span> <span class="n">builder</span><span class="o">);</span>
+      <span class="n">position_at_end</span> <span class="n">new_else_bb</span> <span class="n">builder</span><span class="o">;</span> <span class="n">ignore</span> <span class="o">(</span><span class="n">build_br</span> <span class="n">merge_bb</span> <span class="n">builder</span><span class="o">);</span>
+
+      <span class="c">(* Finally, set the builder to the end of the merge block. *)</span>
+      <span class="n">position_at_end</span> <span class="n">merge_bb</span> <span class="n">builder</span><span class="o">;</span>
+
+      <span class="n">phi</span>
+  <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">For</span> <span class="o">(</span><span class="n">var_name</span><span class="o">,</span> <span class="n">start</span><span class="o">,</span> <span class="n">end_</span><span class="o">,</span> <span class="n">step</span><span class="o">,</span> <span class="n">body</span><span class="o">)</span> <span class="o">-></span>
+      <span class="c">(* Emit the start code first, without 'variable' in scope. *)</span>
+      <span class="k">let</span> <span class="n">start_val</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">start</span> <span class="k">in</span>
+
+      <span class="c">(* Make the new basic block for the loop header, inserting after current</span>
+<span class="c">       * block. *)</span>
+      <span class="k">let</span> <span class="n">preheader_bb</span> <span class="o">=</span> <span class="n">insertion_block</span> <span class="n">builder</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">the_function</span> <span class="o">=</span> <span class="n">block_parent</span> <span class="n">preheader_bb</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">loop_bb</span> <span class="o">=</span> <span class="n">append_block</span> <span class="n">context</span> <span class="s2">"loop"</span> <span class="n">the_function</span> <span class="k">in</span>
+
+      <span class="c">(* Insert an explicit fall through from the current block to the</span>
+<span class="c">       * loop_bb. *)</span>
+      <span class="n">ignore</span> <span class="o">(</span><span class="n">build_br</span> <span class="n">loop_bb</span> <span class="n">builder</span><span class="o">);</span>
+
+      <span class="c">(* Start insertion in loop_bb. *)</span>
+      <span class="n">position_at_end</span> <span class="n">loop_bb</span> <span class="n">builder</span><span class="o">;</span>
+
+      <span class="c">(* Start the PHI node with an entry for start. *)</span>
+      <span class="k">let</span> <span class="n">variable</span> <span class="o">=</span> <span class="n">build_phi</span> <span class="o">[(</span><span class="n">start_val</span><span class="o">,</span> <span class="n">preheader_bb</span><span class="o">)]</span> <span class="n">var_name</span> <span class="n">builder</span> <span class="k">in</span>
+
+      <span class="c">(* Within the loop, the variable is defined equal to the PHI node. If it</span>
+<span class="c">       * shadows an existing variable, we have to restore it, so save it</span>
+<span class="c">       * now. *)</span>
+      <span class="k">let</span> <span class="n">old_val</span> <span class="o">=</span>
+        <span class="k">try</span> <span class="nc">Some</span> <span class="o">(</span><span class="nn">Hashtbl</span><span class="p">.</span><span class="n">find</span> <span class="n">named_values</span> <span class="n">var_name</span><span class="o">)</span> <span class="k">with</span> <span class="nc">Not_found</span> <span class="o">-></span> <span class="nc">None</span>
+      <span class="k">in</span>
+      <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">add</span> <span class="n">named_values</span> <span class="n">var_name</span> <span class="n">variable</span><span class="o">;</span>
+
+      <span class="c">(* Emit the body of the loop.  This, like any other expr, can change the</span>
+<span class="c">       * current BB.  Note that we ignore the value computed by the body, but</span>
+<span class="c">       * don't allow an error *)</span>
+      <span class="n">ignore</span> <span class="o">(</span><span class="n">codegen_expr</span> <span class="n">body</span><span class="o">);</span>
+
+      <span class="c">(* Emit the step value. *)</span>
+      <span class="k">let</span> <span class="n">step_val</span> <span class="o">=</span>
+        <span class="k">match</span> <span class="n">step</span> <span class="k">with</span>
+        <span class="o">|</span> <span class="nc">Some</span> <span class="n">step</span> <span class="o">-></span> <span class="n">codegen_expr</span> <span class="n">step</span>
+        <span class="c">(* If not specified, use 1.0. *)</span>
+        <span class="o">|</span> <span class="nc">None</span> <span class="o">-></span> <span class="n">const_float</span> <span class="n">double_type</span> <span class="mi">1</span><span class="o">.</span><span class="mi">0</span>
+      <span class="k">in</span>
+
+      <span class="k">let</span> <span class="n">next_var</span> <span class="o">=</span> <span class="n">build_add</span> <span class="n">variable</span> <span class="n">step_val</span> <span class="s2">"nextvar"</span> <span class="n">builder</span> <span class="k">in</span>
+
+      <span class="c">(* Compute the end condition. *)</span>
+      <span class="k">let</span> <span class="n">end_cond</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">end_</span> <span class="k">in</span>
+
+      <span class="c">(* Convert condition to a bool by comparing equal to 0.0. *)</span>
+      <span class="k">let</span> <span class="n">zero</span> <span class="o">=</span> <span class="n">const_float</span> <span class="n">double_type</span> <span class="mi">0</span><span class="o">.</span><span class="mi">0</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">end_cond</span> <span class="o">=</span> <span class="n">build_fcmp</span> <span class="nn">Fcmp</span><span class="p">.</span><span class="nc">One</span> <span class="n">end_cond</span> <span class="n">zero</span> <span class="s2">"loopcond"</span> <span class="n">builder</span> <span class="k">in</span>
+
+      <span class="c">(* Create the "after loop" block and insert it. *)</span>
+      <span class="k">let</span> <span class="n">loop_end_bb</span> <span class="o">=</span> <span class="n">insertion_block</span> <span class="n">builder</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">after_bb</span> <span class="o">=</span> <span class="n">append_block</span> <span class="n">context</span> <span class="s2">"afterloop"</span> <span class="n">the_function</span> <span class="k">in</span>
+
+      <span class="c">(* Insert the conditional branch into the end of loop_end_bb. *)</span>
+      <span class="n">ignore</span> <span class="o">(</span><span class="n">build_cond_br</span> <span class="n">end_cond</span> <span class="n">loop_bb</span> <span class="n">after_bb</span> <span class="n">builder</span><span class="o">);</span>
+
+      <span class="c">(* Any new code will be inserted in after_bb. *)</span>
+      <span class="n">position_at_end</span> <span class="n">after_bb</span> <span class="n">builder</span><span class="o">;</span>
+
+      <span class="c">(* Add a new entry to the PHI node for the backedge. *)</span>
+      <span class="n">add_incoming</span> <span class="o">(</span><span class="n">next_var</span><span class="o">,</span> <span class="n">loop_end_bb</span><span class="o">)</span> <span class="n">variable</span><span class="o">;</span>
+
+      <span class="c">(* Restore the unshadowed variable. *)</span>
+      <span class="k">begin</span> <span class="k">match</span> <span class="n">old_val</span> <span class="k">with</span>
+      <span class="o">|</span> <span class="nc">Some</span> <span class="n">old_val</span> <span class="o">-></span> <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">add</span> <span class="n">named_values</span> <span class="n">var_name</span> <span class="n">old_val</span>
+      <span class="o">|</span> <span class="nc">None</span> <span class="o">-></span> <span class="bp">()</span>
+      <span class="k">end</span><span class="o">;</span>
+
+      <span class="c">(* for expr always returns 0.0. *)</span>
+      <span class="n">const_null</span> <span class="n">double_type</span>
+
+<span class="k">let</span> <span class="n">codegen_proto</span> <span class="o">=</span> <span class="k">function</span>
+  <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Prototype</span> <span class="o">(</span><span class="n">name</span><span class="o">,</span> <span class="n">args</span><span class="o">)</span> <span class="o">-></span>
+      <span class="c">(* Make the function type: double(double,double) etc. *)</span>
+      <span class="k">let</span> <span class="n">doubles</span> <span class="o">=</span> <span class="nn">Array</span><span class="p">.</span><span class="n">make</span> <span class="o">(</span><span class="nn">Array</span><span class="p">.</span><span class="n">length</span> <span class="n">args</span><span class="o">)</span> <span class="n">double_type</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">ft</span> <span class="o">=</span> <span class="n">function_type</span> <span class="n">double_type</span> <span class="n">doubles</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">f</span> <span class="o">=</span>
+        <span class="k">match</span> <span class="n">lookup_function</span> <span class="n">name</span> <span class="n">the_module</span> <span class="k">with</span>
+        <span class="o">|</span> <span class="nc">None</span> <span class="o">-></span> <span class="n">declare_function</span> <span class="n">name</span> <span class="n">ft</span> <span class="n">the_module</span>
+
+        <span class="c">(* If 'f' conflicted, there was already something named 'name'. If it</span>
+<span class="c">         * has a body, don't allow redefinition or reextern. *)</span>
+        <span class="o">|</span> <span class="nc">Some</span> <span class="n">f</span> <span class="o">-></span>
+            <span class="c">(* If 'f' already has a body, reject this. *)</span>
+            <span class="k">if</span> <span class="n">block_begin</span> <span class="n">f</span> <span class="o"><></span> <span class="nc">At_end</span> <span class="n">f</span> <span class="k">then</span>
+              <span class="k">raise</span> <span class="o">(</span><span class="nc">Error</span> <span class="s2">"redefinition of function"</span><span class="o">);</span>
+
+            <span class="c">(* If 'f' took a different number of arguments, reject. *)</span>
+            <span class="k">if</span> <span class="n">element_type</span> <span class="o">(</span><span class="n">type_of</span> <span class="n">f</span><span class="o">)</span> <span class="o"><></span> <span class="n">ft</span> <span class="k">then</span>
+              <span class="k">raise</span> <span class="o">(</span><span class="nc">Error</span> <span class="s2">"redefinition of function with different # args"</span><span class="o">);</span>
+            <span class="n">f</span>
+      <span class="k">in</span>
+
+      <span class="c">(* Set names for all arguments. *)</span>
+      <span class="nn">Array</span><span class="p">.</span><span class="n">iteri</span> <span class="o">(</span><span class="k">fun</span> <span class="n">i</span> <span class="n">a</span> <span class="o">-></span>
+        <span class="k">let</span> <span class="n">n</span> <span class="o">=</span> <span class="n">args</span><span class="o">.(</span><span class="n">i</span><span class="o">)</span> <span class="k">in</span>
+        <span class="n">set_value_name</span> <span class="n">n</span> <span class="n">a</span><span class="o">;</span>
+        <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">add</span> <span class="n">named_values</span> <span class="n">n</span> <span class="n">a</span><span class="o">;</span>
+      <span class="o">)</span> <span class="o">(</span><span class="n">params</span> <span class="n">f</span><span class="o">);</span>
+      <span class="n">f</span>
+
+<span class="k">let</span> <span class="n">codegen_func</span> <span class="n">the_fpm</span> <span class="o">=</span> <span class="k">function</span>
+  <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Function</span> <span class="o">(</span><span class="n">proto</span><span class="o">,</span> <span class="n">body</span><span class="o">)</span> <span class="o">-></span>
+      <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">clear</span> <span class="n">named_values</span><span class="o">;</span>
+      <span class="k">let</span> <span class="n">the_function</span> <span class="o">=</span> <span class="n">codegen_proto</span> <span class="n">proto</span> <span class="k">in</span>
+
+      <span class="c">(* Create a new basic block to start insertion into. *)</span>
+      <span class="k">let</span> <span class="n">bb</span> <span class="o">=</span> <span class="n">append_block</span> <span class="n">context</span> <span class="s2">"entry"</span> <span class="n">the_function</span> <span class="k">in</span>
+      <span class="n">position_at_end</span> <span class="n">bb</span> <span class="n">builder</span><span class="o">;</span>
+
+      <span class="k">try</span>
+        <span class="k">let</span> <span class="n">ret_val</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">body</span> <span class="k">in</span>
+
+        <span class="c">(* Finish off the function. *)</span>
+        <span class="k">let</span> <span class="o">_</span> <span class="o">=</span> <span class="n">build_ret</span> <span class="n">ret_val</span> <span class="n">builder</span> <span class="k">in</span>
+
+        <span class="c">(* Validate the generated code, checking for consistency. *)</span>
+        <span class="nn">Llvm_analysis</span><span class="p">.</span><span class="n">assert_valid_function</span> <span class="n">the_function</span><span class="o">;</span>
+
+        <span class="c">(* Optimize the function. *)</span>
+        <span class="k">let</span> <span class="o">_</span> <span class="o">=</span> <span class="nn">PassManager</span><span class="p">.</span><span class="n">run_function</span> <span class="n">the_function</span> <span class="n">the_fpm</span> <span class="k">in</span>
+
+        <span class="n">the_function</span>
+      <span class="k">with</span> <span class="n">e</span> <span class="o">-></span>
+        <span class="n">delete_function</span> <span class="n">the_function</span><span class="o">;</span>
+        <span class="k">raise</span> <span class="n">e</span>
+</pre></div>
+</div>
+</dd>
+<dt>toplevel.ml:</dt>
+<dd><div class="first last highlight-ocaml"><div class="highlight"><pre><span class="c">(*===----------------------------------------------------------------------===</span>
+<span class="c"> * Top-Level parsing and JIT Driver</span>
+<span class="c"> *===----------------------------------------------------------------------===*)</span>
+
+<span class="k">open</span> <span class="nc">Llvm</span>
+<span class="k">open</span> <span class="nc">Llvm_executionengine</span>
+
+<span class="c">(* top ::= definition | external | expression | ';' *)</span>
+<span class="k">let</span> <span class="k">rec</span> <span class="n">main_loop</span> <span class="n">the_fpm</span> <span class="n">the_execution_engine</span> <span class="n">stream</span> <span class="o">=</span>
+  <span class="k">match</span> <span class="nn">Stream</span><span class="p">.</span><span class="n">peek</span> <span class="n">stream</span> <span class="k">with</span>
+  <span class="o">|</span> <span class="nc">None</span> <span class="o">-></span> <span class="bp">()</span>
+
+  <span class="c">(* ignore top-level semicolons. *)</span>
+  <span class="o">|</span> <span class="nc">Some</span> <span class="o">(</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">';'</span><span class="o">)</span> <span class="o">-></span>
+      <span class="nn">Stream</span><span class="p">.</span><span class="n">junk</span> <span class="n">stream</span><span class="o">;</span>
+      <span class="n">main_loop</span> <span class="n">the_fpm</span> <span class="n">the_execution_engine</span> <span class="n">stream</span>
+
+  <span class="o">|</span> <span class="nc">Some</span> <span class="n">token</span> <span class="o">-></span>
+      <span class="k">begin</span>
+        <span class="k">try</span> <span class="k">match</span> <span class="n">token</span> <span class="k">with</span>
+        <span class="o">|</span> <span class="nn">Token</span><span class="p">.</span><span class="nc">Def</span> <span class="o">-></span>
+            <span class="k">let</span> <span class="n">e</span> <span class="o">=</span> <span class="nn">Parser</span><span class="p">.</span><span class="n">parse_definition</span> <span class="n">stream</span> <span class="k">in</span>
+            <span class="n">print_endline</span> <span class="s2">"parsed a function definition."</span><span class="o">;</span>
+            <span class="n">dump_value</span> <span class="o">(</span><span class="nn">Codegen</span><span class="p">.</span><span class="n">codegen_func</span> <span class="n">the_fpm</span> <span class="n">e</span><span class="o">);</span>
+        <span class="o">|</span> <span class="nn">Token</span><span class="p">.</span><span class="nc">Extern</span> <span class="o">-></span>
+            <span class="k">let</span> <span class="n">e</span> <span class="o">=</span> <span class="nn">Parser</span><span class="p">.</span><span class="n">parse_extern</span> <span class="n">stream</span> <span class="k">in</span>
+            <span class="n">print_endline</span> <span class="s2">"parsed an extern."</span><span class="o">;</span>
+            <span class="n">dump_value</span> <span class="o">(</span><span class="nn">Codegen</span><span class="p">.</span><span class="n">codegen_proto</span> <span class="n">e</span><span class="o">);</span>
+        <span class="o">|</span> <span class="o">_</span> <span class="o">-></span>
+            <span class="c">(* Evaluate a top-level expression into an anonymous function. *)</span>
+            <span class="k">let</span> <span class="n">e</span> <span class="o">=</span> <span class="nn">Parser</span><span class="p">.</span><span class="n">parse_toplevel</span> <span class="n">stream</span> <span class="k">in</span>
+            <span class="n">print_endline</span> <span class="s2">"parsed a top-level expr"</span><span class="o">;</span>
+            <span class="k">let</span> <span class="n">the_function</span> <span class="o">=</span> <span class="nn">Codegen</span><span class="p">.</span><span class="n">codegen_func</span> <span class="n">the_fpm</span> <span class="n">e</span> <span class="k">in</span>
+            <span class="n">dump_value</span> <span class="n">the_function</span><span class="o">;</span>
+
+            <span class="c">(* JIT the function, returning a function pointer. *)</span>
+            <span class="k">let</span> <span class="n">result</span> <span class="o">=</span> <span class="nn">ExecutionEngine</span><span class="p">.</span><span class="n">run_function</span> <span class="n">the_function</span> <span class="o">[||]</span>
+              <span class="n">the_execution_engine</span> <span class="k">in</span>
+
+            <span class="n">print_string</span> <span class="s2">"Evaluated to "</span><span class="o">;</span>
+            <span class="n">print_float</span> <span class="o">(</span><span class="nn">GenericValue</span><span class="p">.</span><span class="n">as_float</span> <span class="nn">Codegen</span><span class="p">.</span><span class="n">double_type</span> <span class="n">result</span><span class="o">);</span>
+            <span class="n">print_newline</span> <span class="bp">()</span><span class="o">;</span>
+        <span class="k">with</span> <span class="nn">Stream</span><span class="p">.</span><span class="nc">Error</span> <span class="n">s</span> <span class="o">|</span> <span class="nn">Codegen</span><span class="p">.</span><span class="nc">Error</span> <span class="n">s</span> <span class="o">-></span>
+          <span class="c">(* Skip token for error recovery. *)</span>
+          <span class="nn">Stream</span><span class="p">.</span><span class="n">junk</span> <span class="n">stream</span><span class="o">;</span>
+          <span class="n">print_endline</span> <span class="n">s</span><span class="o">;</span>
+      <span class="k">end</span><span class="o">;</span>
+      <span class="n">print_string</span> <span class="s2">"ready> "</span><span class="o">;</span> <span class="n">flush</span> <span class="n">stdout</span><span class="o">;</span>
+      <span class="n">main_loop</span> <span class="n">the_fpm</span> <span class="n">the_execution_engine</span> <span class="n">stream</span>
+</pre></div>
+</div>
+</dd>
+<dt>toy.ml:</dt>
+<dd><div class="first last highlight-ocaml"><div class="highlight"><pre><span class="c">(*===----------------------------------------------------------------------===</span>
+<span class="c"> * Main driver code.</span>
+<span class="c"> *===----------------------------------------------------------------------===*)</span>
+
+<span class="k">open</span> <span class="nc">Llvm</span>
+<span class="k">open</span> <span class="nc">Llvm_executionengine</span>
+<span class="k">open</span> <span class="nc">Llvm_target</span>
+<span class="k">open</span> <span class="nc">Llvm_scalar_opts</span>
+
+<span class="k">let</span> <span class="n">main</span> <span class="bp">()</span> <span class="o">=</span>
+  <span class="n">ignore</span> <span class="o">(</span><span class="n">initialize_native_target</span> <span class="bp">()</span><span class="o">);</span>
+
+  <span class="c">(* Install standard binary operators.</span>
+<span class="c">   * 1 is the lowest precedence. *)</span>
+  <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">add</span> <span class="nn">Parser</span><span class="p">.</span><span class="n">binop_precedence</span> <span class="sc">'<'</span> <span class="mi">10</span><span class="o">;</span>
+  <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">add</span> <span class="nn">Parser</span><span class="p">.</span><span class="n">binop_precedence</span> <span class="sc">'+'</span> <span class="mi">20</span><span class="o">;</span>
+  <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">add</span> <span class="nn">Parser</span><span class="p">.</span><span class="n">binop_precedence</span> <span class="sc">'-'</span> <span class="mi">20</span><span class="o">;</span>
+  <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">add</span> <span class="nn">Parser</span><span class="p">.</span><span class="n">binop_precedence</span> <span class="sc">'*'</span> <span class="mi">40</span><span class="o">;</span>    <span class="c">(* highest. *)</span>
+
+  <span class="c">(* Prime the first token. *)</span>
+  <span class="n">print_string</span> <span class="s2">"ready> "</span><span class="o">;</span> <span class="n">flush</span> <span class="n">stdout</span><span class="o">;</span>
+  <span class="k">let</span> <span class="n">stream</span> <span class="o">=</span> <span class="nn">Lexer</span><span class="p">.</span><span class="n">lex</span> <span class="o">(</span><span class="nn">Stream</span><span class="p">.</span><span class="n">of_channel</span> <span class="n">stdin</span><span class="o">)</span> <span class="k">in</span>
+
+  <span class="c">(* Create the JIT. *)</span>
+  <span class="k">let</span> <span class="n">the_execution_engine</span> <span class="o">=</span> <span class="nn">ExecutionEngine</span><span class="p">.</span><span class="n">create</span> <span class="nn">Codegen</span><span class="p">.</span><span class="n">the_module</span> <span class="k">in</span>
+  <span class="k">let</span> <span class="n">the_fpm</span> <span class="o">=</span> <span class="nn">PassManager</span><span class="p">.</span><span class="n">create_function</span> <span class="nn">Codegen</span><span class="p">.</span><span class="n">the_module</span> <span class="k">in</span>
+
+  <span class="c">(* Set up the optimizer pipeline.  Start with registering info about how the</span>
+<span class="c">   * target lays out data structures. *)</span>
+  <span class="nn">DataLayout</span><span class="p">.</span><span class="n">add</span> <span class="o">(</span><span class="nn">ExecutionEngine</span><span class="p">.</span><span class="n">target_data</span> <span class="n">the_execution_engine</span><span class="o">)</span> <span class="n">the_fpm</span><span class="o">;</span>
+
+  <span class="c">(* Do simple "peephole" optimizations and bit-twiddling optzn. *)</span>
+  <span class="n">add_instruction_combination</span> <span class="n">the_fpm</span><span class="o">;</span>
+
+  <span class="c">(* reassociate expressions. *)</span>
+  <span class="n">add_reassociation</span> <span class="n">the_fpm</span><span class="o">;</span>
+
+  <span class="c">(* Eliminate Common SubExpressions. *)</span>
+  <span class="n">add_gvn</span> <span class="n">the_fpm</span><span class="o">;</span>
+
+  <span class="c">(* Simplify the control flow graph (deleting unreachable blocks, etc). *)</span>
+  <span class="n">add_cfg_simplification</span> <span class="n">the_fpm</span><span class="o">;</span>
+
+  <span class="n">ignore</span> <span class="o">(</span><span class="nn">PassManager</span><span class="p">.</span><span class="n">initialize</span> <span class="n">the_fpm</span><span class="o">);</span>
+
+  <span class="c">(* Run the main "interpreter loop" now. *)</span>
+  <span class="nn">Toplevel</span><span class="p">.</span><span class="n">main_loop</span> <span class="n">the_fpm</span> <span class="n">the_execution_engine</span> <span class="n">stream</span><span class="o">;</span>
+
+  <span class="c">(* Print out all the generated code. *)</span>
+  <span class="n">dump_module</span> <span class="nn">Codegen</span><span class="p">.</span><span class="n">the_module</span>
+<span class="o">;;</span>
+
+<span class="n">main</span> <span class="bp">()</span>
+</pre></div>
+</div>
+</dd>
+<dt>bindings.c</dt>
+<dd><div class="first last highlight-c"><div class="highlight"><pre><span class="cp">#include <stdio.h></span>
+
+<span class="cm">/* putchard - putchar that takes a double and returns 0. */</span>
+<span class="k">extern</span> <span class="kt">double</span> <span class="nf">putchard</span><span class="p">(</span><span class="kt">double</span> <span class="n">X</span><span class="p">)</span> <span class="p">{</span>
+  <span class="n">putchar</span><span class="p">((</span><span class="kt">char</span><span class="p">)</span><span class="n">X</span><span class="p">);</span>
+  <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
+<span class="p">}</span>
+</pre></div>
+</div>
+</dd>
+</dl>
+<p><a class="reference external" href="OCamlLangImpl6.html">Next: Extending the language: user-defined
+operators</a></p>
+</div>
+</div>
+
+
+          </div>
+      </div>
+      <div class="clearer"></div>
+    </div>
+    <div class="related">
+      <h3>Navigation</h3>
+      <ul>
+        <li class="right" style="margin-right: 10px">
+          <a href="../genindex.html" title="General Index"
+             >index</a></li>
+        <li class="right" >
+          <a href="OCamlLangImpl6.html" title="6. Kaleidoscope: Extending the Language: User-defined Operators"
+             >next</a> |</li>
+        <li class="right" >
+          <a href="OCamlLangImpl4.html" title="4. Kaleidoscope: Adding JIT and Optimizer Support"
+             >previous</a> |</li>
+  <li><a href="http://llvm.org/">LLVM Home</a> | </li>
+  <li><a href="../index.html">Documentation</a>»</li>
+
+          <li><a href="index.html" >LLVM Tutorial: Table of Contents</a> »</li> 
+      </ul>
+    </div>
+    <div class="footer">
+        © Copyright 2003-2016, LLVM Project.
+      Last updated on 2016-01-04.
+      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.3.
+    </div>
+  </body>
+</html>
\ No newline at end of file

Added: www-releases/trunk/3.7.1/docs/tutorial/OCamlLangImpl6.html
URL: http://llvm.org/viewvc/llvm-project/www-releases/trunk/3.7.1/docs/tutorial/OCamlLangImpl6.html?rev=257950&view=auto
==============================================================================
--- www-releases/trunk/3.7.1/docs/tutorial/OCamlLangImpl6.html (added)
+++ www-releases/trunk/3.7.1/docs/tutorial/OCamlLangImpl6.html Fri Jan 15 17:13:16 2016
@@ -0,0 +1,1492 @@
+
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+
+
+<html xmlns="http://www.w3.org/1999/xhtml">
+  <head>
+    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+    
+    <title>6. Kaleidoscope: Extending the Language: User-defined Operators — LLVM 3.7 documentation</title>
+    
+    <link rel="stylesheet" href="../_static/llvm-theme.css" type="text/css" />
+    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
+    
+    <script type="text/javascript">
+      var DOCUMENTATION_OPTIONS = {
+        URL_ROOT:    '../',
+        VERSION:     '3.7',
+        COLLAPSE_INDEX: false,
+        FILE_SUFFIX: '.html',
+        HAS_SOURCE:  true
+      };
+    </script>
+    <script type="text/javascript" src="../_static/jquery.js"></script>
+    <script type="text/javascript" src="../_static/underscore.js"></script>
+    <script type="text/javascript" src="../_static/doctools.js"></script>
+    <link rel="top" title="LLVM 3.7 documentation" href="../index.html" />
+    <link rel="up" title="LLVM Tutorial: Table of Contents" href="index.html" />
+    <link rel="next" title="7. Kaleidoscope: Extending the Language: Mutable Variables" href="OCamlLangImpl7.html" />
+    <link rel="prev" title="5. Kaleidoscope: Extending the Language: Control Flow" href="OCamlLangImpl5.html" />
+<style type="text/css">
+  table.right { float: right; margin-left: 20px; }
+  table.right td { border: 1px solid #ccc; }
+</style>
+
+  </head>
+  <body>
+<div class="logo">
+  <a href="../index.html">
+    <img src="../_static/logo.png"
+         alt="LLVM Logo" width="250" height="88"/></a>
+</div>
+
+    <div class="related">
+      <h3>Navigation</h3>
+      <ul>
+        <li class="right" style="margin-right: 10px">
+          <a href="../genindex.html" title="General Index"
+             accesskey="I">index</a></li>
+        <li class="right" >
+          <a href="OCamlLangImpl7.html" title="7. Kaleidoscope: Extending the Language: Mutable Variables"
+             accesskey="N">next</a> |</li>
+        <li class="right" >
+          <a href="OCamlLangImpl5.html" title="5. Kaleidoscope: Extending the Language: Control Flow"
+             accesskey="P">previous</a> |</li>
+  <li><a href="http://llvm.org/">LLVM Home</a> | </li>
+  <li><a href="../index.html">Documentation</a>»</li>
+
+          <li><a href="index.html" accesskey="U">LLVM Tutorial: Table of Contents</a> »</li> 
+      </ul>
+    </div>
+
+
+    <div class="document">
+      <div class="documentwrapper">
+          <div class="body">
+            
+  <div class="section" id="kaleidoscope-extending-the-language-user-defined-operators">
+<h1>6. Kaleidoscope: Extending the Language: User-defined Operators<a class="headerlink" href="#kaleidoscope-extending-the-language-user-defined-operators" title="Permalink to this headline">¶</a></h1>
+<div class="contents local topic" id="contents">
+<ul class="simple">
+<li><a class="reference internal" href="#chapter-6-introduction" id="id1">Chapter 6 Introduction</a></li>
+<li><a class="reference internal" href="#user-defined-operators-the-idea" id="id2">User-defined Operators: the Idea</a></li>
+<li><a class="reference internal" href="#user-defined-binary-operators" id="id3">User-defined Binary Operators</a></li>
+<li><a class="reference internal" href="#user-defined-unary-operators" id="id4">User-defined Unary Operators</a></li>
+<li><a class="reference internal" href="#kicking-the-tires" id="id5">Kicking the Tires</a></li>
+<li><a class="reference internal" href="#full-code-listing" id="id6">Full Code Listing</a></li>
+</ul>
+</div>
+<div class="section" id="chapter-6-introduction">
+<h2><a class="toc-backref" href="#id1">6.1. Chapter 6 Introduction</a><a class="headerlink" href="#chapter-6-introduction" title="Permalink to this headline">¶</a></h2>
+<p>Welcome to Chapter 6 of the “<a class="reference external" href="index.html">Implementing a language with
+LLVM</a>” tutorial. At this point in our tutorial, we now
+have a fully functional language that is fairly minimal, but also
+useful. There is still one big problem with it, however. Our language
+doesn’t have many useful operators (like division, logical negation, or
+even any comparisons besides less-than).</p>
+<p>This chapter of the tutorial takes a wild digression into adding
+user-defined operators to the simple and beautiful Kaleidoscope
+language. This digression now gives us a simple and ugly language in
+some ways, but also a powerful one at the same time. One of the great
+things about creating your own language is that you get to decide what
+is good or bad. In this tutorial we’ll assume that it is okay to use
+this as a way to show some interesting parsing techniques.</p>
+<p>At the end of this tutorial, we’ll run through an example Kaleidoscope
+application that <a class="reference external" href="#example">renders the Mandelbrot set</a>. This gives an
+example of what you can build with Kaleidoscope and its feature set.</p>
+</div>
+<div class="section" id="user-defined-operators-the-idea">
+<h2><a class="toc-backref" href="#id2">6.2. User-defined Operators: the Idea</a><a class="headerlink" href="#user-defined-operators-the-idea" title="Permalink to this headline">¶</a></h2>
+<p>The “operator overloading” that we will add to Kaleidoscope is more
+general than languages like C++. In C++, you are only allowed to
+redefine existing operators: you can’t programatically change the
+grammar, introduce new operators, change precedence levels, etc. In this
+chapter, we will add this capability to Kaleidoscope, which will let the
+user round out the set of operators that are supported.</p>
+<p>The point of going into user-defined operators in a tutorial like this
+is to show the power and flexibility of using a hand-written parser.
+Thus far, the parser we have been implementing uses recursive descent
+for most parts of the grammar and operator precedence parsing for the
+expressions. See <a class="reference external" href="OCamlLangImpl2.html">Chapter 2</a> for details. Without
+using operator precedence parsing, it would be very difficult to allow
+the programmer to introduce new operators into the grammar: the grammar
+is dynamically extensible as the JIT runs.</p>
+<p>The two specific features we’ll add are programmable unary operators
+(right now, Kaleidoscope has no unary operators at all) as well as
+binary operators. An example of this is:</p>
+<div class="highlight-python"><div class="highlight"><pre># Logical unary not.
+def unary!(v)
+  if v then
+    0
+  else
+    1;
+
+# Define > with the same precedence as <.
+def binary> 10 (LHS RHS)
+  RHS < LHS;
+
+# Binary "logical or", (note that it does not "short circuit")
+def binary| 5 (LHS RHS)
+  if LHS then
+    1
+  else if RHS then
+    1
+  else
+    0;
+
+# Define = with slightly lower precedence than relationals.
+def binary= 9 (LHS RHS)
+  !(LHS < RHS | LHS > RHS);
+</pre></div>
+</div>
+<p>Many languages aspire to being able to implement their standard runtime
+library in the language itself. In Kaleidoscope, we can implement
+significant parts of the language in the library!</p>
+<p>We will break down implementation of these features into two parts:
+implementing support for user-defined binary operators and adding unary
+operators.</p>
+</div>
+<div class="section" id="user-defined-binary-operators">
+<h2><a class="toc-backref" href="#id3">6.3. User-defined Binary Operators</a><a class="headerlink" href="#user-defined-binary-operators" title="Permalink to this headline">¶</a></h2>
+<p>Adding support for user-defined binary operators is pretty simple with
+our current framework. We’ll first add support for the unary/binary
+keywords:</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="k">type</span> <span class="n">token</span> <span class="o">=</span>
+  <span class="o">...</span>
+  <span class="c">(* operators *)</span>
+  <span class="o">|</span> <span class="nc">Binary</span> <span class="o">|</span> <span class="nn">Unary</span>
+
+<span class="p">...</span>
+
+<span class="n">and</span> <span class="n">lex_ident</span> <span class="n">buffer</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="o">...</span>
+      <span class="o">|</span> <span class="s2">"for"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">For</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+      <span class="o">|</span> <span class="s2">"in"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">In</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+      <span class="o">|</span> <span class="s2">"binary"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Binary</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+      <span class="o">|</span> <span class="s2">"unary"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Unary</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+</pre></div>
+</div>
+<p>This just adds lexer support for the unary and binary keywords, like we
+did in <a class="reference external" href="OCamlLangImpl5.html#iflexer">previous chapters</a>. One nice
+thing about our current AST, is that we represent binary operators with
+full generalisation by using their ASCII code as the opcode. For our
+extended operators, we’ll use this same representation, so we don’t need
+any new AST or parser support.</p>
+<p>On the other hand, we have to be able to represent the definitions of
+these new operators, in the “def binary| 5” part of the function
+definition. In our grammar so far, the “name” for the function
+definition is parsed as the “prototype” production and into the
+<tt class="docutils literal"><span class="pre">Ast.Prototype</span></tt> AST node. To represent our new user-defined operators
+as prototypes, we have to extend the <tt class="docutils literal"><span class="pre">Ast.Prototype</span></tt> AST node like
+this:</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="c">(* proto - This type represents the "prototype" for a function, which captures</span>
+<span class="c"> * its name, and its argument names (thus implicitly the number of arguments the</span>
+<span class="c"> * function takes). *)</span>
+<span class="k">type</span> <span class="n">proto</span> <span class="o">=</span>
+  <span class="o">|</span> <span class="nc">Prototype</span> <span class="k">of</span> <span class="kt">string</span> <span class="o">*</span> <span class="kt">string</span> <span class="kt">array</span>
+  <span class="o">|</span> <span class="nc">BinOpPrototype</span> <span class="k">of</span> <span class="kt">string</span> <span class="o">*</span> <span class="kt">string</span> <span class="kt">array</span> <span class="o">*</span> <span class="kt">int</span>
+</pre></div>
+</div>
+<p>Basically, in addition to knowing a name for the prototype, we now keep
+track of whether it was an operator, and if it was, what precedence
+level the operator is at. The precedence is only used for binary
+operators (as you’ll see below, it just doesn’t apply for unary
+operators). Now that we have a way to represent the prototype for a
+user-defined operator, we need to parse it:</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="c">(* prototype</span>
+<span class="c"> *   ::= id '(' id* ')'</span>
+<span class="c"> *   ::= binary LETTER number? (id, id)</span>
+<span class="c"> *   ::= unary LETTER number? (id) *)</span>
+<span class="k">let</span> <span class="n">parse_prototype</span> <span class="o">=</span>
+  <span class="k">let</span> <span class="k">rec</span> <span class="n">parse_args</span> <span class="n">accumulator</span> <span class="o">=</span> <span class="n">parser</span>
+    <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Ident</span> <span class="n">id</span><span class="o">;</span> <span class="n">e</span><span class="o">=</span><span class="n">parse_args</span> <span class="o">(</span><span class="n">id</span><span class="o">::</span><span class="n">accumulator</span><span class="o">)</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">e</span>
+    <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">accumulator</span>
+  <span class="k">in</span>
+  <span class="k">let</span> <span class="n">parse_operator</span> <span class="o">=</span> <span class="n">parser</span>
+    <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Unary</span> <span class="o">>]</span> <span class="o">-></span> <span class="s2">"unary"</span><span class="o">,</span> <span class="mi">1</span>
+    <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Binary</span> <span class="o">>]</span> <span class="o">-></span> <span class="s2">"binary"</span><span class="o">,</span> <span class="mi">2</span>
+  <span class="k">in</span>
+  <span class="k">let</span> <span class="n">parse_binary_precedence</span> <span class="o">=</span> <span class="n">parser</span>
+    <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Number</span> <span class="n">n</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">int_of_float</span> <span class="n">n</span>
+    <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span> <span class="mi">30</span>
+  <span class="k">in</span>
+  <span class="n">parser</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Ident</span> <span class="n">id</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">'('</span> <span class="o">??</span> <span class="s2">"expected '(' in prototype"</span><span class="o">;</span>
+       <span class="n">args</span><span class="o">=</span><span class="n">parse_args</span> <span class="bp">[]</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">')'</span> <span class="o">??</span> <span class="s2">"expected ')' in prototype"</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="c">(* success. *)</span>
+      <span class="nn">Ast</span><span class="p">.</span><span class="nc">Prototype</span> <span class="o">(</span><span class="n">id</span><span class="o">,</span> <span class="nn">Array</span><span class="p">.</span><span class="n">of_list</span> <span class="o">(</span><span class="nn">List</span><span class="p">.</span><span class="n">rev</span> <span class="n">args</span><span class="o">))</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="o">(</span><span class="n">prefix</span><span class="o">,</span> <span class="n">kind</span><span class="o">)=</span><span class="n">parse_operator</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="n">op</span> <span class="o">??</span> <span class="s2">"expected an operator"</span><span class="o">;</span>
+       <span class="c">(* Read the precedence if present. *)</span>
+       <span class="n">binary_precedence</span><span class="o">=</span><span class="n">parse_binary_precedence</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">'('</span> <span class="o">??</span> <span class="s2">"expected '(' in prototype"</span><span class="o">;</span>
+        <span class="n">args</span><span class="o">=</span><span class="n">parse_args</span> <span class="bp">[]</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">')'</span> <span class="o">??</span> <span class="s2">"expected ')' in prototype"</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="k">let</span> <span class="n">name</span> <span class="o">=</span> <span class="n">prefix</span> <span class="o">^</span> <span class="o">(</span><span class="nn">String</span><span class="p">.</span><span class="n">make</span> <span class="mi">1</span> <span class="n">op</span><span class="o">)</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">args</span> <span class="o">=</span> <span class="nn">Array</span><span class="p">.</span><span class="n">of_list</span> <span class="o">(</span><span class="nn">List</span><span class="p">.</span><span class="n">rev</span> <span class="n">args</span><span class="o">)</span> <span class="k">in</span>
+
+      <span class="c">(* Verify right number of arguments for operator. *)</span>
+      <span class="k">if</span> <span class="nn">Array</span><span class="p">.</span><span class="n">length</span> <span class="n">args</span> <span class="o">!=</span> <span class="n">kind</span>
+      <span class="k">then</span> <span class="k">raise</span> <span class="o">(</span><span class="nn">Stream</span><span class="p">.</span><span class="nc">Error</span> <span class="s2">"invalid number of operands for operator"</span><span class="o">)</span>
+      <span class="k">else</span>
+        <span class="k">if</span> <span class="n">kind</span> <span class="o">==</span> <span class="mi">1</span> <span class="k">then</span>
+          <span class="nn">Ast</span><span class="p">.</span><span class="nc">Prototype</span> <span class="o">(</span><span class="n">name</span><span class="o">,</span> <span class="n">args</span><span class="o">)</span>
+        <span class="k">else</span>
+          <span class="nn">Ast</span><span class="p">.</span><span class="nc">BinOpPrototype</span> <span class="o">(</span><span class="n">name</span><span class="o">,</span> <span class="n">args</span><span class="o">,</span> <span class="n">binary_precedence</span><span class="o">)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="k">raise</span> <span class="o">(</span><span class="nn">Stream</span><span class="p">.</span><span class="nc">Error</span> <span class="s2">"expected function name in prototype"</span><span class="o">)</span>
+</pre></div>
+</div>
+<p>This is all fairly straightforward parsing code, and we have already
+seen a lot of similar code in the past. One interesting part about the
+code above is the couple lines that set up <tt class="docutils literal"><span class="pre">name</span></tt> for binary
+operators. This builds names like “binary@” for a newly defined “@”
+operator. This then takes advantage of the fact that symbol names in the
+LLVM symbol table are allowed to have any character in them, including
+embedded nul characters.</p>
+<p>The next interesting thing to add, is codegen support for these binary
+operators. Given our current structure, this is a simple addition of a
+default case for our existing binary operator node:</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="k">let</span> <span class="n">codegen_expr</span> <span class="o">=</span> <span class="k">function</span>
+  <span class="o">...</span>
+  <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Binary</span> <span class="o">(</span><span class="n">op</span><span class="o">,</span> <span class="n">lhs</span><span class="o">,</span> <span class="n">rhs</span><span class="o">)</span> <span class="o">-></span>
+      <span class="k">let</span> <span class="n">lhs_val</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">lhs</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">rhs_val</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">rhs</span> <span class="k">in</span>
+      <span class="k">begin</span>
+        <span class="k">match</span> <span class="n">op</span> <span class="k">with</span>
+        <span class="o">|</span> <span class="sc">'+'</span> <span class="o">-></span> <span class="n">build_add</span> <span class="n">lhs_val</span> <span class="n">rhs_val</span> <span class="s2">"addtmp"</span> <span class="n">builder</span>
+        <span class="o">|</span> <span class="sc">'-'</span> <span class="o">-></span> <span class="n">build_sub</span> <span class="n">lhs_val</span> <span class="n">rhs_val</span> <span class="s2">"subtmp"</span> <span class="n">builder</span>
+        <span class="o">|</span> <span class="sc">'*'</span> <span class="o">-></span> <span class="n">build_mul</span> <span class="n">lhs_val</span> <span class="n">rhs_val</span> <span class="s2">"multmp"</span> <span class="n">builder</span>
+        <span class="o">|</span> <span class="sc">'<'</span> <span class="o">-></span>
+            <span class="c">(* Convert bool 0/1 to double 0.0 or 1.0 *)</span>
+            <span class="k">let</span> <span class="n">i</span> <span class="o">=</span> <span class="n">build_fcmp</span> <span class="nn">Fcmp</span><span class="p">.</span><span class="nc">Ult</span> <span class="n">lhs_val</span> <span class="n">rhs_val</span> <span class="s2">"cmptmp"</span> <span class="n">builder</span> <span class="k">in</span>
+            <span class="n">build_uitofp</span> <span class="n">i</span> <span class="n">double_type</span> <span class="s2">"booltmp"</span> <span class="n">builder</span>
+        <span class="o">|</span> <span class="o">_</span> <span class="o">-></span>
+            <span class="c">(* If it wasn't a builtin binary operator, it must be a user defined</span>
+<span class="c">             * one. Emit a call to it. *)</span>
+            <span class="k">let</span> <span class="n">callee</span> <span class="o">=</span> <span class="s2">"binary"</span> <span class="o">^</span> <span class="o">(</span><span class="nn">String</span><span class="p">.</span><span class="n">make</span> <span class="mi">1</span> <span class="n">op</span><span class="o">)</span> <span class="k">in</span>
+            <span class="k">let</span> <span class="n">callee</span> <span class="o">=</span>
+              <span class="k">match</span> <span class="n">lookup_function</span> <span class="n">callee</span> <span class="n">the_module</span> <span class="k">with</span>
+              <span class="o">|</span> <span class="nc">Some</span> <span class="n">callee</span> <span class="o">-></span> <span class="n">callee</span>
+              <span class="o">|</span> <span class="nc">None</span> <span class="o">-></span> <span class="k">raise</span> <span class="o">(</span><span class="nc">Error</span> <span class="s2">"binary operator not found!"</span><span class="o">)</span>
+            <span class="k">in</span>
+            <span class="n">build_call</span> <span class="n">callee</span> <span class="o">[|</span><span class="n">lhs_val</span><span class="o">;</span> <span class="n">rhs_val</span><span class="o">|]</span> <span class="s2">"binop"</span> <span class="n">builder</span>
+      <span class="k">end</span>
+</pre></div>
+</div>
+<p>As you can see above, the new code is actually really simple. It just
+does a lookup for the appropriate operator in the symbol table and
+generates a function call to it. Since user-defined operators are just
+built as normal functions (because the “prototype” boils down to a
+function with the right name) everything falls into place.</p>
+<p>The final piece of code we are missing, is a bit of top level magic:</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="k">let</span> <span class="n">codegen_func</span> <span class="n">the_fpm</span> <span class="o">=</span> <span class="k">function</span>
+  <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Function</span> <span class="o">(</span><span class="n">proto</span><span class="o">,</span> <span class="n">body</span><span class="o">)</span> <span class="o">-></span>
+      <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">clear</span> <span class="n">named_values</span><span class="o">;</span>
+      <span class="k">let</span> <span class="n">the_function</span> <span class="o">=</span> <span class="n">codegen_proto</span> <span class="n">proto</span> <span class="k">in</span>
+
+      <span class="c">(* If this is an operator, install it. *)</span>
+      <span class="k">begin</span> <span class="k">match</span> <span class="n">proto</span> <span class="k">with</span>
+      <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">BinOpPrototype</span> <span class="o">(</span><span class="n">name</span><span class="o">,</span> <span class="n">args</span><span class="o">,</span> <span class="n">prec</span><span class="o">)</span> <span class="o">-></span>
+          <span class="k">let</span> <span class="n">op</span> <span class="o">=</span> <span class="n">name</span><span class="o">.[</span><span class="nn">String</span><span class="p">.</span><span class="n">length</span> <span class="n">name</span> <span class="o">-</span> <span class="mi">1</span><span class="o">]</span> <span class="k">in</span>
+          <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">add</span> <span class="nn">Parser</span><span class="p">.</span><span class="n">binop_precedence</span> <span class="n">op</span> <span class="n">prec</span><span class="o">;</span>
+      <span class="o">|</span> <span class="o">_</span> <span class="o">-></span> <span class="bp">()</span>
+      <span class="k">end</span><span class="o">;</span>
+
+      <span class="c">(* Create a new basic block to start insertion into. *)</span>
+      <span class="k">let</span> <span class="n">bb</span> <span class="o">=</span> <span class="n">append_block</span> <span class="n">context</span> <span class="s2">"entry"</span> <span class="n">the_function</span> <span class="k">in</span>
+      <span class="n">position_at_end</span> <span class="n">bb</span> <span class="n">builder</span><span class="o">;</span>
+      <span class="o">...</span>
+</pre></div>
+</div>
+<p>Basically, before codegening a function, if it is a user-defined
+operator, we register it in the precedence table. This allows the binary
+operator parsing logic we already have in place to handle it. Since we
+are working on a fully-general operator precedence parser, this is all
+we need to do to “extend the grammar”.</p>
+<p>Now we have useful user-defined binary operators. This builds a lot on
+the previous framework we built for other operators. Adding unary
+operators is a bit more challenging, because we don’t have any framework
+for it yet - lets see what it takes.</p>
+</div>
+<div class="section" id="user-defined-unary-operators">
+<h2><a class="toc-backref" href="#id4">6.4. User-defined Unary Operators</a><a class="headerlink" href="#user-defined-unary-operators" title="Permalink to this headline">¶</a></h2>
+<p>Since we don’t currently support unary operators in the Kaleidoscope
+language, we’ll need to add everything to support them. Above, we added
+simple support for the ‘unary’ keyword to the lexer. In addition to
+that, we need an AST node:</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="k">type</span> <span class="n">expr</span> <span class="o">=</span>
+  <span class="o">...</span>
+  <span class="c">(* variant for a unary operator. *)</span>
+  <span class="o">|</span> <span class="nc">Unary</span> <span class="k">of</span> <span class="kt">char</span> <span class="o">*</span> <span class="n">expr</span>
+  <span class="o">...</span>
+</pre></div>
+</div>
+<p>This AST node is very simple and obvious by now. It directly mirrors the
+binary operator AST node, except that it only has one child. With this,
+we need to add the parsing logic. Parsing a unary operator is pretty
+simple: we’ll add a new function to do it:</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="c">(* unary</span>
+<span class="c"> *   ::= primary</span>
+<span class="c"> *   ::= '!' unary *)</span>
+<span class="ow">and</span> <span class="n">parse_unary</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="c">(* If this is a unary operator, read it. *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="n">op</span> <span class="k">when</span> <span class="n">op</span> <span class="o">!=</span> <span class="sc">'('</span> <span class="o">&&</span> <span class="n">op</span> <span class="o">!=</span> <span class="sc">')'</span><span class="o">;</span> <span class="n">operand</span><span class="o">=</span><span class="n">parse_expr</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="nn">Ast</span><span class="p">.</span><span class="nc">Unary</span> <span class="o">(</span><span class="n">op</span><span class="o">,</span> <span class="n">operand</span><span class="o">)</span>
+
+  <span class="c">(* If the current token is not an operator, it must be a primary expr. *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">parse_primary</span> <span class="n">stream</span>
+</pre></div>
+</div>
+<p>The grammar we add is pretty straightforward here. If we see a unary
+operator when parsing a primary operator, we eat the operator as a
+prefix and parse the remaining piece as another unary operator. This
+allows us to handle multiple unary operators (e.g. ”!!x”). Note that
+unary operators can’t have ambiguous parses like binary operators can,
+so there is no need for precedence information.</p>
+<p>The problem with this function, is that we need to call ParseUnary from
+somewhere. To do this, we change previous callers of ParsePrimary to
+call <tt class="docutils literal"><span class="pre">parse_unary</span></tt> instead:</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="c">(* binoprhs</span>
+<span class="c"> *   ::= ('+' primary)* *)</span>
+<span class="ow">and</span> <span class="n">parse_bin_rhs</span> <span class="n">expr_prec</span> <span class="n">lhs</span> <span class="n">stream</span> <span class="o">=</span>
+        <span class="o">...</span>
+        <span class="c">(* Parse the unary expression after the binary operator. *)</span>
+        <span class="k">let</span> <span class="n">rhs</span> <span class="o">=</span> <span class="n">parse_unary</span> <span class="n">stream</span> <span class="k">in</span>
+        <span class="o">...</span>
+
+<span class="o">...</span>
+
+<span class="c">(* expression</span>
+<span class="c"> *   ::= primary binoprhs *)</span>
+<span class="ow">and</span> <span class="n">parse_expr</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="n">lhs</span><span class="o">=</span><span class="n">parse_unary</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">parse_bin_rhs</span> <span class="mi">0</span> <span class="n">lhs</span> <span class="n">stream</span>
+</pre></div>
+</div>
+<p>With these two simple changes, we are now able to parse unary operators
+and build the AST for them. Next up, we need to add parser support for
+prototypes, to parse the unary operator prototype. We extend the binary
+operator code above with:</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="c">(* prototype</span>
+<span class="c"> *   ::= id '(' id* ')'</span>
+<span class="c"> *   ::= binary LETTER number? (id, id)</span>
+<span class="c"> *   ::= unary LETTER number? (id) *)</span>
+<span class="k">let</span> <span class="n">parse_prototype</span> <span class="o">=</span>
+  <span class="k">let</span> <span class="k">rec</span> <span class="n">parse_args</span> <span class="n">accumulator</span> <span class="o">=</span> <span class="n">parser</span>
+    <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Ident</span> <span class="n">id</span><span class="o">;</span> <span class="n">e</span><span class="o">=</span><span class="n">parse_args</span> <span class="o">(</span><span class="n">id</span><span class="o">::</span><span class="n">accumulator</span><span class="o">)</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">e</span>
+    <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">accumulator</span>
+  <span class="k">in</span>
+  <span class="k">let</span> <span class="n">parse_operator</span> <span class="o">=</span> <span class="n">parser</span>
+    <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Unary</span> <span class="o">>]</span> <span class="o">-></span> <span class="s2">"unary"</span><span class="o">,</span> <span class="mi">1</span>
+    <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Binary</span> <span class="o">>]</span> <span class="o">-></span> <span class="s2">"binary"</span><span class="o">,</span> <span class="mi">2</span>
+  <span class="k">in</span>
+  <span class="k">let</span> <span class="n">parse_binary_precedence</span> <span class="o">=</span> <span class="n">parser</span>
+    <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Number</span> <span class="n">n</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">int_of_float</span> <span class="n">n</span>
+    <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span> <span class="mi">30</span>
+  <span class="k">in</span>
+  <span class="n">parser</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Ident</span> <span class="n">id</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">'('</span> <span class="o">??</span> <span class="s2">"expected '(' in prototype"</span><span class="o">;</span>
+       <span class="n">args</span><span class="o">=</span><span class="n">parse_args</span> <span class="bp">[]</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">')'</span> <span class="o">??</span> <span class="s2">"expected ')' in prototype"</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="c">(* success. *)</span>
+      <span class="nn">Ast</span><span class="p">.</span><span class="nc">Prototype</span> <span class="o">(</span><span class="n">id</span><span class="o">,</span> <span class="nn">Array</span><span class="p">.</span><span class="n">of_list</span> <span class="o">(</span><span class="nn">List</span><span class="p">.</span><span class="n">rev</span> <span class="n">args</span><span class="o">))</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="o">(</span><span class="n">prefix</span><span class="o">,</span> <span class="n">kind</span><span class="o">)=</span><span class="n">parse_operator</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="n">op</span> <span class="o">??</span> <span class="s2">"expected an operator"</span><span class="o">;</span>
+       <span class="c">(* Read the precedence if present. *)</span>
+       <span class="n">binary_precedence</span><span class="o">=</span><span class="n">parse_binary_precedence</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">'('</span> <span class="o">??</span> <span class="s2">"expected '(' in prototype"</span><span class="o">;</span>
+        <span class="n">args</span><span class="o">=</span><span class="n">parse_args</span> <span class="bp">[]</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">')'</span> <span class="o">??</span> <span class="s2">"expected ')' in prototype"</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="k">let</span> <span class="n">name</span> <span class="o">=</span> <span class="n">prefix</span> <span class="o">^</span> <span class="o">(</span><span class="nn">String</span><span class="p">.</span><span class="n">make</span> <span class="mi">1</span> <span class="n">op</span><span class="o">)</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">args</span> <span class="o">=</span> <span class="nn">Array</span><span class="p">.</span><span class="n">of_list</span> <span class="o">(</span><span class="nn">List</span><span class="p">.</span><span class="n">rev</span> <span class="n">args</span><span class="o">)</span> <span class="k">in</span>
+
+      <span class="c">(* Verify right number of arguments for operator. *)</span>
+      <span class="k">if</span> <span class="nn">Array</span><span class="p">.</span><span class="n">length</span> <span class="n">args</span> <span class="o">!=</span> <span class="n">kind</span>
+      <span class="k">then</span> <span class="k">raise</span> <span class="o">(</span><span class="nn">Stream</span><span class="p">.</span><span class="nc">Error</span> <span class="s2">"invalid number of operands for operator"</span><span class="o">)</span>
+      <span class="k">else</span>
+        <span class="k">if</span> <span class="n">kind</span> <span class="o">==</span> <span class="mi">1</span> <span class="k">then</span>
+          <span class="nn">Ast</span><span class="p">.</span><span class="nc">Prototype</span> <span class="o">(</span><span class="n">name</span><span class="o">,</span> <span class="n">args</span><span class="o">)</span>
+        <span class="k">else</span>
+          <span class="nn">Ast</span><span class="p">.</span><span class="nc">BinOpPrototype</span> <span class="o">(</span><span class="n">name</span><span class="o">,</span> <span class="n">args</span><span class="o">,</span> <span class="n">binary_precedence</span><span class="o">)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="k">raise</span> <span class="o">(</span><span class="nn">Stream</span><span class="p">.</span><span class="nc">Error</span> <span class="s2">"expected function name in prototype"</span><span class="o">)</span>
+</pre></div>
+</div>
+<p>As with binary operators, we name unary operators with a name that
+includes the operator character. This assists us at code generation
+time. Speaking of, the final piece we need to add is codegen support for
+unary operators. It looks like this:</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="k">let</span> <span class="k">rec</span> <span class="n">codegen_expr</span> <span class="o">=</span> <span class="k">function</span>
+  <span class="o">...</span>
+  <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Unary</span> <span class="o">(</span><span class="n">op</span><span class="o">,</span> <span class="n">operand</span><span class="o">)</span> <span class="o">-></span>
+      <span class="k">let</span> <span class="n">operand</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">operand</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">callee</span> <span class="o">=</span> <span class="s2">"unary"</span> <span class="o">^</span> <span class="o">(</span><span class="nn">String</span><span class="p">.</span><span class="n">make</span> <span class="mi">1</span> <span class="n">op</span><span class="o">)</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">callee</span> <span class="o">=</span>
+        <span class="k">match</span> <span class="n">lookup_function</span> <span class="n">callee</span> <span class="n">the_module</span> <span class="k">with</span>
+        <span class="o">|</span> <span class="nc">Some</span> <span class="n">callee</span> <span class="o">-></span> <span class="n">callee</span>
+        <span class="o">|</span> <span class="nc">None</span> <span class="o">-></span> <span class="k">raise</span> <span class="o">(</span><span class="nc">Error</span> <span class="s2">"unknown unary operator"</span><span class="o">)</span>
+      <span class="k">in</span>
+      <span class="n">build_call</span> <span class="n">callee</span> <span class="o">[|</span><span class="n">operand</span><span class="o">|]</span> <span class="s2">"unop"</span> <span class="n">builder</span>
+</pre></div>
+</div>
+<p>This code is similar to, but simpler than, the code for binary
+operators. It is simpler primarily because it doesn’t need to handle any
+predefined operators.</p>
+</div>
+<div class="section" id="kicking-the-tires">
+<h2><a class="toc-backref" href="#id5">6.5. Kicking the Tires</a><a class="headerlink" href="#kicking-the-tires" title="Permalink to this headline">¶</a></h2>
+<p>It is somewhat hard to believe, but with a few simple extensions we’ve
+covered in the last chapters, we have grown a real-ish language. With
+this, we can do a lot of interesting things, including I/O, math, and a
+bunch of other things. For example, we can now add a nice sequencing
+operator (printd is defined to print out the specified value and a
+newline):</p>
+<div class="highlight-python"><div class="highlight"><pre>ready> extern printd(x);
+Read extern: declare double @printd(double)
+ready> def binary : 1 (x y) 0;  # Low-precedence operator that ignores operands.
+..
+ready> printd(123) : printd(456) : printd(789);
+123.000000
+456.000000
+789.000000
+Evaluated to 0.000000
+</pre></div>
+</div>
+<p>We can also define a bunch of other “primitive” operations, such as:</p>
+<div class="highlight-python"><div class="highlight"><pre># Logical unary not.
+def unary!(v)
+  if v then
+    0
+  else
+    1;
+
+# Unary negate.
+def unary-(v)
+  0-v;
+
+# Define > with the same precedence as <.
+def binary> 10 (LHS RHS)
+  RHS < LHS;
+
+# Binary logical or, which does not short circuit.
+def binary| 5 (LHS RHS)
+  if LHS then
+    1
+  else if RHS then
+    1
+  else
+    0;
+
+# Binary logical and, which does not short circuit.
+def binary& 6 (LHS RHS)
+  if !LHS then
+    0
+  else
+    !!RHS;
+
+# Define = with slightly lower precedence than relationals.
+def binary = 9 (LHS RHS)
+  !(LHS < RHS | LHS > RHS);
+</pre></div>
+</div>
+<p>Given the previous if/then/else support, we can also define interesting
+functions for I/O. For example, the following prints out a character
+whose “density” reflects the value passed in: the lower the value, the
+denser the character:</p>
+<div class="highlight-python"><div class="highlight"><pre>ready>
+
+extern putchard(char)
+def printdensity(d)
+  if d > 8 then
+    putchard(32)  # ' '
+  else if d > 4 then
+    putchard(46)  # '.'
+  else if d > 2 then
+    putchard(43)  # '+'
+  else
+    putchard(42); # '*'
+...
+ready> printdensity(1): printdensity(2): printdensity(3) :
+          printdensity(4): printdensity(5): printdensity(9): putchard(10);
+*++..
+Evaluated to 0.000000
+</pre></div>
+</div>
+<p>Based on these simple primitive operations, we can start to define more
+interesting things. For example, here’s a little function that solves
+for the number of iterations it takes a function in the complex plane to
+converge:</p>
+<div class="highlight-python"><div class="highlight"><pre># determine whether the specific location diverges.
+# Solve for z = z^2 + c in the complex plane.
+def mandleconverger(real imag iters creal cimag)
+  if iters > 255 | (real*real + imag*imag > 4) then
+    iters
+  else
+    mandleconverger(real*real - imag*imag + creal,
+                    2*real*imag + cimag,
+                    iters+1, creal, cimag);
+
+# return the number of iterations required for the iteration to escape
+def mandleconverge(real imag)
+  mandleconverger(real, imag, 0, real, imag);
+</pre></div>
+</div>
+<p>This “z = z<sup>2</sup> + c” function is a beautiful little creature
+that is the basis for computation of the <a class="reference external" href="http://en.wikipedia.org/wiki/Mandelbrot_set">Mandelbrot
+Set</a>. Our
+<tt class="docutils literal"><span class="pre">mandelconverge</span></tt> function returns the number of iterations that it
+takes for a complex orbit to escape, saturating to 255. This is not a
+very useful function by itself, but if you plot its value over a
+two-dimensional plane, you can see the Mandelbrot set. Given that we are
+limited to using putchard here, our amazing graphical output is limited,
+but we can whip together something using the density plotter above:</p>
+<div class="highlight-python"><div class="highlight"><pre># compute and plot the mandlebrot set with the specified 2 dimensional range
+# info.
+def mandelhelp(xmin xmax xstep   ymin ymax ystep)
+  for y = ymin, y < ymax, ystep in (
+    (for x = xmin, x < xmax, xstep in
+       printdensity(mandleconverge(x,y)))
+    : putchard(10)
+  )
+
+# mandel - This is a convenient helper function for plotting the mandelbrot set
+# from the specified position with the specified Magnification.
+def mandel(realstart imagstart realmag imagmag)
+  mandelhelp(realstart, realstart+realmag*78, realmag,
+             imagstart, imagstart+imagmag*40, imagmag);
+</pre></div>
+</div>
+<p>Given this, we can try plotting out the mandlebrot set! Lets try it out:</p>
+<div class="highlight-python"><div class="highlight"><pre>ready> mandel(-2.3, -1.3, 0.05, 0.07);
+*******************************+++++++++++*************************************
+*************************+++++++++++++++++++++++*******************************
+**********************+++++++++++++++++++++++++++++****************************
+*******************+++++++++++++++++++++.. ...++++++++*************************
+*****************++++++++++++++++++++++.... ...+++++++++***********************
+***************+++++++++++++++++++++++.....   ...+++++++++*********************
+**************+++++++++++++++++++++++....     ....+++++++++********************
+*************++++++++++++++++++++++......      .....++++++++*******************
+************+++++++++++++++++++++.......       .......+++++++******************
+***********+++++++++++++++++++....                ... .+++++++*****************
+**********+++++++++++++++++.......                     .+++++++****************
+*********++++++++++++++...........                    ...+++++++***************
+********++++++++++++............                      ...++++++++**************
+********++++++++++... ..........                        .++++++++**************
+*******+++++++++.....                                   .+++++++++*************
+*******++++++++......                                  ..+++++++++*************
+*******++++++.......                                   ..+++++++++*************
+*******+++++......                                     ..+++++++++*************
+*******.... ....                                      ...+++++++++*************
+*******.... .                                         ...+++++++++*************
+*******+++++......                                    ...+++++++++*************
+*******++++++.......                                   ..+++++++++*************
+*******++++++++......                                   .+++++++++*************
+*******+++++++++.....                                  ..+++++++++*************
+********++++++++++... ..........                        .++++++++**************
+********++++++++++++............                      ...++++++++**************
+*********++++++++++++++..........                     ...+++++++***************
+**********++++++++++++++++........                     .+++++++****************
+**********++++++++++++++++++++....                ... ..+++++++****************
+***********++++++++++++++++++++++.......       .......++++++++*****************
+************+++++++++++++++++++++++......      ......++++++++******************
+**************+++++++++++++++++++++++....      ....++++++++********************
+***************+++++++++++++++++++++++.....   ...+++++++++*********************
+*****************++++++++++++++++++++++....  ...++++++++***********************
+*******************+++++++++++++++++++++......++++++++*************************
+*********************++++++++++++++++++++++.++++++++***************************
+*************************+++++++++++++++++++++++*******************************
+******************************+++++++++++++************************************
+*******************************************************************************
+*******************************************************************************
+*******************************************************************************
+Evaluated to 0.000000
+ready> mandel(-2, -1, 0.02, 0.04);
+**************************+++++++++++++++++++++++++++++++++++++++++++++++++++++
+***********************++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+*********************+++++++++++++++++++++++++++++++++++++++++++++++++++++++++.
+*******************+++++++++++++++++++++++++++++++++++++++++++++++++++++++++...
+*****************+++++++++++++++++++++++++++++++++++++++++++++++++++++++++.....
+***************++++++++++++++++++++++++++++++++++++++++++++++++++++++++........
+**************++++++++++++++++++++++++++++++++++++++++++++++++++++++...........
+************+++++++++++++++++++++++++++++++++++++++++++++++++++++..............
+***********++++++++++++++++++++++++++++++++++++++++++++++++++........        .
+**********++++++++++++++++++++++++++++++++++++++++++++++.............
+********+++++++++++++++++++++++++++++++++++++++++++..................
+*******+++++++++++++++++++++++++++++++++++++++.......................
+******+++++++++++++++++++++++++++++++++++...........................
+*****++++++++++++++++++++++++++++++++............................
+*****++++++++++++++++++++++++++++...............................
+****++++++++++++++++++++++++++......   .........................
+***++++++++++++++++++++++++.........     ......    ...........
+***++++++++++++++++++++++............
+**+++++++++++++++++++++..............
+**+++++++++++++++++++................
+*++++++++++++++++++.................
+*++++++++++++++++............ ...
+*++++++++++++++..............
+*+++....++++................
+*..........  ...........
+*
+*..........  ...........
+*+++....++++................
+*++++++++++++++..............
+*++++++++++++++++............ ...
+*++++++++++++++++++.................
+**+++++++++++++++++++................
+**+++++++++++++++++++++..............
+***++++++++++++++++++++++............
+***++++++++++++++++++++++++.........     ......    ...........
+****++++++++++++++++++++++++++......   .........................
+*****++++++++++++++++++++++++++++...............................
+*****++++++++++++++++++++++++++++++++............................
+******+++++++++++++++++++++++++++++++++++...........................
+*******+++++++++++++++++++++++++++++++++++++++.......................
+********+++++++++++++++++++++++++++++++++++++++++++..................
+Evaluated to 0.000000
+ready> mandel(-0.9, -1.4, 0.02, 0.03);
+*******************************************************************************
+*******************************************************************************
+*******************************************************************************
+**********+++++++++++++++++++++************************************************
+*+++++++++++++++++++++++++++++++++++++++***************************************
++++++++++++++++++++++++++++++++++++++++++++++**********************************
+++++++++++++++++++++++++++++++++++++++++++++++++++*****************************
+++++++++++++++++++++++++++++++++++++++++++++++++++++++*************************
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++**********************
++++++++++++++++++++++++++++++++++.........++++++++++++++++++*******************
++++++++++++++++++++++++++++++++....   ......+++++++++++++++++++****************
++++++++++++++++++++++++++++++.......  ........+++++++++++++++++++**************
+++++++++++++++++++++++++++++........   ........++++++++++++++++++++************
++++++++++++++++++++++++++++.........     ..  ...+++++++++++++++++++++**********
+++++++++++++++++++++++++++...........        ....++++++++++++++++++++++********
+++++++++++++++++++++++++.............       .......++++++++++++++++++++++******
++++++++++++++++++++++++.............        ........+++++++++++++++++++++++****
+++++++++++++++++++++++...........           ..........++++++++++++++++++++++***
+++++++++++++++++++++...........                .........++++++++++++++++++++++*
+++++++++++++++++++............                  ...........++++++++++++++++++++
+++++++++++++++++...............                 .............++++++++++++++++++
+++++++++++++++.................                 ...............++++++++++++++++
+++++++++++++..................                  .................++++++++++++++
++++++++++..................                      .................+++++++++++++
+++++++........        .                               .........  ..++++++++++++
+++............                                         ......    ....++++++++++
+..............                                                    ...++++++++++
+..............                                                    ....+++++++++
+..............                                                    .....++++++++
+.............                                                    ......++++++++
+...........                                                     .......++++++++
+.........                                                       ........+++++++
+.........                                                       ........+++++++
+.........                                                           ....+++++++
+........                                                             ...+++++++
+.......                                                              ...+++++++
+                                                                    ....+++++++
+                                                                   .....+++++++
+                                                                    ....+++++++
+                                                                    ....+++++++
+                                                                    ....+++++++
+Evaluated to 0.000000
+ready> ^D
+</pre></div>
+</div>
+<p>At this point, you may be starting to realize that Kaleidoscope is a
+real and powerful language. It may not be self-similar :), but it can be
+used to plot things that are!</p>
+<p>With this, we conclude the “adding user-defined operators” chapter of
+the tutorial. We have successfully augmented our language, adding the
+ability to extend the language in the library, and we have shown how
+this can be used to build a simple but interesting end-user application
+in Kaleidoscope. At this point, Kaleidoscope can build a variety of
+applications that are functional and can call functions with
+side-effects, but it can’t actually define and mutate a variable itself.</p>
+<p>Strikingly, variable mutation is an important feature of some languages,
+and it is not at all obvious how to <a class="reference external" href="OCamlLangImpl7.html">add support for mutable
+variables</a> without having to add an “SSA
+construction” phase to your front-end. In the next chapter, we will
+describe how you can add variable mutation without building SSA in your
+front-end.</p>
+</div>
+<div class="section" id="full-code-listing">
+<h2><a class="toc-backref" href="#id6">6.6. Full Code Listing</a><a class="headerlink" href="#full-code-listing" title="Permalink to this headline">¶</a></h2>
+<p>Here is the complete code listing for our running example, enhanced with
+the if/then/else and for expressions.. To build this example, use:</p>
+<div class="highlight-bash"><div class="highlight"><pre><span class="c"># Compile</span>
+ocamlbuild toy.byte
+<span class="c"># Run</span>
+./toy.byte
+</pre></div>
+</div>
+<p>Here is the code:</p>
+<dl class="docutils">
+<dt>_tags:</dt>
+<dd><div class="first last highlight-python"><div class="highlight"><pre><{lexer,parser}.ml>: use_camlp4, pp(camlp4of)
+<*.{byte,native}>: g++, use_llvm, use_llvm_analysis
+<*.{byte,native}>: use_llvm_executionengine, use_llvm_target
+<*.{byte,native}>: use_llvm_scalar_opts, use_bindings
+</pre></div>
+</div>
+</dd>
+<dt>myocamlbuild.ml:</dt>
+<dd><div class="first last highlight-ocaml"><div class="highlight"><pre><span class="k">open</span> <span class="nc">Ocamlbuild_plugin</span><span class="o">;;</span>
+
+<span class="n">ocaml_lib</span> <span class="o">~</span><span class="n">extern</span><span class="o">:</span><span class="bp">true</span> <span class="s2">"llvm"</span><span class="o">;;</span>
+<span class="n">ocaml_lib</span> <span class="o">~</span><span class="n">extern</span><span class="o">:</span><span class="bp">true</span> <span class="s2">"llvm_analysis"</span><span class="o">;;</span>
+<span class="n">ocaml_lib</span> <span class="o">~</span><span class="n">extern</span><span class="o">:</span><span class="bp">true</span> <span class="s2">"llvm_executionengine"</span><span class="o">;;</span>
+<span class="n">ocaml_lib</span> <span class="o">~</span><span class="n">extern</span><span class="o">:</span><span class="bp">true</span> <span class="s2">"llvm_target"</span><span class="o">;;</span>
+<span class="n">ocaml_lib</span> <span class="o">~</span><span class="n">extern</span><span class="o">:</span><span class="bp">true</span> <span class="s2">"llvm_scalar_opts"</span><span class="o">;;</span>
+
+<span class="n">flag</span> <span class="o">[</span><span class="s2">"link"</span><span class="o">;</span> <span class="s2">"ocaml"</span><span class="o">;</span> <span class="s2">"g++"</span><span class="o">]</span> <span class="o">(</span><span class="nc">S</span><span class="o">[</span><span class="nc">A</span><span class="s2">"-cc"</span><span class="o">;</span> <span class="nc">A</span><span class="s2">"g++"</span><span class="o">;</span> <span class="nc">A</span><span class="s2">"-cclib"</span><span class="o">;</span> <span class="nc">A</span><span class="s2">"-rdynamic"</span><span class="o">]);;</span>
+<span class="n">dep</span> <span class="o">[</span><span class="s2">"link"</span><span class="o">;</span> <span class="s2">"ocaml"</span><span class="o">;</span> <span class="s2">"use_bindings"</span><span class="o">]</span> <span class="o">[</span><span class="s2">"bindings.o"</span><span class="o">];;</span>
+</pre></div>
+</div>
+</dd>
+<dt>token.ml:</dt>
+<dd><div class="first last highlight-ocaml"><div class="highlight"><pre><span class="c">(*===----------------------------------------------------------------------===</span>
+<span class="c"> * Lexer Tokens</span>
+<span class="c"> *===----------------------------------------------------------------------===*)</span>
+
+<span class="c">(* The lexer returns these 'Kwd' if it is an unknown character, otherwise one of</span>
+<span class="c"> * these others for known things. *)</span>
+<span class="k">type</span> <span class="n">token</span> <span class="o">=</span>
+  <span class="c">(* commands *)</span>
+  <span class="o">|</span> <span class="nc">Def</span> <span class="o">|</span> <span class="nc">Extern</span>
+
+  <span class="c">(* primary *)</span>
+  <span class="o">|</span> <span class="nc">Ident</span> <span class="k">of</span> <span class="kt">string</span> <span class="o">|</span> <span class="nc">Number</span> <span class="k">of</span> <span class="kt">float</span>
+
+  <span class="c">(* unknown *)</span>
+  <span class="o">|</span> <span class="nc">Kwd</span> <span class="k">of</span> <span class="kt">char</span>
+
+  <span class="c">(* control *)</span>
+  <span class="o">|</span> <span class="nc">If</span> <span class="o">|</span> <span class="nc">Then</span> <span class="o">|</span> <span class="nc">Else</span>
+  <span class="o">|</span> <span class="nc">For</span> <span class="o">|</span> <span class="nc">In</span>
+
+  <span class="c">(* operators *)</span>
+  <span class="o">|</span> <span class="nc">Binary</span> <span class="o">|</span> <span class="nc">Unary</span>
+</pre></div>
+</div>
+</dd>
+<dt>lexer.ml:</dt>
+<dd><div class="first last highlight-ocaml"><div class="highlight"><pre><span class="c">(*===----------------------------------------------------------------------===</span>
+<span class="c"> * Lexer</span>
+<span class="c"> *===----------------------------------------------------------------------===*)</span>
+
+<span class="k">let</span> <span class="k">rec</span> <span class="n">lex</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="c">(* Skip any whitespace. *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span> <span class="o">(</span><span class="sc">' '</span> <span class="o">|</span> <span class="sc">'\n'</span> <span class="o">|</span> <span class="sc">'\r'</span> <span class="o">|</span> <span class="sc">'\t'</span><span class="o">);</span> <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">lex</span> <span class="n">stream</span>
+
+  <span class="c">(* identifier: [a-zA-Z][a-zA-Z0-9] *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span> <span class="o">(</span><span class="sc">'A'</span> <span class="o">..</span> <span class="sc">'Z'</span> <span class="o">|</span> <span class="sc">'a'</span> <span class="o">..</span> <span class="sc">'z'</span> <span class="k">as</span> <span class="n">c</span><span class="o">);</span> <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="k">let</span> <span class="n">buffer</span> <span class="o">=</span> <span class="nn">Buffer</span><span class="p">.</span><span class="n">create</span> <span class="mi">1</span> <span class="k">in</span>
+      <span class="nn">Buffer</span><span class="p">.</span><span class="n">add_char</span> <span class="n">buffer</span> <span class="n">c</span><span class="o">;</span>
+      <span class="n">lex_ident</span> <span class="n">buffer</span> <span class="n">stream</span>
+
+  <span class="c">(* number: [0-9.]+ *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span> <span class="o">(</span><span class="sc">'0'</span> <span class="o">..</span> <span class="sc">'9'</span> <span class="k">as</span> <span class="n">c</span><span class="o">);</span> <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="k">let</span> <span class="n">buffer</span> <span class="o">=</span> <span class="nn">Buffer</span><span class="p">.</span><span class="n">create</span> <span class="mi">1</span> <span class="k">in</span>
+      <span class="nn">Buffer</span><span class="p">.</span><span class="n">add_char</span> <span class="n">buffer</span> <span class="n">c</span><span class="o">;</span>
+      <span class="n">lex_number</span> <span class="n">buffer</span> <span class="n">stream</span>
+
+  <span class="c">(* Comment until end of line. *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span> <span class="o">(</span><span class="sc">'#'</span><span class="o">);</span> <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="n">lex_comment</span> <span class="n">stream</span>
+
+  <span class="c">(* Otherwise, just return the character as its ascii value. *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="n">c</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="n">c</span><span class="o">;</span> <span class="n">lex</span> <span class="n">stream</span> <span class="o">>]</span>
+
+  <span class="c">(* end of stream. *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span> <span class="o">[<</span> <span class="o">>]</span>
+
+<span class="ow">and</span> <span class="n">lex_number</span> <span class="n">buffer</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span> <span class="o">(</span><span class="sc">'0'</span> <span class="o">..</span> <span class="sc">'9'</span> <span class="o">|</span> <span class="sc">'.'</span> <span class="k">as</span> <span class="n">c</span><span class="o">);</span> <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="nn">Buffer</span><span class="p">.</span><span class="n">add_char</span> <span class="n">buffer</span> <span class="n">c</span><span class="o">;</span>
+      <span class="n">lex_number</span> <span class="n">buffer</span> <span class="n">stream</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="n">stream</span><span class="o">=</span><span class="n">lex</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Number</span> <span class="o">(</span><span class="n">float_of_string</span> <span class="o">(</span><span class="nn">Buffer</span><span class="p">.</span><span class="n">contents</span> <span class="n">buffer</span><span class="o">));</span> <span class="n">stream</span> <span class="o">>]</span>
+
+<span class="ow">and</span> <span class="n">lex_ident</span> <span class="n">buffer</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span> <span class="o">(</span><span class="sc">'A'</span> <span class="o">..</span> <span class="sc">'Z'</span> <span class="o">|</span> <span class="sc">'a'</span> <span class="o">..</span> <span class="sc">'z'</span> <span class="o">|</span> <span class="sc">'0'</span> <span class="o">..</span> <span class="sc">'9'</span> <span class="k">as</span> <span class="n">c</span><span class="o">);</span> <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="nn">Buffer</span><span class="p">.</span><span class="n">add_char</span> <span class="n">buffer</span> <span class="n">c</span><span class="o">;</span>
+      <span class="n">lex_ident</span> <span class="n">buffer</span> <span class="n">stream</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="n">stream</span><span class="o">=</span><span class="n">lex</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="k">match</span> <span class="nn">Buffer</span><span class="p">.</span><span class="n">contents</span> <span class="n">buffer</span> <span class="k">with</span>
+      <span class="o">|</span> <span class="s2">"def"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Def</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+      <span class="o">|</span> <span class="s2">"extern"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Extern</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+      <span class="o">|</span> <span class="s2">"if"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">If</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+      <span class="o">|</span> <span class="s2">"then"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Then</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+      <span class="o">|</span> <span class="s2">"else"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Else</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+      <span class="o">|</span> <span class="s2">"for"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">For</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+      <span class="o">|</span> <span class="s2">"in"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">In</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+      <span class="o">|</span> <span class="s2">"binary"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Binary</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+      <span class="o">|</span> <span class="s2">"unary"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Unary</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+      <span class="o">|</span> <span class="n">id</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Ident</span> <span class="n">id</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+
+<span class="ow">and</span> <span class="n">lex_comment</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span> <span class="o">(</span><span class="sc">'\n'</span><span class="o">);</span> <span class="n">stream</span><span class="o">=</span><span class="n">lex</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">stream</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="n">c</span><span class="o">;</span> <span class="n">e</span><span class="o">=</span><span class="n">lex_comment</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">e</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span> <span class="o">[<</span> <span class="o">>]</span>
+</pre></div>
+</div>
+</dd>
+<dt>ast.ml:</dt>
+<dd><div class="first last highlight-ocaml"><div class="highlight"><pre><span class="c">(*===----------------------------------------------------------------------===</span>
+<span class="c"> * Abstract Syntax Tree (aka Parse Tree)</span>
+<span class="c"> *===----------------------------------------------------------------------===*)</span>
+
+<span class="c">(* expr - Base type for all expression nodes. *)</span>
+<span class="k">type</span> <span class="n">expr</span> <span class="o">=</span>
+  <span class="c">(* variant for numeric literals like "1.0". *)</span>
+  <span class="o">|</span> <span class="nc">Number</span> <span class="k">of</span> <span class="kt">float</span>
+
+  <span class="c">(* variant for referencing a variable, like "a". *)</span>
+  <span class="o">|</span> <span class="nc">Variable</span> <span class="k">of</span> <span class="kt">string</span>
+
+  <span class="c">(* variant for a unary operator. *)</span>
+  <span class="o">|</span> <span class="nc">Unary</span> <span class="k">of</span> <span class="kt">char</span> <span class="o">*</span> <span class="n">expr</span>
+
+  <span class="c">(* variant for a binary operator. *)</span>
+  <span class="o">|</span> <span class="nc">Binary</span> <span class="k">of</span> <span class="kt">char</span> <span class="o">*</span> <span class="n">expr</span> <span class="o">*</span> <span class="n">expr</span>
+
+  <span class="c">(* variant for function calls. *)</span>
+  <span class="o">|</span> <span class="nc">Call</span> <span class="k">of</span> <span class="kt">string</span> <span class="o">*</span> <span class="n">expr</span> <span class="kt">array</span>
+
+  <span class="c">(* variant for if/then/else. *)</span>
+  <span class="o">|</span> <span class="nc">If</span> <span class="k">of</span> <span class="n">expr</span> <span class="o">*</span> <span class="n">expr</span> <span class="o">*</span> <span class="n">expr</span>
+
+  <span class="c">(* variant for for/in. *)</span>
+  <span class="o">|</span> <span class="nc">For</span> <span class="k">of</span> <span class="kt">string</span> <span class="o">*</span> <span class="n">expr</span> <span class="o">*</span> <span class="n">expr</span> <span class="o">*</span> <span class="n">expr</span> <span class="n">option</span> <span class="o">*</span> <span class="n">expr</span>
+
+<span class="c">(* proto - This type represents the "prototype" for a function, which captures</span>
+<span class="c"> * its name, and its argument names (thus implicitly the number of arguments the</span>
+<span class="c"> * function takes). *)</span>
+<span class="k">type</span> <span class="n">proto</span> <span class="o">=</span>
+  <span class="o">|</span> <span class="nc">Prototype</span> <span class="k">of</span> <span class="kt">string</span> <span class="o">*</span> <span class="kt">string</span> <span class="kt">array</span>
+  <span class="o">|</span> <span class="nc">BinOpPrototype</span> <span class="k">of</span> <span class="kt">string</span> <span class="o">*</span> <span class="kt">string</span> <span class="kt">array</span> <span class="o">*</span> <span class="kt">int</span>
+
+<span class="c">(* func - This type represents a function definition itself. *)</span>
+<span class="k">type</span> <span class="n">func</span> <span class="o">=</span> <span class="nc">Function</span> <span class="k">of</span> <span class="n">proto</span> <span class="o">*</span> <span class="n">expr</span>
+</pre></div>
+</div>
+</dd>
+<dt>parser.ml:</dt>
+<dd><div class="first last highlight-ocaml"><div class="highlight"><pre><span class="c">(*===---------------------------------------------------------------------===</span>
+<span class="c"> * Parser</span>
+<span class="c"> *===---------------------------------------------------------------------===*)</span>
+
+<span class="c">(* binop_precedence - This holds the precedence for each binary operator that is</span>
+<span class="c"> * defined *)</span>
+<span class="k">let</span> <span class="n">binop_precedence</span><span class="o">:(</span><span class="kt">char</span><span class="o">,</span> <span class="kt">int</span><span class="o">)</span> <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">t</span> <span class="o">=</span> <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">create</span> <span class="mi">10</span>
+
+<span class="c">(* precedence - Get the precedence of the pending binary operator token. *)</span>
+<span class="k">let</span> <span class="n">precedence</span> <span class="n">c</span> <span class="o">=</span> <span class="k">try</span> <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">find</span> <span class="n">binop_precedence</span> <span class="n">c</span> <span class="k">with</span> <span class="nc">Not_found</span> <span class="o">-></span> <span class="o">-</span><span class="mi">1</span>
+
+<span class="c">(* primary</span>
+<span class="c"> *   ::= identifier</span>
+<span class="c"> *   ::= numberexpr</span>
+<span class="c"> *   ::= parenexpr</span>
+<span class="c"> *   ::= ifexpr</span>
+<span class="c"> *   ::= forexpr *)</span>
+<span class="k">let</span> <span class="k">rec</span> <span class="n">parse_primary</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="c">(* numberexpr ::= number *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Number</span> <span class="n">n</span> <span class="o">>]</span> <span class="o">-></span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Number</span> <span class="n">n</span>
+
+  <span class="c">(* parenexpr ::= '(' expression ')' *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">'('</span><span class="o">;</span> <span class="n">e</span><span class="o">=</span><span class="n">parse_expr</span><span class="o">;</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">')'</span> <span class="o">??</span> <span class="s2">"expected ')'"</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">e</span>
+
+  <span class="c">(* identifierexpr</span>
+<span class="c">   *   ::= identifier</span>
+<span class="c">   *   ::= identifier '(' argumentexpr ')' *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Ident</span> <span class="n">id</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="k">let</span> <span class="k">rec</span> <span class="n">parse_args</span> <span class="n">accumulator</span> <span class="o">=</span> <span class="n">parser</span>
+        <span class="o">|</span> <span class="o">[<</span> <span class="n">e</span><span class="o">=</span><span class="n">parse_expr</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span>
+            <span class="k">begin</span> <span class="n">parser</span>
+              <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">','</span><span class="o">;</span> <span class="n">e</span><span class="o">=</span><span class="n">parse_args</span> <span class="o">(</span><span class="n">e</span> <span class="o">::</span> <span class="n">accumulator</span><span class="o">)</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">e</span>
+              <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">e</span> <span class="o">::</span> <span class="n">accumulator</span>
+            <span class="k">end</span> <span class="n">stream</span>
+        <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">accumulator</span>
+      <span class="k">in</span>
+      <span class="k">let</span> <span class="k">rec</span> <span class="n">parse_ident</span> <span class="n">id</span> <span class="o">=</span> <span class="n">parser</span>
+        <span class="c">(* Call. *)</span>
+        <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">'('</span><span class="o">;</span>
+             <span class="n">args</span><span class="o">=</span><span class="n">parse_args</span> <span class="bp">[]</span><span class="o">;</span>
+             <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">')'</span> <span class="o">??</span> <span class="s2">"expected ')'"</span><span class="o">>]</span> <span class="o">-></span>
+            <span class="nn">Ast</span><span class="p">.</span><span class="nc">Call</span> <span class="o">(</span><span class="n">id</span><span class="o">,</span> <span class="nn">Array</span><span class="p">.</span><span class="n">of_list</span> <span class="o">(</span><span class="nn">List</span><span class="p">.</span><span class="n">rev</span> <span class="n">args</span><span class="o">))</span>
+
+        <span class="c">(* Simple variable ref. *)</span>
+        <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Variable</span> <span class="n">id</span>
+      <span class="k">in</span>
+      <span class="n">parse_ident</span> <span class="n">id</span> <span class="n">stream</span>
+
+  <span class="c">(* ifexpr ::= 'if' expr 'then' expr 'else' expr *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">If</span><span class="o">;</span> <span class="n">c</span><span class="o">=</span><span class="n">parse_expr</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Then</span> <span class="o">??</span> <span class="s2">"expected 'then'"</span><span class="o">;</span> <span class="n">t</span><span class="o">=</span><span class="n">parse_expr</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Else</span> <span class="o">??</span> <span class="s2">"expected 'else'"</span><span class="o">;</span> <span class="n">e</span><span class="o">=</span><span class="n">parse_expr</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="nn">Ast</span><span class="p">.</span><span class="nc">If</span> <span class="o">(</span><span class="n">c</span><span class="o">,</span> <span class="n">t</span><span class="o">,</span> <span class="n">e</span><span class="o">)</span>
+
+  <span class="c">(* forexpr</span>
+<span class="c">        ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">For</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Ident</span> <span class="n">id</span> <span class="o">??</span> <span class="s2">"expected identifier after for"</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">'='</span> <span class="o">??</span> <span class="s2">"expected '=' after for"</span><span class="o">;</span>
+       <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="k">begin</span> <span class="n">parser</span>
+        <span class="o">|</span> <span class="o">[<</span>
+             <span class="n">start</span><span class="o">=</span><span class="n">parse_expr</span><span class="o">;</span>
+             <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">','</span> <span class="o">??</span> <span class="s2">"expected ',' after for"</span><span class="o">;</span>
+             <span class="n">end_</span><span class="o">=</span><span class="n">parse_expr</span><span class="o">;</span>
+             <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span>
+            <span class="k">let</span> <span class="n">step</span> <span class="o">=</span>
+              <span class="k">begin</span> <span class="n">parser</span>
+              <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">','</span><span class="o">;</span> <span class="n">step</span><span class="o">=</span><span class="n">parse_expr</span> <span class="o">>]</span> <span class="o">-></span> <span class="nc">Some</span> <span class="n">step</span>
+              <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span> <span class="nc">None</span>
+              <span class="k">end</span> <span class="n">stream</span>
+            <span class="k">in</span>
+            <span class="k">begin</span> <span class="n">parser</span>
+            <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">In</span><span class="o">;</span> <span class="n">body</span><span class="o">=</span><span class="n">parse_expr</span> <span class="o">>]</span> <span class="o">-></span>
+                <span class="nn">Ast</span><span class="p">.</span><span class="nc">For</span> <span class="o">(</span><span class="n">id</span><span class="o">,</span> <span class="n">start</span><span class="o">,</span> <span class="n">end_</span><span class="o">,</span> <span class="n">step</span><span class="o">,</span> <span class="n">body</span><span class="o">)</span>
+            <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span>
+                <span class="k">raise</span> <span class="o">(</span><span class="nn">Stream</span><span class="p">.</span><span class="nc">Error</span> <span class="s2">"expected 'in' after for"</span><span class="o">)</span>
+            <span class="k">end</span> <span class="n">stream</span>
+        <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span>
+            <span class="k">raise</span> <span class="o">(</span><span class="nn">Stream</span><span class="p">.</span><span class="nc">Error</span> <span class="s2">"expected '=' after for"</span><span class="o">)</span>
+      <span class="k">end</span> <span class="n">stream</span>
+
+  <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span> <span class="k">raise</span> <span class="o">(</span><span class="nn">Stream</span><span class="p">.</span><span class="nc">Error</span> <span class="s2">"unknown token when expecting an expression."</span><span class="o">)</span>
+
+<span class="c">(* unary</span>
+<span class="c"> *   ::= primary</span>
+<span class="c"> *   ::= '!' unary *)</span>
+<span class="ow">and</span> <span class="n">parse_unary</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="c">(* If this is a unary operator, read it. *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="n">op</span> <span class="k">when</span> <span class="n">op</span> <span class="o">!=</span> <span class="sc">'('</span> <span class="o">&&</span> <span class="n">op</span> <span class="o">!=</span> <span class="sc">')'</span><span class="o">;</span> <span class="n">operand</span><span class="o">=</span><span class="n">parse_expr</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="nn">Ast</span><span class="p">.</span><span class="nc">Unary</span> <span class="o">(</span><span class="n">op</span><span class="o">,</span> <span class="n">operand</span><span class="o">)</span>
+
+  <span class="c">(* If the current token is not an operator, it must be a primary expr. *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">parse_primary</span> <span class="n">stream</span>
+
+<span class="c">(* binoprhs</span>
+<span class="c"> *   ::= ('+' primary)* *)</span>
+<span class="ow">and</span> <span class="n">parse_bin_rhs</span> <span class="n">expr_prec</span> <span class="n">lhs</span> <span class="n">stream</span> <span class="o">=</span>
+  <span class="k">match</span> <span class="nn">Stream</span><span class="p">.</span><span class="n">peek</span> <span class="n">stream</span> <span class="k">with</span>
+  <span class="c">(* If this is a binop, find its precedence. *)</span>
+  <span class="o">|</span> <span class="nc">Some</span> <span class="o">(</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="n">c</span><span class="o">)</span> <span class="k">when</span> <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">mem</span> <span class="n">binop_precedence</span> <span class="n">c</span> <span class="o">-></span>
+      <span class="k">let</span> <span class="n">token_prec</span> <span class="o">=</span> <span class="n">precedence</span> <span class="n">c</span> <span class="k">in</span>
+
+      <span class="c">(* If this is a binop that binds at least as tightly as the current binop,</span>
+<span class="c">       * consume it, otherwise we are done. *)</span>
+      <span class="k">if</span> <span class="n">token_prec</span> <span class="o"><</span> <span class="n">expr_prec</span> <span class="k">then</span> <span class="n">lhs</span> <span class="k">else</span> <span class="k">begin</span>
+        <span class="c">(* Eat the binop. *)</span>
+        <span class="nn">Stream</span><span class="p">.</span><span class="n">junk</span> <span class="n">stream</span><span class="o">;</span>
+
+        <span class="c">(* Parse the unary expression after the binary operator. *)</span>
+        <span class="k">let</span> <span class="n">rhs</span> <span class="o">=</span> <span class="n">parse_unary</span> <span class="n">stream</span> <span class="k">in</span>
+
+        <span class="c">(* Okay, we know this is a binop. *)</span>
+        <span class="k">let</span> <span class="n">rhs</span> <span class="o">=</span>
+          <span class="k">match</span> <span class="nn">Stream</span><span class="p">.</span><span class="n">peek</span> <span class="n">stream</span> <span class="k">with</span>
+          <span class="o">|</span> <span class="nc">Some</span> <span class="o">(</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="n">c2</span><span class="o">)</span> <span class="o">-></span>
+              <span class="c">(* If BinOp binds less tightly with rhs than the operator after</span>
+<span class="c">               * rhs, let the pending operator take rhs as its lhs. *)</span>
+              <span class="k">let</span> <span class="n">next_prec</span> <span class="o">=</span> <span class="n">precedence</span> <span class="n">c2</span> <span class="k">in</span>
+              <span class="k">if</span> <span class="n">token_prec</span> <span class="o"><</span> <span class="n">next_prec</span>
+              <span class="k">then</span> <span class="n">parse_bin_rhs</span> <span class="o">(</span><span class="n">token_prec</span> <span class="o">+</span> <span class="mi">1</span><span class="o">)</span> <span class="n">rhs</span> <span class="n">stream</span>
+              <span class="k">else</span> <span class="n">rhs</span>
+          <span class="o">|</span> <span class="o">_</span> <span class="o">-></span> <span class="n">rhs</span>
+        <span class="k">in</span>
+
+        <span class="c">(* Merge lhs/rhs. *)</span>
+        <span class="k">let</span> <span class="n">lhs</span> <span class="o">=</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Binary</span> <span class="o">(</span><span class="n">c</span><span class="o">,</span> <span class="n">lhs</span><span class="o">,</span> <span class="n">rhs</span><span class="o">)</span> <span class="k">in</span>
+        <span class="n">parse_bin_rhs</span> <span class="n">expr_prec</span> <span class="n">lhs</span> <span class="n">stream</span>
+      <span class="k">end</span>
+  <span class="o">|</span> <span class="o">_</span> <span class="o">-></span> <span class="n">lhs</span>
+
+<span class="c">(* expression</span>
+<span class="c"> *   ::= primary binoprhs *)</span>
+<span class="ow">and</span> <span class="n">parse_expr</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="n">lhs</span><span class="o">=</span><span class="n">parse_unary</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">parse_bin_rhs</span> <span class="mi">0</span> <span class="n">lhs</span> <span class="n">stream</span>
+
+<span class="c">(* prototype</span>
+<span class="c"> *   ::= id '(' id* ')'</span>
+<span class="c"> *   ::= binary LETTER number? (id, id)</span>
+<span class="c"> *   ::= unary LETTER number? (id) *)</span>
+<span class="k">let</span> <span class="n">parse_prototype</span> <span class="o">=</span>
+  <span class="k">let</span> <span class="k">rec</span> <span class="n">parse_args</span> <span class="n">accumulator</span> <span class="o">=</span> <span class="n">parser</span>
+    <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Ident</span> <span class="n">id</span><span class="o">;</span> <span class="n">e</span><span class="o">=</span><span class="n">parse_args</span> <span class="o">(</span><span class="n">id</span><span class="o">::</span><span class="n">accumulator</span><span class="o">)</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">e</span>
+    <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">accumulator</span>
+  <span class="k">in</span>
+  <span class="k">let</span> <span class="n">parse_operator</span> <span class="o">=</span> <span class="n">parser</span>
+    <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Unary</span> <span class="o">>]</span> <span class="o">-></span> <span class="s2">"unary"</span><span class="o">,</span> <span class="mi">1</span>
+    <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Binary</span> <span class="o">>]</span> <span class="o">-></span> <span class="s2">"binary"</span><span class="o">,</span> <span class="mi">2</span>
+  <span class="k">in</span>
+  <span class="k">let</span> <span class="n">parse_binary_precedence</span> <span class="o">=</span> <span class="n">parser</span>
+    <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Number</span> <span class="n">n</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">int_of_float</span> <span class="n">n</span>
+    <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span> <span class="mi">30</span>
+  <span class="k">in</span>
+  <span class="n">parser</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Ident</span> <span class="n">id</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">'('</span> <span class="o">??</span> <span class="s2">"expected '(' in prototype"</span><span class="o">;</span>
+       <span class="n">args</span><span class="o">=</span><span class="n">parse_args</span> <span class="bp">[]</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">')'</span> <span class="o">??</span> <span class="s2">"expected ')' in prototype"</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="c">(* success. *)</span>
+      <span class="nn">Ast</span><span class="p">.</span><span class="nc">Prototype</span> <span class="o">(</span><span class="n">id</span><span class="o">,</span> <span class="nn">Array</span><span class="p">.</span><span class="n">of_list</span> <span class="o">(</span><span class="nn">List</span><span class="p">.</span><span class="n">rev</span> <span class="n">args</span><span class="o">))</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="o">(</span><span class="n">prefix</span><span class="o">,</span> <span class="n">kind</span><span class="o">)=</span><span class="n">parse_operator</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="n">op</span> <span class="o">??</span> <span class="s2">"expected an operator"</span><span class="o">;</span>
+       <span class="c">(* Read the precedence if present. *)</span>
+       <span class="n">binary_precedence</span><span class="o">=</span><span class="n">parse_binary_precedence</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">'('</span> <span class="o">??</span> <span class="s2">"expected '(' in prototype"</span><span class="o">;</span>
+        <span class="n">args</span><span class="o">=</span><span class="n">parse_args</span> <span class="bp">[]</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">')'</span> <span class="o">??</span> <span class="s2">"expected ')' in prototype"</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="k">let</span> <span class="n">name</span> <span class="o">=</span> <span class="n">prefix</span> <span class="o">^</span> <span class="o">(</span><span class="nn">String</span><span class="p">.</span><span class="n">make</span> <span class="mi">1</span> <span class="n">op</span><span class="o">)</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">args</span> <span class="o">=</span> <span class="nn">Array</span><span class="p">.</span><span class="n">of_list</span> <span class="o">(</span><span class="nn">List</span><span class="p">.</span><span class="n">rev</span> <span class="n">args</span><span class="o">)</span> <span class="k">in</span>
+
+      <span class="c">(* Verify right number of arguments for operator. *)</span>
+      <span class="k">if</span> <span class="nn">Array</span><span class="p">.</span><span class="n">length</span> <span class="n">args</span> <span class="o">!=</span> <span class="n">kind</span>
+      <span class="k">then</span> <span class="k">raise</span> <span class="o">(</span><span class="nn">Stream</span><span class="p">.</span><span class="nc">Error</span> <span class="s2">"invalid number of operands for operator"</span><span class="o">)</span>
+      <span class="k">else</span>
+        <span class="k">if</span> <span class="n">kind</span> <span class="o">==</span> <span class="mi">1</span> <span class="k">then</span>
+          <span class="nn">Ast</span><span class="p">.</span><span class="nc">Prototype</span> <span class="o">(</span><span class="n">name</span><span class="o">,</span> <span class="n">args</span><span class="o">)</span>
+        <span class="k">else</span>
+          <span class="nn">Ast</span><span class="p">.</span><span class="nc">BinOpPrototype</span> <span class="o">(</span><span class="n">name</span><span class="o">,</span> <span class="n">args</span><span class="o">,</span> <span class="n">binary_precedence</span><span class="o">)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="k">raise</span> <span class="o">(</span><span class="nn">Stream</span><span class="p">.</span><span class="nc">Error</span> <span class="s2">"expected function name in prototype"</span><span class="o">)</span>
+
+<span class="c">(* definition ::= 'def' prototype expression *)</span>
+<span class="k">let</span> <span class="n">parse_definition</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Def</span><span class="o">;</span> <span class="n">p</span><span class="o">=</span><span class="n">parse_prototype</span><span class="o">;</span> <span class="n">e</span><span class="o">=</span><span class="n">parse_expr</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="nn">Ast</span><span class="p">.</span><span class="nc">Function</span> <span class="o">(</span><span class="n">p</span><span class="o">,</span> <span class="n">e</span><span class="o">)</span>
+
+<span class="c">(* toplevelexpr ::= expression *)</span>
+<span class="k">let</span> <span class="n">parse_toplevel</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="n">e</span><span class="o">=</span><span class="n">parse_expr</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="c">(* Make an anonymous proto. *)</span>
+      <span class="nn">Ast</span><span class="p">.</span><span class="nc">Function</span> <span class="o">(</span><span class="nn">Ast</span><span class="p">.</span><span class="nc">Prototype</span> <span class="o">(</span><span class="s2">""</span><span class="o">,</span> <span class="o">[||]),</span> <span class="n">e</span><span class="o">)</span>
+
+<span class="c">(*  external ::= 'extern' prototype *)</span>
+<span class="k">let</span> <span class="n">parse_extern</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Extern</span><span class="o">;</span> <span class="n">e</span><span class="o">=</span><span class="n">parse_prototype</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">e</span>
+</pre></div>
+</div>
+</dd>
+<dt>codegen.ml:</dt>
+<dd><div class="first last highlight-ocaml"><div class="highlight"><pre><span class="c">(*===----------------------------------------------------------------------===</span>
+<span class="c"> * Code Generation</span>
+<span class="c"> *===----------------------------------------------------------------------===*)</span>
+
+<span class="k">open</span> <span class="nc">Llvm</span>
+
+<span class="k">exception</span> <span class="nc">Error</span> <span class="k">of</span> <span class="kt">string</span>
+
+<span class="k">let</span> <span class="n">context</span> <span class="o">=</span> <span class="n">global_context</span> <span class="bp">()</span>
+<span class="k">let</span> <span class="n">the_module</span> <span class="o">=</span> <span class="n">create_module</span> <span class="n">context</span> <span class="s2">"my cool jit"</span>
+<span class="k">let</span> <span class="n">builder</span> <span class="o">=</span> <span class="n">builder</span> <span class="n">context</span>
+<span class="k">let</span> <span class="n">named_values</span><span class="o">:(</span><span class="kt">string</span><span class="o">,</span> <span class="n">llvalue</span><span class="o">)</span> <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">t</span> <span class="o">=</span> <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">create</span> <span class="mi">10</span>
+<span class="k">let</span> <span class="n">double_type</span> <span class="o">=</span> <span class="n">double_type</span> <span class="n">context</span>
+
+<span class="k">let</span> <span class="k">rec</span> <span class="n">codegen_expr</span> <span class="o">=</span> <span class="k">function</span>
+  <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Number</span> <span class="n">n</span> <span class="o">-></span> <span class="n">const_float</span> <span class="n">double_type</span> <span class="n">n</span>
+  <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Variable</span> <span class="n">name</span> <span class="o">-></span>
+      <span class="o">(</span><span class="k">try</span> <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">find</span> <span class="n">named_values</span> <span class="n">name</span> <span class="k">with</span>
+        <span class="o">|</span> <span class="nc">Not_found</span> <span class="o">-></span> <span class="k">raise</span> <span class="o">(</span><span class="nc">Error</span> <span class="s2">"unknown variable name"</span><span class="o">))</span>
+  <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Unary</span> <span class="o">(</span><span class="n">op</span><span class="o">,</span> <span class="n">operand</span><span class="o">)</span> <span class="o">-></span>
+      <span class="k">let</span> <span class="n">operand</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">operand</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">callee</span> <span class="o">=</span> <span class="s2">"unary"</span> <span class="o">^</span> <span class="o">(</span><span class="nn">String</span><span class="p">.</span><span class="n">make</span> <span class="mi">1</span> <span class="n">op</span><span class="o">)</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">callee</span> <span class="o">=</span>
+        <span class="k">match</span> <span class="n">lookup_function</span> <span class="n">callee</span> <span class="n">the_module</span> <span class="k">with</span>
+        <span class="o">|</span> <span class="nc">Some</span> <span class="n">callee</span> <span class="o">-></span> <span class="n">callee</span>
+        <span class="o">|</span> <span class="nc">None</span> <span class="o">-></span> <span class="k">raise</span> <span class="o">(</span><span class="nc">Error</span> <span class="s2">"unknown unary operator"</span><span class="o">)</span>
+      <span class="k">in</span>
+      <span class="n">build_call</span> <span class="n">callee</span> <span class="o">[|</span><span class="n">operand</span><span class="o">|]</span> <span class="s2">"unop"</span> <span class="n">builder</span>
+  <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Binary</span> <span class="o">(</span><span class="n">op</span><span class="o">,</span> <span class="n">lhs</span><span class="o">,</span> <span class="n">rhs</span><span class="o">)</span> <span class="o">-></span>
+      <span class="k">let</span> <span class="n">lhs_val</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">lhs</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">rhs_val</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">rhs</span> <span class="k">in</span>
+      <span class="k">begin</span>
+        <span class="k">match</span> <span class="n">op</span> <span class="k">with</span>
+        <span class="o">|</span> <span class="sc">'+'</span> <span class="o">-></span> <span class="n">build_add</span> <span class="n">lhs_val</span> <span class="n">rhs_val</span> <span class="s2">"addtmp"</span> <span class="n">builder</span>
+        <span class="o">|</span> <span class="sc">'-'</span> <span class="o">-></span> <span class="n">build_sub</span> <span class="n">lhs_val</span> <span class="n">rhs_val</span> <span class="s2">"subtmp"</span> <span class="n">builder</span>
+        <span class="o">|</span> <span class="sc">'*'</span> <span class="o">-></span> <span class="n">build_mul</span> <span class="n">lhs_val</span> <span class="n">rhs_val</span> <span class="s2">"multmp"</span> <span class="n">builder</span>
+        <span class="o">|</span> <span class="sc">'<'</span> <span class="o">-></span>
+            <span class="c">(* Convert bool 0/1 to double 0.0 or 1.0 *)</span>
+            <span class="k">let</span> <span class="n">i</span> <span class="o">=</span> <span class="n">build_fcmp</span> <span class="nn">Fcmp</span><span class="p">.</span><span class="nc">Ult</span> <span class="n">lhs_val</span> <span class="n">rhs_val</span> <span class="s2">"cmptmp"</span> <span class="n">builder</span> <span class="k">in</span>
+            <span class="n">build_uitofp</span> <span class="n">i</span> <span class="n">double_type</span> <span class="s2">"booltmp"</span> <span class="n">builder</span>
+        <span class="o">|</span> <span class="o">_</span> <span class="o">-></span>
+            <span class="c">(* If it wasn't a builtin binary operator, it must be a user defined</span>
+<span class="c">             * one. Emit a call to it. *)</span>
+            <span class="k">let</span> <span class="n">callee</span> <span class="o">=</span> <span class="s2">"binary"</span> <span class="o">^</span> <span class="o">(</span><span class="nn">String</span><span class="p">.</span><span class="n">make</span> <span class="mi">1</span> <span class="n">op</span><span class="o">)</span> <span class="k">in</span>
+            <span class="k">let</span> <span class="n">callee</span> <span class="o">=</span>
+              <span class="k">match</span> <span class="n">lookup_function</span> <span class="n">callee</span> <span class="n">the_module</span> <span class="k">with</span>
+              <span class="o">|</span> <span class="nc">Some</span> <span class="n">callee</span> <span class="o">-></span> <span class="n">callee</span>
+              <span class="o">|</span> <span class="nc">None</span> <span class="o">-></span> <span class="k">raise</span> <span class="o">(</span><span class="nc">Error</span> <span class="s2">"binary operator not found!"</span><span class="o">)</span>
+            <span class="k">in</span>
+            <span class="n">build_call</span> <span class="n">callee</span> <span class="o">[|</span><span class="n">lhs_val</span><span class="o">;</span> <span class="n">rhs_val</span><span class="o">|]</span> <span class="s2">"binop"</span> <span class="n">builder</span>
+      <span class="k">end</span>
+  <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Call</span> <span class="o">(</span><span class="n">callee</span><span class="o">,</span> <span class="n">args</span><span class="o">)</span> <span class="o">-></span>
+      <span class="c">(* Look up the name in the module table. *)</span>
+      <span class="k">let</span> <span class="n">callee</span> <span class="o">=</span>
+        <span class="k">match</span> <span class="n">lookup_function</span> <span class="n">callee</span> <span class="n">the_module</span> <span class="k">with</span>
+        <span class="o">|</span> <span class="nc">Some</span> <span class="n">callee</span> <span class="o">-></span> <span class="n">callee</span>
+        <span class="o">|</span> <span class="nc">None</span> <span class="o">-></span> <span class="k">raise</span> <span class="o">(</span><span class="nc">Error</span> <span class="s2">"unknown function referenced"</span><span class="o">)</span>
+      <span class="k">in</span>
+      <span class="k">let</span> <span class="n">params</span> <span class="o">=</span> <span class="n">params</span> <span class="n">callee</span> <span class="k">in</span>
+
+      <span class="c">(* If argument mismatch error. *)</span>
+      <span class="k">if</span> <span class="nn">Array</span><span class="p">.</span><span class="n">length</span> <span class="n">params</span> <span class="o">==</span> <span class="nn">Array</span><span class="p">.</span><span class="n">length</span> <span class="n">args</span> <span class="k">then</span> <span class="bp">()</span> <span class="k">else</span>
+        <span class="k">raise</span> <span class="o">(</span><span class="nc">Error</span> <span class="s2">"incorrect # arguments passed"</span><span class="o">);</span>
+      <span class="k">let</span> <span class="n">args</span> <span class="o">=</span> <span class="nn">Array</span><span class="p">.</span><span class="n">map</span> <span class="n">codegen_expr</span> <span class="n">args</span> <span class="k">in</span>
+      <span class="n">build_call</span> <span class="n">callee</span> <span class="n">args</span> <span class="s2">"calltmp"</span> <span class="n">builder</span>
+  <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">If</span> <span class="o">(</span><span class="n">cond</span><span class="o">,</span> <span class="n">then_</span><span class="o">,</span> <span class="n">else_</span><span class="o">)</span> <span class="o">-></span>
+      <span class="k">let</span> <span class="n">cond</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">cond</span> <span class="k">in</span>
+
+      <span class="c">(* Convert condition to a bool by comparing equal to 0.0 *)</span>
+      <span class="k">let</span> <span class="n">zero</span> <span class="o">=</span> <span class="n">const_float</span> <span class="n">double_type</span> <span class="mi">0</span><span class="o">.</span><span class="mi">0</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">cond_val</span> <span class="o">=</span> <span class="n">build_fcmp</span> <span class="nn">Fcmp</span><span class="p">.</span><span class="nc">One</span> <span class="n">cond</span> <span class="n">zero</span> <span class="s2">"ifcond"</span> <span class="n">builder</span> <span class="k">in</span>
+
+      <span class="c">(* Grab the first block so that we might later add the conditional branch</span>
+<span class="c">       * to it at the end of the function. *)</span>
+      <span class="k">let</span> <span class="n">start_bb</span> <span class="o">=</span> <span class="n">insertion_block</span> <span class="n">builder</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">the_function</span> <span class="o">=</span> <span class="n">block_parent</span> <span class="n">start_bb</span> <span class="k">in</span>
+
+      <span class="k">let</span> <span class="n">then_bb</span> <span class="o">=</span> <span class="n">append_block</span> <span class="n">context</span> <span class="s2">"then"</span> <span class="n">the_function</span> <span class="k">in</span>
+
+      <span class="c">(* Emit 'then' value. *)</span>
+      <span class="n">position_at_end</span> <span class="n">then_bb</span> <span class="n">builder</span><span class="o">;</span>
+      <span class="k">let</span> <span class="n">then_val</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">then_</span> <span class="k">in</span>
+
+      <span class="c">(* Codegen of 'then' can change the current block, update then_bb for the</span>
+<span class="c">       * phi. We create a new name because one is used for the phi node, and the</span>
+<span class="c">       * other is used for the conditional branch. *)</span>
+      <span class="k">let</span> <span class="n">new_then_bb</span> <span class="o">=</span> <span class="n">insertion_block</span> <span class="n">builder</span> <span class="k">in</span>
+
+      <span class="c">(* Emit 'else' value. *)</span>
+      <span class="k">let</span> <span class="n">else_bb</span> <span class="o">=</span> <span class="n">append_block</span> <span class="n">context</span> <span class="s2">"else"</span> <span class="n">the_function</span> <span class="k">in</span>
+      <span class="n">position_at_end</span> <span class="n">else_bb</span> <span class="n">builder</span><span class="o">;</span>
+      <span class="k">let</span> <span class="n">else_val</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">else_</span> <span class="k">in</span>
+
+      <span class="c">(* Codegen of 'else' can change the current block, update else_bb for the</span>
+<span class="c">       * phi. *)</span>
+      <span class="k">let</span> <span class="n">new_else_bb</span> <span class="o">=</span> <span class="n">insertion_block</span> <span class="n">builder</span> <span class="k">in</span>
+
+      <span class="c">(* Emit merge block. *)</span>
+      <span class="k">let</span> <span class="n">merge_bb</span> <span class="o">=</span> <span class="n">append_block</span> <span class="n">context</span> <span class="s2">"ifcont"</span> <span class="n">the_function</span> <span class="k">in</span>
+      <span class="n">position_at_end</span> <span class="n">merge_bb</span> <span class="n">builder</span><span class="o">;</span>
+      <span class="k">let</span> <span class="n">incoming</span> <span class="o">=</span> <span class="o">[(</span><span class="n">then_val</span><span class="o">,</span> <span class="n">new_then_bb</span><span class="o">);</span> <span class="o">(</span><span class="n">else_val</span><span class="o">,</span> <span class="n">new_else_bb</span><span class="o">)]</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">phi</span> <span class="o">=</span> <span class="n">build_phi</span> <span class="n">incoming</span> <span class="s2">"iftmp"</span> <span class="n">builder</span> <span class="k">in</span>
+
+      <span class="c">(* Return to the start block to add the conditional branch. *)</span>
+      <span class="n">position_at_end</span> <span class="n">start_bb</span> <span class="n">builder</span><span class="o">;</span>
+      <span class="n">ignore</span> <span class="o">(</span><span class="n">build_cond_br</span> <span class="n">cond_val</span> <span class="n">then_bb</span> <span class="n">else_bb</span> <span class="n">builder</span><span class="o">);</span>
+
+      <span class="c">(* Set a unconditional branch at the end of the 'then' block and the</span>
+<span class="c">       * 'else' block to the 'merge' block. *)</span>
+      <span class="n">position_at_end</span> <span class="n">new_then_bb</span> <span class="n">builder</span><span class="o">;</span> <span class="n">ignore</span> <span class="o">(</span><span class="n">build_br</span> <span class="n">merge_bb</span> <span class="n">builder</span><span class="o">);</span>
+      <span class="n">position_at_end</span> <span class="n">new_else_bb</span> <span class="n">builder</span><span class="o">;</span> <span class="n">ignore</span> <span class="o">(</span><span class="n">build_br</span> <span class="n">merge_bb</span> <span class="n">builder</span><span class="o">);</span>
+
+      <span class="c">(* Finally, set the builder to the end of the merge block. *)</span>
+      <span class="n">position_at_end</span> <span class="n">merge_bb</span> <span class="n">builder</span><span class="o">;</span>
+
+      <span class="n">phi</span>
+  <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">For</span> <span class="o">(</span><span class="n">var_name</span><span class="o">,</span> <span class="n">start</span><span class="o">,</span> <span class="n">end_</span><span class="o">,</span> <span class="n">step</span><span class="o">,</span> <span class="n">body</span><span class="o">)</span> <span class="o">-></span>
+      <span class="c">(* Emit the start code first, without 'variable' in scope. *)</span>
+      <span class="k">let</span> <span class="n">start_val</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">start</span> <span class="k">in</span>
+
+      <span class="c">(* Make the new basic block for the loop header, inserting after current</span>
+<span class="c">       * block. *)</span>
+      <span class="k">let</span> <span class="n">preheader_bb</span> <span class="o">=</span> <span class="n">insertion_block</span> <span class="n">builder</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">the_function</span> <span class="o">=</span> <span class="n">block_parent</span> <span class="n">preheader_bb</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">loop_bb</span> <span class="o">=</span> <span class="n">append_block</span> <span class="n">context</span> <span class="s2">"loop"</span> <span class="n">the_function</span> <span class="k">in</span>
+
+      <span class="c">(* Insert an explicit fall through from the current block to the</span>
+<span class="c">       * loop_bb. *)</span>
+      <span class="n">ignore</span> <span class="o">(</span><span class="n">build_br</span> <span class="n">loop_bb</span> <span class="n">builder</span><span class="o">);</span>
+
+      <span class="c">(* Start insertion in loop_bb. *)</span>
+      <span class="n">position_at_end</span> <span class="n">loop_bb</span> <span class="n">builder</span><span class="o">;</span>
+
+      <span class="c">(* Start the PHI node with an entry for start. *)</span>
+      <span class="k">let</span> <span class="n">variable</span> <span class="o">=</span> <span class="n">build_phi</span> <span class="o">[(</span><span class="n">start_val</span><span class="o">,</span> <span class="n">preheader_bb</span><span class="o">)]</span> <span class="n">var_name</span> <span class="n">builder</span> <span class="k">in</span>
+
+      <span class="c">(* Within the loop, the variable is defined equal to the PHI node. If it</span>
+<span class="c">       * shadows an existing variable, we have to restore it, so save it</span>
+<span class="c">       * now. *)</span>
+      <span class="k">let</span> <span class="n">old_val</span> <span class="o">=</span>
+        <span class="k">try</span> <span class="nc">Some</span> <span class="o">(</span><span class="nn">Hashtbl</span><span class="p">.</span><span class="n">find</span> <span class="n">named_values</span> <span class="n">var_name</span><span class="o">)</span> <span class="k">with</span> <span class="nc">Not_found</span> <span class="o">-></span> <span class="nc">None</span>
+      <span class="k">in</span>
+      <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">add</span> <span class="n">named_values</span> <span class="n">var_name</span> <span class="n">variable</span><span class="o">;</span>
+
+      <span class="c">(* Emit the body of the loop.  This, like any other expr, can change the</span>
+<span class="c">       * current BB.  Note that we ignore the value computed by the body, but</span>
+<span class="c">       * don't allow an error *)</span>
+      <span class="n">ignore</span> <span class="o">(</span><span class="n">codegen_expr</span> <span class="n">body</span><span class="o">);</span>
+
+      <span class="c">(* Emit the step value. *)</span>
+      <span class="k">let</span> <span class="n">step_val</span> <span class="o">=</span>
+        <span class="k">match</span> <span class="n">step</span> <span class="k">with</span>
+        <span class="o">|</span> <span class="nc">Some</span> <span class="n">step</span> <span class="o">-></span> <span class="n">codegen_expr</span> <span class="n">step</span>
+        <span class="c">(* If not specified, use 1.0. *)</span>
+        <span class="o">|</span> <span class="nc">None</span> <span class="o">-></span> <span class="n">const_float</span> <span class="n">double_type</span> <span class="mi">1</span><span class="o">.</span><span class="mi">0</span>
+      <span class="k">in</span>
+
+      <span class="k">let</span> <span class="n">next_var</span> <span class="o">=</span> <span class="n">build_add</span> <span class="n">variable</span> <span class="n">step_val</span> <span class="s2">"nextvar"</span> <span class="n">builder</span> <span class="k">in</span>
+
+      <span class="c">(* Compute the end condition. *)</span>
+      <span class="k">let</span> <span class="n">end_cond</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">end_</span> <span class="k">in</span>
+
+      <span class="c">(* Convert condition to a bool by comparing equal to 0.0. *)</span>
+      <span class="k">let</span> <span class="n">zero</span> <span class="o">=</span> <span class="n">const_float</span> <span class="n">double_type</span> <span class="mi">0</span><span class="o">.</span><span class="mi">0</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">end_cond</span> <span class="o">=</span> <span class="n">build_fcmp</span> <span class="nn">Fcmp</span><span class="p">.</span><span class="nc">One</span> <span class="n">end_cond</span> <span class="n">zero</span> <span class="s2">"loopcond"</span> <span class="n">builder</span> <span class="k">in</span>
+
+      <span class="c">(* Create the "after loop" block and insert it. *)</span>
+      <span class="k">let</span> <span class="n">loop_end_bb</span> <span class="o">=</span> <span class="n">insertion_block</span> <span class="n">builder</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">after_bb</span> <span class="o">=</span> <span class="n">append_block</span> <span class="n">context</span> <span class="s2">"afterloop"</span> <span class="n">the_function</span> <span class="k">in</span>
+
+      <span class="c">(* Insert the conditional branch into the end of loop_end_bb. *)</span>
+      <span class="n">ignore</span> <span class="o">(</span><span class="n">build_cond_br</span> <span class="n">end_cond</span> <span class="n">loop_bb</span> <span class="n">after_bb</span> <span class="n">builder</span><span class="o">);</span>
+
+      <span class="c">(* Any new code will be inserted in after_bb. *)</span>
+      <span class="n">position_at_end</span> <span class="n">after_bb</span> <span class="n">builder</span><span class="o">;</span>
+
+      <span class="c">(* Add a new entry to the PHI node for the backedge. *)</span>
+      <span class="n">add_incoming</span> <span class="o">(</span><span class="n">next_var</span><span class="o">,</span> <span class="n">loop_end_bb</span><span class="o">)</span> <span class="n">variable</span><span class="o">;</span>
+
+      <span class="c">(* Restore the unshadowed variable. *)</span>
+      <span class="k">begin</span> <span class="k">match</span> <span class="n">old_val</span> <span class="k">with</span>
+      <span class="o">|</span> <span class="nc">Some</span> <span class="n">old_val</span> <span class="o">-></span> <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">add</span> <span class="n">named_values</span> <span class="n">var_name</span> <span class="n">old_val</span>
+      <span class="o">|</span> <span class="nc">None</span> <span class="o">-></span> <span class="bp">()</span>
+      <span class="k">end</span><span class="o">;</span>
+
+      <span class="c">(* for expr always returns 0.0. *)</span>
+      <span class="n">const_null</span> <span class="n">double_type</span>
+
+<span class="k">let</span> <span class="n">codegen_proto</span> <span class="o">=</span> <span class="k">function</span>
+  <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Prototype</span> <span class="o">(</span><span class="n">name</span><span class="o">,</span> <span class="n">args</span><span class="o">)</span> <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">BinOpPrototype</span> <span class="o">(</span><span class="n">name</span><span class="o">,</span> <span class="n">args</span><span class="o">,</span> <span class="o">_)</span> <span class="o">-></span>
+      <span class="c">(* Make the function type: double(double,double) etc. *)</span>
+      <span class="k">let</span> <span class="n">doubles</span> <span class="o">=</span> <span class="nn">Array</span><span class="p">.</span><span class="n">make</span> <span class="o">(</span><span class="nn">Array</span><span class="p">.</span><span class="n">length</span> <span class="n">args</span><span class="o">)</span> <span class="n">double_type</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">ft</span> <span class="o">=</span> <span class="n">function_type</span> <span class="n">double_type</span> <span class="n">doubles</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">f</span> <span class="o">=</span>
+        <span class="k">match</span> <span class="n">lookup_function</span> <span class="n">name</span> <span class="n">the_module</span> <span class="k">with</span>
+        <span class="o">|</span> <span class="nc">None</span> <span class="o">-></span> <span class="n">declare_function</span> <span class="n">name</span> <span class="n">ft</span> <span class="n">the_module</span>
+
+        <span class="c">(* If 'f' conflicted, there was already something named 'name'. If it</span>
+<span class="c">         * has a body, don't allow redefinition or reextern. *)</span>
+        <span class="o">|</span> <span class="nc">Some</span> <span class="n">f</span> <span class="o">-></span>
+            <span class="c">(* If 'f' already has a body, reject this. *)</span>
+            <span class="k">if</span> <span class="n">block_begin</span> <span class="n">f</span> <span class="o"><></span> <span class="nc">At_end</span> <span class="n">f</span> <span class="k">then</span>
+              <span class="k">raise</span> <span class="o">(</span><span class="nc">Error</span> <span class="s2">"redefinition of function"</span><span class="o">);</span>
+
+            <span class="c">(* If 'f' took a different number of arguments, reject. *)</span>
+            <span class="k">if</span> <span class="n">element_type</span> <span class="o">(</span><span class="n">type_of</span> <span class="n">f</span><span class="o">)</span> <span class="o"><></span> <span class="n">ft</span> <span class="k">then</span>
+              <span class="k">raise</span> <span class="o">(</span><span class="nc">Error</span> <span class="s2">"redefinition of function with different # args"</span><span class="o">);</span>
+            <span class="n">f</span>
+      <span class="k">in</span>
+
+      <span class="c">(* Set names for all arguments. *)</span>
+      <span class="nn">Array</span><span class="p">.</span><span class="n">iteri</span> <span class="o">(</span><span class="k">fun</span> <span class="n">i</span> <span class="n">a</span> <span class="o">-></span>
+        <span class="k">let</span> <span class="n">n</span> <span class="o">=</span> <span class="n">args</span><span class="o">.(</span><span class="n">i</span><span class="o">)</span> <span class="k">in</span>
+        <span class="n">set_value_name</span> <span class="n">n</span> <span class="n">a</span><span class="o">;</span>
+        <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">add</span> <span class="n">named_values</span> <span class="n">n</span> <span class="n">a</span><span class="o">;</span>
+      <span class="o">)</span> <span class="o">(</span><span class="n">params</span> <span class="n">f</span><span class="o">);</span>
+      <span class="n">f</span>
+
+<span class="k">let</span> <span class="n">codegen_func</span> <span class="n">the_fpm</span> <span class="o">=</span> <span class="k">function</span>
+  <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Function</span> <span class="o">(</span><span class="n">proto</span><span class="o">,</span> <span class="n">body</span><span class="o">)</span> <span class="o">-></span>
+      <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">clear</span> <span class="n">named_values</span><span class="o">;</span>
+      <span class="k">let</span> <span class="n">the_function</span> <span class="o">=</span> <span class="n">codegen_proto</span> <span class="n">proto</span> <span class="k">in</span>
+
+      <span class="c">(* If this is an operator, install it. *)</span>
+      <span class="k">begin</span> <span class="k">match</span> <span class="n">proto</span> <span class="k">with</span>
+      <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">BinOpPrototype</span> <span class="o">(</span><span class="n">name</span><span class="o">,</span> <span class="n">args</span><span class="o">,</span> <span class="n">prec</span><span class="o">)</span> <span class="o">-></span>
+          <span class="k">let</span> <span class="n">op</span> <span class="o">=</span> <span class="n">name</span><span class="o">.[</span><span class="nn">String</span><span class="p">.</span><span class="n">length</span> <span class="n">name</span> <span class="o">-</span> <span class="mi">1</span><span class="o">]</span> <span class="k">in</span>
+          <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">add</span> <span class="nn">Parser</span><span class="p">.</span><span class="n">binop_precedence</span> <span class="n">op</span> <span class="n">prec</span><span class="o">;</span>
+      <span class="o">|</span> <span class="o">_</span> <span class="o">-></span> <span class="bp">()</span>
+      <span class="k">end</span><span class="o">;</span>
+
+      <span class="c">(* Create a new basic block to start insertion into. *)</span>
+      <span class="k">let</span> <span class="n">bb</span> <span class="o">=</span> <span class="n">append_block</span> <span class="n">context</span> <span class="s2">"entry"</span> <span class="n">the_function</span> <span class="k">in</span>
+      <span class="n">position_at_end</span> <span class="n">bb</span> <span class="n">builder</span><span class="o">;</span>
+
+      <span class="k">try</span>
+        <span class="k">let</span> <span class="n">ret_val</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">body</span> <span class="k">in</span>
+
+        <span class="c">(* Finish off the function. *)</span>
+        <span class="k">let</span> <span class="o">_</span> <span class="o">=</span> <span class="n">build_ret</span> <span class="n">ret_val</span> <span class="n">builder</span> <span class="k">in</span>
+
+        <span class="c">(* Validate the generated code, checking for consistency. *)</span>
+        <span class="nn">Llvm_analysis</span><span class="p">.</span><span class="n">assert_valid_function</span> <span class="n">the_function</span><span class="o">;</span>
+
+        <span class="c">(* Optimize the function. *)</span>
+        <span class="k">let</span> <span class="o">_</span> <span class="o">=</span> <span class="nn">PassManager</span><span class="p">.</span><span class="n">run_function</span> <span class="n">the_function</span> <span class="n">the_fpm</span> <span class="k">in</span>
+
+        <span class="n">the_function</span>
+      <span class="k">with</span> <span class="n">e</span> <span class="o">-></span>
+        <span class="n">delete_function</span> <span class="n">the_function</span><span class="o">;</span>
+        <span class="k">raise</span> <span class="n">e</span>
+</pre></div>
+</div>
+</dd>
+<dt>toplevel.ml:</dt>
+<dd><div class="first last highlight-ocaml"><div class="highlight"><pre><span class="c">(*===----------------------------------------------------------------------===</span>
+<span class="c"> * Top-Level parsing and JIT Driver</span>
+<span class="c"> *===----------------------------------------------------------------------===*)</span>
+
+<span class="k">open</span> <span class="nc">Llvm</span>
+<span class="k">open</span> <span class="nc">Llvm_executionengine</span>
+
+<span class="c">(* top ::= definition | external | expression | ';' *)</span>
+<span class="k">let</span> <span class="k">rec</span> <span class="n">main_loop</span> <span class="n">the_fpm</span> <span class="n">the_execution_engine</span> <span class="n">stream</span> <span class="o">=</span>
+  <span class="k">match</span> <span class="nn">Stream</span><span class="p">.</span><span class="n">peek</span> <span class="n">stream</span> <span class="k">with</span>
+  <span class="o">|</span> <span class="nc">None</span> <span class="o">-></span> <span class="bp">()</span>
+
+  <span class="c">(* ignore top-level semicolons. *)</span>
+  <span class="o">|</span> <span class="nc">Some</span> <span class="o">(</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">';'</span><span class="o">)</span> <span class="o">-></span>
+      <span class="nn">Stream</span><span class="p">.</span><span class="n">junk</span> <span class="n">stream</span><span class="o">;</span>
+      <span class="n">main_loop</span> <span class="n">the_fpm</span> <span class="n">the_execution_engine</span> <span class="n">stream</span>
+
+  <span class="o">|</span> <span class="nc">Some</span> <span class="n">token</span> <span class="o">-></span>
+      <span class="k">begin</span>
+        <span class="k">try</span> <span class="k">match</span> <span class="n">token</span> <span class="k">with</span>
+        <span class="o">|</span> <span class="nn">Token</span><span class="p">.</span><span class="nc">Def</span> <span class="o">-></span>
+            <span class="k">let</span> <span class="n">e</span> <span class="o">=</span> <span class="nn">Parser</span><span class="p">.</span><span class="n">parse_definition</span> <span class="n">stream</span> <span class="k">in</span>
+            <span class="n">print_endline</span> <span class="s2">"parsed a function definition."</span><span class="o">;</span>
+            <span class="n">dump_value</span> <span class="o">(</span><span class="nn">Codegen</span><span class="p">.</span><span class="n">codegen_func</span> <span class="n">the_fpm</span> <span class="n">e</span><span class="o">);</span>
+        <span class="o">|</span> <span class="nn">Token</span><span class="p">.</span><span class="nc">Extern</span> <span class="o">-></span>
+            <span class="k">let</span> <span class="n">e</span> <span class="o">=</span> <span class="nn">Parser</span><span class="p">.</span><span class="n">parse_extern</span> <span class="n">stream</span> <span class="k">in</span>
+            <span class="n">print_endline</span> <span class="s2">"parsed an extern."</span><span class="o">;</span>
+            <span class="n">dump_value</span> <span class="o">(</span><span class="nn">Codegen</span><span class="p">.</span><span class="n">codegen_proto</span> <span class="n">e</span><span class="o">);</span>
+        <span class="o">|</span> <span class="o">_</span> <span class="o">-></span>
+            <span class="c">(* Evaluate a top-level expression into an anonymous function. *)</span>
+            <span class="k">let</span> <span class="n">e</span> <span class="o">=</span> <span class="nn">Parser</span><span class="p">.</span><span class="n">parse_toplevel</span> <span class="n">stream</span> <span class="k">in</span>
+            <span class="n">print_endline</span> <span class="s2">"parsed a top-level expr"</span><span class="o">;</span>
+            <span class="k">let</span> <span class="n">the_function</span> <span class="o">=</span> <span class="nn">Codegen</span><span class="p">.</span><span class="n">codegen_func</span> <span class="n">the_fpm</span> <span class="n">e</span> <span class="k">in</span>
+            <span class="n">dump_value</span> <span class="n">the_function</span><span class="o">;</span>
+
+            <span class="c">(* JIT the function, returning a function pointer. *)</span>
+            <span class="k">let</span> <span class="n">result</span> <span class="o">=</span> <span class="nn">ExecutionEngine</span><span class="p">.</span><span class="n">run_function</span> <span class="n">the_function</span> <span class="o">[||]</span>
+              <span class="n">the_execution_engine</span> <span class="k">in</span>
+
+            <span class="n">print_string</span> <span class="s2">"Evaluated to "</span><span class="o">;</span>
+            <span class="n">print_float</span> <span class="o">(</span><span class="nn">GenericValue</span><span class="p">.</span><span class="n">as_float</span> <span class="nn">Codegen</span><span class="p">.</span><span class="n">double_type</span> <span class="n">result</span><span class="o">);</span>
+            <span class="n">print_newline</span> <span class="bp">()</span><span class="o">;</span>
+        <span class="k">with</span> <span class="nn">Stream</span><span class="p">.</span><span class="nc">Error</span> <span class="n">s</span> <span class="o">|</span> <span class="nn">Codegen</span><span class="p">.</span><span class="nc">Error</span> <span class="n">s</span> <span class="o">-></span>
+          <span class="c">(* Skip token for error recovery. *)</span>
+          <span class="nn">Stream</span><span class="p">.</span><span class="n">junk</span> <span class="n">stream</span><span class="o">;</span>
+          <span class="n">print_endline</span> <span class="n">s</span><span class="o">;</span>
+      <span class="k">end</span><span class="o">;</span>
+      <span class="n">print_string</span> <span class="s2">"ready> "</span><span class="o">;</span> <span class="n">flush</span> <span class="n">stdout</span><span class="o">;</span>
+      <span class="n">main_loop</span> <span class="n">the_fpm</span> <span class="n">the_execution_engine</span> <span class="n">stream</span>
+</pre></div>
+</div>
+</dd>
+<dt>toy.ml:</dt>
+<dd><div class="first last highlight-ocaml"><div class="highlight"><pre><span class="c">(*===----------------------------------------------------------------------===</span>
+<span class="c"> * Main driver code.</span>
+<span class="c"> *===----------------------------------------------------------------------===*)</span>
+
+<span class="k">open</span> <span class="nc">Llvm</span>
+<span class="k">open</span> <span class="nc">Llvm_executionengine</span>
+<span class="k">open</span> <span class="nc">Llvm_target</span>
+<span class="k">open</span> <span class="nc">Llvm_scalar_opts</span>
+
+<span class="k">let</span> <span class="n">main</span> <span class="bp">()</span> <span class="o">=</span>
+  <span class="n">ignore</span> <span class="o">(</span><span class="n">initialize_native_target</span> <span class="bp">()</span><span class="o">);</span>
+
+  <span class="c">(* Install standard binary operators.</span>
+<span class="c">   * 1 is the lowest precedence. *)</span>
+  <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">add</span> <span class="nn">Parser</span><span class="p">.</span><span class="n">binop_precedence</span> <span class="sc">'<'</span> <span class="mi">10</span><span class="o">;</span>
+  <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">add</span> <span class="nn">Parser</span><span class="p">.</span><span class="n">binop_precedence</span> <span class="sc">'+'</span> <span class="mi">20</span><span class="o">;</span>
+  <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">add</span> <span class="nn">Parser</span><span class="p">.</span><span class="n">binop_precedence</span> <span class="sc">'-'</span> <span class="mi">20</span><span class="o">;</span>
+  <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">add</span> <span class="nn">Parser</span><span class="p">.</span><span class="n">binop_precedence</span> <span class="sc">'*'</span> <span class="mi">40</span><span class="o">;</span>    <span class="c">(* highest. *)</span>
+
+  <span class="c">(* Prime the first token. *)</span>
+  <span class="n">print_string</span> <span class="s2">"ready> "</span><span class="o">;</span> <span class="n">flush</span> <span class="n">stdout</span><span class="o">;</span>
+  <span class="k">let</span> <span class="n">stream</span> <span class="o">=</span> <span class="nn">Lexer</span><span class="p">.</span><span class="n">lex</span> <span class="o">(</span><span class="nn">Stream</span><span class="p">.</span><span class="n">of_channel</span> <span class="n">stdin</span><span class="o">)</span> <span class="k">in</span>
+
+  <span class="c">(* Create the JIT. *)</span>
+  <span class="k">let</span> <span class="n">the_execution_engine</span> <span class="o">=</span> <span class="nn">ExecutionEngine</span><span class="p">.</span><span class="n">create</span> <span class="nn">Codegen</span><span class="p">.</span><span class="n">the_module</span> <span class="k">in</span>
+  <span class="k">let</span> <span class="n">the_fpm</span> <span class="o">=</span> <span class="nn">PassManager</span><span class="p">.</span><span class="n">create_function</span> <span class="nn">Codegen</span><span class="p">.</span><span class="n">the_module</span> <span class="k">in</span>
+
+  <span class="c">(* Set up the optimizer pipeline.  Start with registering info about how the</span>
+<span class="c">   * target lays out data structures. *)</span>
+  <span class="nn">DataLayout</span><span class="p">.</span><span class="n">add</span> <span class="o">(</span><span class="nn">ExecutionEngine</span><span class="p">.</span><span class="n">target_data</span> <span class="n">the_execution_engine</span><span class="o">)</span> <span class="n">the_fpm</span><span class="o">;</span>
+
+  <span class="c">(* Do simple "peephole" optimizations and bit-twiddling optzn. *)</span>
+  <span class="n">add_instruction_combination</span> <span class="n">the_fpm</span><span class="o">;</span>
+
+  <span class="c">(* reassociate expressions. *)</span>
+  <span class="n">add_reassociation</span> <span class="n">the_fpm</span><span class="o">;</span>
+
+  <span class="c">(* Eliminate Common SubExpressions. *)</span>
+  <span class="n">add_gvn</span> <span class="n">the_fpm</span><span class="o">;</span>
+
+  <span class="c">(* Simplify the control flow graph (deleting unreachable blocks, etc). *)</span>
+  <span class="n">add_cfg_simplification</span> <span class="n">the_fpm</span><span class="o">;</span>
+
+  <span class="n">ignore</span> <span class="o">(</span><span class="nn">PassManager</span><span class="p">.</span><span class="n">initialize</span> <span class="n">the_fpm</span><span class="o">);</span>
+
+  <span class="c">(* Run the main "interpreter loop" now. *)</span>
+  <span class="nn">Toplevel</span><span class="p">.</span><span class="n">main_loop</span> <span class="n">the_fpm</span> <span class="n">the_execution_engine</span> <span class="n">stream</span><span class="o">;</span>
+
+  <span class="c">(* Print out all the generated code. *)</span>
+  <span class="n">dump_module</span> <span class="nn">Codegen</span><span class="p">.</span><span class="n">the_module</span>
+<span class="o">;;</span>
+
+<span class="n">main</span> <span class="bp">()</span>
+</pre></div>
+</div>
+</dd>
+<dt>bindings.c</dt>
+<dd><div class="first last highlight-c"><div class="highlight"><pre><span class="cp">#include <stdio.h></span>
+
+<span class="cm">/* putchard - putchar that takes a double and returns 0. */</span>
+<span class="k">extern</span> <span class="kt">double</span> <span class="nf">putchard</span><span class="p">(</span><span class="kt">double</span> <span class="n">X</span><span class="p">)</span> <span class="p">{</span>
+  <span class="n">putchar</span><span class="p">((</span><span class="kt">char</span><span class="p">)</span><span class="n">X</span><span class="p">);</span>
+  <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
+<span class="p">}</span>
+
+<span class="cm">/* printd - printf that takes a double prints it as "%f\n", returning 0. */</span>
+<span class="k">extern</span> <span class="kt">double</span> <span class="nf">printd</span><span class="p">(</span><span class="kt">double</span> <span class="n">X</span><span class="p">)</span> <span class="p">{</span>
+  <span class="n">printf</span><span class="p">(</span><span class="s">"%f</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">X</span><span class="p">);</span>
+  <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
+<span class="p">}</span>
+</pre></div>
+</div>
+</dd>
+</dl>
+<p><a class="reference external" href="OCamlLangImpl7.html">Next: Extending the language: mutable variables / SSA
+construction</a></p>
+</div>
+</div>
+
+
+          </div>
+      </div>
+      <div class="clearer"></div>
+    </div>
+    <div class="related">
+      <h3>Navigation</h3>
+      <ul>
+        <li class="right" style="margin-right: 10px">
+          <a href="../genindex.html" title="General Index"
+             >index</a></li>
+        <li class="right" >
+          <a href="OCamlLangImpl7.html" title="7. Kaleidoscope: Extending the Language: Mutable Variables"
+             >next</a> |</li>
+        <li class="right" >
+          <a href="OCamlLangImpl5.html" title="5. Kaleidoscope: Extending the Language: Control Flow"
+             >previous</a> |</li>
+  <li><a href="http://llvm.org/">LLVM Home</a> | </li>
+  <li><a href="../index.html">Documentation</a>»</li>
+
+          <li><a href="index.html" >LLVM Tutorial: Table of Contents</a> »</li> 
+      </ul>
+    </div>
+    <div class="footer">
+        © Copyright 2003-2016, LLVM Project.
+      Last updated on 2016-01-04.
+      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.3.
+    </div>
+  </body>
+</html>
\ No newline at end of file

Added: www-releases/trunk/3.7.1/docs/tutorial/OCamlLangImpl7.html
URL: http://llvm.org/viewvc/llvm-project/www-releases/trunk/3.7.1/docs/tutorial/OCamlLangImpl7.html?rev=257950&view=auto
==============================================================================
--- www-releases/trunk/3.7.1/docs/tutorial/OCamlLangImpl7.html (added)
+++ www-releases/trunk/3.7.1/docs/tutorial/OCamlLangImpl7.html Fri Jan 15 17:13:16 2016
@@ -0,0 +1,1751 @@
+
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+
+
+<html xmlns="http://www.w3.org/1999/xhtml">
+  <head>
+    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+    
+    <title>7. Kaleidoscope: Extending the Language: Mutable Variables — LLVM 3.7 documentation</title>
+    
+    <link rel="stylesheet" href="../_static/llvm-theme.css" type="text/css" />
+    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
+    
+    <script type="text/javascript">
+      var DOCUMENTATION_OPTIONS = {
+        URL_ROOT:    '../',
+        VERSION:     '3.7',
+        COLLAPSE_INDEX: false,
+        FILE_SUFFIX: '.html',
+        HAS_SOURCE:  true
+      };
+    </script>
+    <script type="text/javascript" src="../_static/jquery.js"></script>
+    <script type="text/javascript" src="../_static/underscore.js"></script>
+    <script type="text/javascript" src="../_static/doctools.js"></script>
+    <link rel="top" title="LLVM 3.7 documentation" href="../index.html" />
+    <link rel="up" title="LLVM Tutorial: Table of Contents" href="index.html" />
+    <link rel="next" title="8. Kaleidoscope: Conclusion and other useful LLVM tidbits" href="OCamlLangImpl8.html" />
+    <link rel="prev" title="6. Kaleidoscope: Extending the Language: User-defined Operators" href="OCamlLangImpl6.html" />
+<style type="text/css">
+  table.right { float: right; margin-left: 20px; }
+  table.right td { border: 1px solid #ccc; }
+</style>
+
+  </head>
+  <body>
+<div class="logo">
+  <a href="../index.html">
+    <img src="../_static/logo.png"
+         alt="LLVM Logo" width="250" height="88"/></a>
+</div>
+
+    <div class="related">
+      <h3>Navigation</h3>
+      <ul>
+        <li class="right" style="margin-right: 10px">
+          <a href="../genindex.html" title="General Index"
+             accesskey="I">index</a></li>
+        <li class="right" >
+          <a href="OCamlLangImpl8.html" title="8. Kaleidoscope: Conclusion and other useful LLVM tidbits"
+             accesskey="N">next</a> |</li>
+        <li class="right" >
+          <a href="OCamlLangImpl6.html" title="6. Kaleidoscope: Extending the Language: User-defined Operators"
+             accesskey="P">previous</a> |</li>
+  <li><a href="http://llvm.org/">LLVM Home</a> | </li>
+  <li><a href="../index.html">Documentation</a>»</li>
+
+          <li><a href="index.html" accesskey="U">LLVM Tutorial: Table of Contents</a> »</li> 
+      </ul>
+    </div>
+
+
+    <div class="document">
+      <div class="documentwrapper">
+          <div class="body">
+            
+  <div class="section" id="kaleidoscope-extending-the-language-mutable-variables">
+<h1>7. Kaleidoscope: Extending the Language: Mutable Variables<a class="headerlink" href="#kaleidoscope-extending-the-language-mutable-variables" title="Permalink to this headline">¶</a></h1>
+<div class="contents local topic" id="contents">
+<ul class="simple">
+<li><a class="reference internal" href="#chapter-7-introduction" id="id2">Chapter 7 Introduction</a></li>
+<li><a class="reference internal" href="#why-is-this-a-hard-problem" id="id3">Why is this a hard problem?</a></li>
+<li><a class="reference internal" href="#memory-in-llvm" id="id4">Memory in LLVM</a></li>
+<li><a class="reference internal" href="#mutable-variables-in-kaleidoscope" id="id5">Mutable Variables in Kaleidoscope</a></li>
+<li><a class="reference internal" href="#adjusting-existing-variables-for-mutation" id="id6">Adjusting Existing Variables for Mutation</a></li>
+<li><a class="reference internal" href="#new-assignment-operator" id="id7">New Assignment Operator</a></li>
+<li><a class="reference internal" href="#user-defined-local-variables" id="id8">User-defined Local Variables</a></li>
+<li><a class="reference internal" href="#id1" id="id9">Full Code Listing</a></li>
+</ul>
+</div>
+<div class="section" id="chapter-7-introduction">
+<h2><a class="toc-backref" href="#id2">7.1. Chapter 7 Introduction</a><a class="headerlink" href="#chapter-7-introduction" title="Permalink to this headline">¶</a></h2>
+<p>Welcome to Chapter 7 of the “<a class="reference external" href="index.html">Implementing a language with
+LLVM</a>” tutorial. In chapters 1 through 6, we’ve built a
+very respectable, albeit simple, <a class="reference external" href="http://en.wikipedia.org/wiki/Functional_programming">functional programming
+language</a>. In our
+journey, we learned some parsing techniques, how to build and represent
+an AST, how to build LLVM IR, and how to optimize the resultant code as
+well as JIT compile it.</p>
+<p>While Kaleidoscope is interesting as a functional language, the fact
+that it is functional makes it “too easy” to generate LLVM IR for it. In
+particular, a functional language makes it very easy to build LLVM IR
+directly in <a class="reference external" href="http://en.wikipedia.org/wiki/Static_single_assignment_form">SSA
+form</a>.
+Since LLVM requires that the input code be in SSA form, this is a very
+nice property and it is often unclear to newcomers how to generate code
+for an imperative language with mutable variables.</p>
+<p>The short (and happy) summary of this chapter is that there is no need
+for your front-end to build SSA form: LLVM provides highly tuned and
+well tested support for this, though the way it works is a bit
+unexpected for some.</p>
+</div>
+<div class="section" id="why-is-this-a-hard-problem">
+<h2><a class="toc-backref" href="#id3">7.2. Why is this a hard problem?</a><a class="headerlink" href="#why-is-this-a-hard-problem" title="Permalink to this headline">¶</a></h2>
+<p>To understand why mutable variables cause complexities in SSA
+construction, consider this extremely simple C example:</p>
+<div class="highlight-c"><div class="highlight"><pre><span class="kt">int</span> <span class="n">G</span><span class="p">,</span> <span class="n">H</span><span class="p">;</span>
+<span class="kt">int</span> <span class="nf">test</span><span class="p">(</span><span class="kt">_Bool</span> <span class="n">Condition</span><span class="p">)</span> <span class="p">{</span>
+  <span class="kt">int</span> <span class="n">X</span><span class="p">;</span>
+  <span class="k">if</span> <span class="p">(</span><span class="n">Condition</span><span class="p">)</span>
+    <span class="n">X</span> <span class="o">=</span> <span class="n">G</span><span class="p">;</span>
+  <span class="k">else</span>
+    <span class="n">X</span> <span class="o">=</span> <span class="n">H</span><span class="p">;</span>
+  <span class="k">return</span> <span class="n">X</span><span class="p">;</span>
+<span class="p">}</span>
+</pre></div>
+</div>
+<p>In this case, we have the variable “X”, whose value depends on the path
+executed in the program. Because there are two different possible values
+for X before the return instruction, a PHI node is inserted to merge the
+two values. The LLVM IR that we want for this example looks like this:</p>
+<div class="highlight-llvm"><div class="highlight"><pre><span class="vg">@G</span> <span class="p">=</span> <span class="k">weak</span> <span class="k">global</span> <span class="k">i32</span> <span class="m">0</span>   <span class="c">; type of @G is i32*</span>
+<span class="vg">@H</span> <span class="p">=</span> <span class="k">weak</span> <span class="k">global</span> <span class="k">i32</span> <span class="m">0</span>   <span class="c">; type of @H is i32*</span>
+
+<span class="k">define</span> <span class="k">i32</span> <span class="vg">@test</span><span class="p">(</span><span class="k">i1</span> <span class="nv">%Condition</span><span class="p">)</span> <span class="p">{</span>
+<span class="nl">entry:</span>
+  <span class="k">br</span> <span class="k">i1</span> <span class="nv">%Condition</span><span class="p">,</span> <span class="kt">label</span> <span class="nv">%cond_true</span><span class="p">,</span> <span class="kt">label</span> <span class="nv">%cond_false</span>
+
+<span class="nl">cond_true:</span>
+  <span class="nv">%X.0</span> <span class="p">=</span> <span class="k">load</span> <span class="k">i32</span><span class="p">*</span> <span class="vg">@G</span>
+  <span class="k">br</span> <span class="kt">label</span> <span class="nv">%cond_next</span>
+
+<span class="nl">cond_false:</span>
+  <span class="nv">%X.1</span> <span class="p">=</span> <span class="k">load</span> <span class="k">i32</span><span class="p">*</span> <span class="vg">@H</span>
+  <span class="k">br</span> <span class="kt">label</span> <span class="nv">%cond_next</span>
+
+<span class="nl">cond_next:</span>
+  <span class="nv">%X.2</span> <span class="p">=</span> <span class="k">phi</span> <span class="k">i32</span> <span class="p">[</span> <span class="nv">%X.1</span><span class="p">,</span> <span class="nv">%cond_false</span> <span class="p">],</span> <span class="p">[</span> <span class="nv">%X.0</span><span class="p">,</span> <span class="nv">%cond_true</span> <span class="p">]</span>
+  <span class="k">ret</span> <span class="k">i32</span> <span class="nv">%X.2</span>
+<span class="p">}</span>
+</pre></div>
+</div>
+<p>In this example, the loads from the G and H global variables are
+explicit in the LLVM IR, and they live in the then/else branches of the
+if statement (cond_true/cond_false). In order to merge the incoming
+values, the X.2 phi node in the cond_next block selects the right value
+to use based on where control flow is coming from: if control flow comes
+from the cond_false block, X.2 gets the value of X.1. Alternatively, if
+control flow comes from cond_true, it gets the value of X.0. The intent
+of this chapter is not to explain the details of SSA form. For more
+information, see one of the many <a class="reference external" href="http://en.wikipedia.org/wiki/Static_single_assignment_form">online
+references</a>.</p>
+<p>The question for this article is “who places the phi nodes when lowering
+assignments to mutable variables?”. The issue here is that LLVM
+<em>requires</em> that its IR be in SSA form: there is no “non-ssa” mode for
+it. However, SSA construction requires non-trivial algorithms and data
+structures, so it is inconvenient and wasteful for every front-end to
+have to reproduce this logic.</p>
+</div>
+<div class="section" id="memory-in-llvm">
+<h2><a class="toc-backref" href="#id4">7.3. Memory in LLVM</a><a class="headerlink" href="#memory-in-llvm" title="Permalink to this headline">¶</a></h2>
+<p>The ‘trick’ here is that while LLVM does require all register values to
+be in SSA form, it does not require (or permit) memory objects to be in
+SSA form. In the example above, note that the loads from G and H are
+direct accesses to G and H: they are not renamed or versioned. This
+differs from some other compiler systems, which do try to version memory
+objects. In LLVM, instead of encoding dataflow analysis of memory into
+the LLVM IR, it is handled with <a class="reference external" href="../WritingAnLLVMPass.html">Analysis
+Passes</a> which are computed on demand.</p>
+<p>With this in mind, the high-level idea is that we want to make a stack
+variable (which lives in memory, because it is on the stack) for each
+mutable object in a function. To take advantage of this trick, we need
+to talk about how LLVM represents stack variables.</p>
+<p>In LLVM, all memory accesses are explicit with load/store instructions,
+and it is carefully designed not to have (or need) an “address-of”
+operator. Notice how the type of the @G/@H global variables is actually
+“i32*” even though the variable is defined as “i32”. What this means is
+that @G defines <em>space</em> for an i32 in the global data area, but its
+<em>name</em> actually refers to the address for that space. Stack variables
+work the same way, except that instead of being declared with global
+variable definitions, they are declared with the <a class="reference external" href="../LangRef.html#i_alloca">LLVM alloca
+instruction</a>:</p>
+<div class="highlight-llvm"><div class="highlight"><pre><span class="k">define</span> <span class="k">i32</span> <span class="vg">@example</span><span class="p">()</span> <span class="p">{</span>
+<span class="nl">entry:</span>
+  <span class="nv">%X</span> <span class="p">=</span> <span class="k">alloca</span> <span class="k">i32</span>           <span class="c">; type of %X is i32*.</span>
+  <span class="p">...</span>
+  <span class="nv">%tmp</span> <span class="p">=</span> <span class="k">load</span> <span class="k">i32</span><span class="p">*</span> <span class="nv">%X</span>       <span class="c">; load the stack value %X from the stack.</span>
+  <span class="nv">%tmp2</span> <span class="p">=</span> <span class="k">add</span> <span class="k">i32</span> <span class="nv">%tmp</span><span class="p">,</span> <span class="m">1</span>   <span class="c">; increment it</span>
+  <span class="k">store</span> <span class="k">i32</span> <span class="nv">%tmp2</span><span class="p">,</span> <span class="k">i32</span><span class="p">*</span> <span class="nv">%X</span>  <span class="c">; store it back</span>
+  <span class="p">...</span>
+</pre></div>
+</div>
+<p>This code shows an example of how you can declare and manipulate a stack
+variable in the LLVM IR. Stack memory allocated with the alloca
+instruction is fully general: you can pass the address of the stack slot
+to functions, you can store it in other variables, etc. In our example
+above, we could rewrite the example to use the alloca technique to avoid
+using a PHI node:</p>
+<div class="highlight-llvm"><div class="highlight"><pre><span class="vg">@G</span> <span class="p">=</span> <span class="k">weak</span> <span class="k">global</span> <span class="k">i32</span> <span class="m">0</span>   <span class="c">; type of @G is i32*</span>
+<span class="vg">@H</span> <span class="p">=</span> <span class="k">weak</span> <span class="k">global</span> <span class="k">i32</span> <span class="m">0</span>   <span class="c">; type of @H is i32*</span>
+
+<span class="k">define</span> <span class="k">i32</span> <span class="vg">@test</span><span class="p">(</span><span class="k">i1</span> <span class="nv">%Condition</span><span class="p">)</span> <span class="p">{</span>
+<span class="nl">entry:</span>
+  <span class="nv">%X</span> <span class="p">=</span> <span class="k">alloca</span> <span class="k">i32</span>           <span class="c">; type of %X is i32*.</span>
+  <span class="k">br</span> <span class="k">i1</span> <span class="nv">%Condition</span><span class="p">,</span> <span class="kt">label</span> <span class="nv">%cond_true</span><span class="p">,</span> <span class="kt">label</span> <span class="nv">%cond_false</span>
+
+<span class="nl">cond_true:</span>
+  <span class="nv">%X.0</span> <span class="p">=</span> <span class="k">load</span> <span class="k">i32</span><span class="p">*</span> <span class="vg">@G</span>
+        <span class="k">store</span> <span class="k">i32</span> <span class="nv">%X.0</span><span class="p">,</span> <span class="k">i32</span><span class="p">*</span> <span class="nv">%X</span>   <span class="c">; Update X</span>
+  <span class="k">br</span> <span class="kt">label</span> <span class="nv">%cond_next</span>
+
+<span class="nl">cond_false:</span>
+  <span class="nv">%X.1</span> <span class="p">=</span> <span class="k">load</span> <span class="k">i32</span><span class="p">*</span> <span class="vg">@H</span>
+        <span class="k">store</span> <span class="k">i32</span> <span class="nv">%X.1</span><span class="p">,</span> <span class="k">i32</span><span class="p">*</span> <span class="nv">%X</span>   <span class="c">; Update X</span>
+  <span class="k">br</span> <span class="kt">label</span> <span class="nv">%cond_next</span>
+
+<span class="nl">cond_next:</span>
+  <span class="nv">%X.2</span> <span class="p">=</span> <span class="k">load</span> <span class="k">i32</span><span class="p">*</span> <span class="nv">%X</span>       <span class="c">; Read X</span>
+  <span class="k">ret</span> <span class="k">i32</span> <span class="nv">%X.2</span>
+<span class="p">}</span>
+</pre></div>
+</div>
+<p>With this, we have discovered a way to handle arbitrary mutable
+variables without the need to create Phi nodes at all:</p>
+<ol class="arabic simple">
+<li>Each mutable variable becomes a stack allocation.</li>
+<li>Each read of the variable becomes a load from the stack.</li>
+<li>Each update of the variable becomes a store to the stack.</li>
+<li>Taking the address of a variable just uses the stack address
+directly.</li>
+</ol>
+<p>While this solution has solved our immediate problem, it introduced
+another one: we have now apparently introduced a lot of stack traffic
+for very simple and common operations, a major performance problem.
+Fortunately for us, the LLVM optimizer has a highly-tuned optimization
+pass named “mem2reg” that handles this case, promoting allocas like this
+into SSA registers, inserting Phi nodes as appropriate. If you run this
+example through the pass, for example, you’ll get:</p>
+<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>llvm-as < example.ll | opt -mem2reg | llvm-dis
+ at G <span class="o">=</span> weak global i32 0
+ at H <span class="o">=</span> weak global i32 0
+
+define i32 @test<span class="o">(</span>i1 %Condition<span class="o">)</span> <span class="o">{</span>
+entry:
+  br i1 %Condition, label %cond_true, label %cond_false
+
+cond_true:
+  %X.0 <span class="o">=</span> load i32* @G
+  br label %cond_next
+
+cond_false:
+  %X.1 <span class="o">=</span> load i32* @H
+  br label %cond_next
+
+cond_next:
+  %X.01 <span class="o">=</span> phi i32 <span class="o">[</span> %X.1, %cond_false <span class="o">]</span>, <span class="o">[</span> %X.0, %cond_true <span class="o">]</span>
+  ret i32 %X.01
+<span class="o">}</span>
+</pre></div>
+</div>
+<p>The mem2reg pass implements the standard “iterated dominance frontier”
+algorithm for constructing SSA form and has a number of optimizations
+that speed up (very common) degenerate cases. The mem2reg optimization
+pass is the answer to dealing with mutable variables, and we highly
+recommend that you depend on it. Note that mem2reg only works on
+variables in certain circumstances:</p>
+<ol class="arabic simple">
+<li>mem2reg is alloca-driven: it looks for allocas and if it can handle
+them, it promotes them. It does not apply to global variables or heap
+allocations.</li>
+<li>mem2reg only looks for alloca instructions in the entry block of the
+function. Being in the entry block guarantees that the alloca is only
+executed once, which makes analysis simpler.</li>
+<li>mem2reg only promotes allocas whose uses are direct loads and stores.
+If the address of the stack object is passed to a function, or if any
+funny pointer arithmetic is involved, the alloca will not be
+promoted.</li>
+<li>mem2reg only works on allocas of <a class="reference external" href="../LangRef.html#t_classifications">first
+class</a> values (such as pointers,
+scalars and vectors), and only if the array size of the allocation is
+1 (or missing in the .ll file). mem2reg is not capable of promoting
+structs or arrays to registers. Note that the “scalarrepl” pass is
+more powerful and can promote structs, “unions”, and arrays in many
+cases.</li>
+</ol>
+<p>All of these properties are easy to satisfy for most imperative
+languages, and we’ll illustrate it below with Kaleidoscope. The final
+question you may be asking is: should I bother with this nonsense for my
+front-end? Wouldn’t it be better if I just did SSA construction
+directly, avoiding use of the mem2reg optimization pass? In short, we
+strongly recommend that you use this technique for building SSA form,
+unless there is an extremely good reason not to. Using this technique
+is:</p>
+<ul class="simple">
+<li>Proven and well tested: clang uses this technique
+for local mutable variables. As such, the most common clients of LLVM
+are using this to handle a bulk of their variables. You can be sure
+that bugs are found fast and fixed early.</li>
+<li>Extremely Fast: mem2reg has a number of special cases that make it
+fast in common cases as well as fully general. For example, it has
+fast-paths for variables that are only used in a single block,
+variables that only have one assignment point, good heuristics to
+avoid insertion of unneeded phi nodes, etc.</li>
+<li>Needed for debug info generation: <a class="reference external" href="../SourceLevelDebugging.html">Debug information in
+LLVM</a> relies on having the address of
+the variable exposed so that debug info can be attached to it. This
+technique dovetails very naturally with this style of debug info.</li>
+</ul>
+<p>If nothing else, this makes it much easier to get your front-end up and
+running, and is very simple to implement. Lets extend Kaleidoscope with
+mutable variables now!</p>
+</div>
+<div class="section" id="mutable-variables-in-kaleidoscope">
+<h2><a class="toc-backref" href="#id5">7.4. Mutable Variables in Kaleidoscope</a><a class="headerlink" href="#mutable-variables-in-kaleidoscope" title="Permalink to this headline">¶</a></h2>
+<p>Now that we know the sort of problem we want to tackle, lets see what
+this looks like in the context of our little Kaleidoscope language.
+We’re going to add two features:</p>
+<ol class="arabic simple">
+<li>The ability to mutate variables with the ‘=’ operator.</li>
+<li>The ability to define new variables.</li>
+</ol>
+<p>While the first item is really what this is about, we only have
+variables for incoming arguments as well as for induction variables, and
+redefining those only goes so far :). Also, the ability to define new
+variables is a useful thing regardless of whether you will be mutating
+them. Here’s a motivating example that shows how we could use these:</p>
+<div class="highlight-python"><div class="highlight"><pre># Define ':' for sequencing: as a low-precedence operator that ignores operands
+# and just returns the RHS.
+def binary : 1 (x y) y;
+
+# Recursive fib, we could do this before.
+def fib(x)
+  if (x < 3) then
+    1
+  else
+    fib(x-1)+fib(x-2);
+
+# Iterative fib.
+def fibi(x)
+  var a = 1, b = 1, c in
+  (for i = 3, i < x in
+     c = a + b :
+     a = b :
+     b = c) :
+  b;
+
+# Call it.
+fibi(10);
+</pre></div>
+</div>
+<p>In order to mutate variables, we have to change our existing variables
+to use the “alloca trick”. Once we have that, we’ll add our new
+operator, then extend Kaleidoscope to support new variable definitions.</p>
+</div>
+<div class="section" id="adjusting-existing-variables-for-mutation">
+<h2><a class="toc-backref" href="#id6">7.5. Adjusting Existing Variables for Mutation</a><a class="headerlink" href="#adjusting-existing-variables-for-mutation" title="Permalink to this headline">¶</a></h2>
+<p>The symbol table in Kaleidoscope is managed at code generation time by
+the ‘<tt class="docutils literal"><span class="pre">named_values</span></tt>‘ map. This map currently keeps track of the LLVM
+“Value*” that holds the double value for the named variable. In order
+to support mutation, we need to change this slightly, so that it
+<tt class="docutils literal"><span class="pre">named_values</span></tt> holds the <em>memory location</em> of the variable in
+question. Note that this change is a refactoring: it changes the
+structure of the code, but does not (by itself) change the behavior of
+the compiler. All of these changes are isolated in the Kaleidoscope code
+generator.</p>
+<p>At this point in Kaleidoscope’s development, it only supports variables
+for two things: incoming arguments to functions and the induction
+variable of ‘for’ loops. For consistency, we’ll allow mutation of these
+variables in addition to other user-defined variables. This means that
+these will both need memory locations.</p>
+<p>To start our transformation of Kaleidoscope, we’ll change the
+<tt class="docutils literal"><span class="pre">named_values</span></tt> map so that it maps to AllocaInst* instead of Value*.
+Once we do this, the C++ compiler will tell us what parts of the code we
+need to update:</p>
+<p><strong>Note:</strong> the ocaml bindings currently model both <tt class="docutils literal"><span class="pre">Value*</span></tt>‘s and
+<tt class="docutils literal"><span class="pre">AllocInst*</span></tt>‘s as <tt class="docutils literal"><span class="pre">Llvm.llvalue</span></tt>‘s, but this may change in the future
+to be more type safe.</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="k">let</span> <span class="n">named_values</span><span class="o">:(</span><span class="kt">string</span><span class="o">,</span> <span class="n">llvalue</span><span class="o">)</span> <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">t</span> <span class="o">=</span> <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">create</span> <span class="mi">10</span>
+</pre></div>
+</div>
+<p>Also, since we will need to create these alloca’s, we’ll use a helper
+function that ensures that the allocas are created in the entry block of
+the function:</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="c">(* Create an alloca instruction in the entry block of the function. This</span>
+<span class="c"> * is used for mutable variables etc. *)</span>
+<span class="k">let</span> <span class="n">create_entry_block_alloca</span> <span class="n">the_function</span> <span class="n">var_name</span> <span class="o">=</span>
+  <span class="k">let</span> <span class="n">builder</span> <span class="o">=</span> <span class="n">builder_at</span> <span class="o">(</span><span class="n">instr_begin</span> <span class="o">(</span><span class="n">entry_block</span> <span class="n">the_function</span><span class="o">))</span> <span class="k">in</span>
+  <span class="n">build_alloca</span> <span class="n">double_type</span> <span class="n">var_name</span> <span class="n">builder</span>
+</pre></div>
+</div>
+<p>This funny looking code creates an <tt class="docutils literal"><span class="pre">Llvm.llbuilder</span></tt> object that is
+pointing at the first instruction of the entry block. It then creates an
+alloca with the expected name and returns it. Because all values in
+Kaleidoscope are doubles, there is no need to pass in a type to use.</p>
+<p>With this in place, the first functionality change we want to make is to
+variable references. In our new scheme, variables live on the stack, so
+code generating a reference to them actually needs to produce a load
+from the stack slot:</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="k">let</span> <span class="k">rec</span> <span class="n">codegen_expr</span> <span class="o">=</span> <span class="k">function</span>
+  <span class="o">...</span>
+  <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Variable</span> <span class="n">name</span> <span class="o">-></span>
+      <span class="k">let</span> <span class="n">v</span> <span class="o">=</span> <span class="k">try</span> <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">find</span> <span class="n">named_values</span> <span class="n">name</span> <span class="k">with</span>
+        <span class="o">|</span> <span class="nc">Not_found</span> <span class="o">-></span> <span class="k">raise</span> <span class="o">(</span><span class="nc">Error</span> <span class="s2">"unknown variable name"</span><span class="o">)</span>
+      <span class="k">in</span>
+      <span class="c">(* Load the value. *)</span>
+      <span class="n">build_load</span> <span class="n">v</span> <span class="n">name</span> <span class="n">builder</span>
+</pre></div>
+</div>
+<p>As you can see, this is pretty straightforward. Now we need to update
+the things that define the variables to set up the alloca. We’ll start
+with <tt class="docutils literal"><span class="pre">codegen_expr</span> <span class="pre">Ast.For</span> <span class="pre">...</span></tt> (see the <a class="reference external" href="#code">full code listing</a>
+for the unabridged code):</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">For</span> <span class="o">(</span><span class="n">var_name</span><span class="o">,</span> <span class="n">start</span><span class="o">,</span> <span class="n">end_</span><span class="o">,</span> <span class="n">step</span><span class="o">,</span> <span class="n">body</span><span class="o">)</span> <span class="o">-></span>
+    <span class="k">let</span> <span class="n">the_function</span> <span class="o">=</span> <span class="n">block_parent</span> <span class="o">(</span><span class="n">insertion_block</span> <span class="n">builder</span><span class="o">)</span> <span class="k">in</span>
+
+    <span class="c">(* Create an alloca for the variable in the entry block. *)</span>
+    <span class="k">let</span> <span class="n">alloca</span> <span class="o">=</span> <span class="n">create_entry_block_alloca</span> <span class="n">the_function</span> <span class="n">var_name</span> <span class="k">in</span>
+
+    <span class="c">(* Emit the start code first, without 'variable' in scope. *)</span>
+    <span class="k">let</span> <span class="n">start_val</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">start</span> <span class="k">in</span>
+
+    <span class="c">(* Store the value into the alloca. *)</span>
+    <span class="n">ignore</span><span class="o">(</span><span class="n">build_store</span> <span class="n">start_val</span> <span class="n">alloca</span> <span class="n">builder</span><span class="o">);</span>
+
+    <span class="o">...</span>
+
+    <span class="c">(* Within the loop, the variable is defined equal to the PHI node. If it</span>
+<span class="c">     * shadows an existing variable, we have to restore it, so save it</span>
+<span class="c">     * now. *)</span>
+    <span class="k">let</span> <span class="n">old_val</span> <span class="o">=</span>
+      <span class="k">try</span> <span class="nc">Some</span> <span class="o">(</span><span class="nn">Hashtbl</span><span class="p">.</span><span class="n">find</span> <span class="n">named_values</span> <span class="n">var_name</span><span class="o">)</span> <span class="k">with</span> <span class="nc">Not_found</span> <span class="o">-></span> <span class="nc">None</span>
+    <span class="k">in</span>
+    <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">add</span> <span class="n">named_values</span> <span class="n">var_name</span> <span class="n">alloca</span><span class="o">;</span>
+
+    <span class="o">...</span>
+
+    <span class="c">(* Compute the end condition. *)</span>
+    <span class="k">let</span> <span class="n">end_cond</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">end_</span> <span class="k">in</span>
+
+    <span class="c">(* Reload, increment, and restore the alloca. This handles the case where</span>
+<span class="c">     * the body of the loop mutates the variable. *)</span>
+    <span class="k">let</span> <span class="n">cur_var</span> <span class="o">=</span> <span class="n">build_load</span> <span class="n">alloca</span> <span class="n">var_name</span> <span class="n">builder</span> <span class="k">in</span>
+    <span class="k">let</span> <span class="n">next_var</span> <span class="o">=</span> <span class="n">build_add</span> <span class="n">cur_var</span> <span class="n">step_val</span> <span class="s2">"nextvar"</span> <span class="n">builder</span> <span class="k">in</span>
+    <span class="n">ignore</span><span class="o">(</span><span class="n">build_store</span> <span class="n">next_var</span> <span class="n">alloca</span> <span class="n">builder</span><span class="o">);</span>
+    <span class="o">...</span>
+</pre></div>
+</div>
+<p>This code is virtually identical to the code <a class="reference external" href="OCamlLangImpl5.html#forcodegen">before we allowed mutable
+variables</a>. The big difference is that
+we no longer have to construct a PHI node, and we use load/store to
+access the variable as needed.</p>
+<p>To support mutable argument variables, we need to also make allocas for
+them. The code for this is also pretty simple:</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="c">(* Create an alloca for each argument and register the argument in the symbol</span>
+<span class="c"> * table so that references to it will succeed. *)</span>
+<span class="k">let</span> <span class="n">create_argument_allocas</span> <span class="n">the_function</span> <span class="n">proto</span> <span class="o">=</span>
+  <span class="k">let</span> <span class="n">args</span> <span class="o">=</span> <span class="k">match</span> <span class="n">proto</span> <span class="k">with</span>
+    <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Prototype</span> <span class="o">(_,</span> <span class="n">args</span><span class="o">)</span> <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">BinOpPrototype</span> <span class="o">(_,</span> <span class="n">args</span><span class="o">,</span> <span class="o">_)</span> <span class="o">-></span> <span class="n">args</span>
+  <span class="k">in</span>
+  <span class="nn">Array</span><span class="p">.</span><span class="n">iteri</span> <span class="o">(</span><span class="k">fun</span> <span class="n">i</span> <span class="n">ai</span> <span class="o">-></span>
+    <span class="k">let</span> <span class="n">var_name</span> <span class="o">=</span> <span class="n">args</span><span class="o">.(</span><span class="n">i</span><span class="o">)</span> <span class="k">in</span>
+    <span class="c">(* Create an alloca for this variable. *)</span>
+    <span class="k">let</span> <span class="n">alloca</span> <span class="o">=</span> <span class="n">create_entry_block_alloca</span> <span class="n">the_function</span> <span class="n">var_name</span> <span class="k">in</span>
+
+    <span class="c">(* Store the initial value into the alloca. *)</span>
+    <span class="n">ignore</span><span class="o">(</span><span class="n">build_store</span> <span class="n">ai</span> <span class="n">alloca</span> <span class="n">builder</span><span class="o">);</span>
+
+    <span class="c">(* Add arguments to variable symbol table. *)</span>
+    <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">add</span> <span class="n">named_values</span> <span class="n">var_name</span> <span class="n">alloca</span><span class="o">;</span>
+  <span class="o">)</span> <span class="o">(</span><span class="n">params</span> <span class="n">the_function</span><span class="o">)</span>
+</pre></div>
+</div>
+<p>For each argument, we make an alloca, store the input value to the
+function into the alloca, and register the alloca as the memory location
+for the argument. This method gets invoked by <tt class="docutils literal"><span class="pre">Codegen.codegen_func</span></tt>
+right after it sets up the entry block for the function.</p>
+<p>The final missing piece is adding the mem2reg pass, which allows us to
+get good codegen once again:</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="k">let</span> <span class="n">main</span> <span class="bp">()</span> <span class="o">=</span>
+  <span class="o">...</span>
+  <span class="k">let</span> <span class="n">the_fpm</span> <span class="o">=</span> <span class="nn">PassManager</span><span class="p">.</span><span class="n">create_function</span> <span class="nn">Codegen</span><span class="p">.</span><span class="n">the_module</span> <span class="k">in</span>
+
+  <span class="c">(* Set up the optimizer pipeline.  Start with registering info about how the</span>
+<span class="c">   * target lays out data structures. *)</span>
+  <span class="nn">DataLayout</span><span class="p">.</span><span class="n">add</span> <span class="o">(</span><span class="nn">ExecutionEngine</span><span class="p">.</span><span class="n">target_data</span> <span class="n">the_execution_engine</span><span class="o">)</span> <span class="n">the_fpm</span><span class="o">;</span>
+
+  <span class="c">(* Promote allocas to registers. *)</span>
+  <span class="n">add_memory_to_register_promotion</span> <span class="n">the_fpm</span><span class="o">;</span>
+
+  <span class="c">(* Do simple "peephole" optimizations and bit-twiddling optzn. *)</span>
+  <span class="n">add_instruction_combining</span> <span class="n">the_fpm</span><span class="o">;</span>
+
+  <span class="c">(* reassociate expressions. *)</span>
+  <span class="n">add_reassociation</span> <span class="n">the_fpm</span><span class="o">;</span>
+</pre></div>
+</div>
+<p>It is interesting to see what the code looks like before and after the
+mem2reg optimization runs. For example, this is the before/after code
+for our recursive fib function. Before the optimization:</p>
+<div class="highlight-llvm"><div class="highlight"><pre><span class="k">define</span> <span class="kt">double</span> <span class="vg">@fib</span><span class="p">(</span><span class="kt">double</span> <span class="nv">%x</span><span class="p">)</span> <span class="p">{</span>
+<span class="nl">entry:</span>
+  <span class="nv">%x1</span> <span class="p">=</span> <span class="k">alloca</span> <span class="kt">double</span>
+  <span class="k">store</span> <span class="kt">double</span> <span class="nv">%x</span><span class="p">,</span> <span class="kt">double</span><span class="p">*</span> <span class="nv">%x1</span>
+  <span class="nv">%x2</span> <span class="p">=</span> <span class="k">load</span> <span class="kt">double</span><span class="p">*</span> <span class="nv">%x1</span>
+  <span class="nv">%cmptmp</span> <span class="p">=</span> <span class="k">fcmp</span> <span class="k">ult</span> <span class="kt">double</span> <span class="nv">%x2</span><span class="p">,</span> <span class="m">3.000000e+00</span>
+  <span class="nv">%booltmp</span> <span class="p">=</span> <span class="k">uitofp</span> <span class="k">i1</span> <span class="nv">%cmptmp</span> <span class="k">to</span> <span class="kt">double</span>
+  <span class="nv">%ifcond</span> <span class="p">=</span> <span class="k">fcmp</span> <span class="k">one</span> <span class="kt">double</span> <span class="nv">%booltmp</span><span class="p">,</span> <span class="m">0.000000e+00</span>
+  <span class="k">br</span> <span class="k">i1</span> <span class="nv">%ifcond</span><span class="p">,</span> <span class="kt">label</span> <span class="nv">%then</span><span class="p">,</span> <span class="kt">label</span> <span class="nv">%else</span>
+
+<span class="nl">then:</span>    <span class="c">; preds = %entry</span>
+  <span class="k">br</span> <span class="kt">label</span> <span class="nv">%ifcont</span>
+
+<span class="nl">else:</span>    <span class="c">; preds = %entry</span>
+  <span class="nv">%x3</span> <span class="p">=</span> <span class="k">load</span> <span class="kt">double</span><span class="p">*</span> <span class="nv">%x1</span>
+  <span class="nv">%subtmp</span> <span class="p">=</span> <span class="k">fsub</span> <span class="kt">double</span> <span class="nv">%x3</span><span class="p">,</span> <span class="m">1.000000e+00</span>
+  <span class="nv">%calltmp</span> <span class="p">=</span> <span class="k">call</span> <span class="kt">double</span> <span class="vg">@fib</span><span class="p">(</span><span class="kt">double</span> <span class="nv">%subtmp</span><span class="p">)</span>
+  <span class="nv">%x4</span> <span class="p">=</span> <span class="k">load</span> <span class="kt">double</span><span class="p">*</span> <span class="nv">%x1</span>
+  <span class="nv">%subtmp5</span> <span class="p">=</span> <span class="k">fsub</span> <span class="kt">double</span> <span class="nv">%x4</span><span class="p">,</span> <span class="m">2.000000e+00</span>
+  <span class="nv">%calltmp6</span> <span class="p">=</span> <span class="k">call</span> <span class="kt">double</span> <span class="vg">@fib</span><span class="p">(</span><span class="kt">double</span> <span class="nv">%subtmp5</span><span class="p">)</span>
+  <span class="nv">%addtmp</span> <span class="p">=</span> <span class="k">fadd</span> <span class="kt">double</span> <span class="nv">%calltmp</span><span class="p">,</span> <span class="nv">%calltmp6</span>
+  <span class="k">br</span> <span class="kt">label</span> <span class="nv">%ifcont</span>
+
+<span class="nl">ifcont:</span>    <span class="c">; preds = %else, %then</span>
+  <span class="nv">%iftmp</span> <span class="p">=</span> <span class="k">phi</span> <span class="kt">double</span> <span class="p">[</span> <span class="m">1.000000e+00</span><span class="p">,</span> <span class="nv">%then</span> <span class="p">],</span> <span class="p">[</span> <span class="nv">%addtmp</span><span class="p">,</span> <span class="nv">%else</span> <span class="p">]</span>
+  <span class="k">ret</span> <span class="kt">double</span> <span class="nv">%iftmp</span>
+<span class="p">}</span>
+</pre></div>
+</div>
+<p>Here there is only one variable (x, the input argument) but you can
+still see the extremely simple-minded code generation strategy we are
+using. In the entry block, an alloca is created, and the initial input
+value is stored into it. Each reference to the variable does a reload
+from the stack. Also, note that we didn’t modify the if/then/else
+expression, so it still inserts a PHI node. While we could make an
+alloca for it, it is actually easier to create a PHI node for it, so we
+still just make the PHI.</p>
+<p>Here is the code after the mem2reg pass runs:</p>
+<div class="highlight-llvm"><div class="highlight"><pre><span class="k">define</span> <span class="kt">double</span> <span class="vg">@fib</span><span class="p">(</span><span class="kt">double</span> <span class="nv">%x</span><span class="p">)</span> <span class="p">{</span>
+<span class="nl">entry:</span>
+  <span class="nv">%cmptmp</span> <span class="p">=</span> <span class="k">fcmp</span> <span class="k">ult</span> <span class="kt">double</span> <span class="nv">%x</span><span class="p">,</span> <span class="m">3.000000e+00</span>
+  <span class="nv">%booltmp</span> <span class="p">=</span> <span class="k">uitofp</span> <span class="k">i1</span> <span class="nv">%cmptmp</span> <span class="k">to</span> <span class="kt">double</span>
+  <span class="nv">%ifcond</span> <span class="p">=</span> <span class="k">fcmp</span> <span class="k">one</span> <span class="kt">double</span> <span class="nv">%booltmp</span><span class="p">,</span> <span class="m">0.000000e+00</span>
+  <span class="k">br</span> <span class="k">i1</span> <span class="nv">%ifcond</span><span class="p">,</span> <span class="kt">label</span> <span class="nv">%then</span><span class="p">,</span> <span class="kt">label</span> <span class="nv">%else</span>
+
+<span class="nl">then:</span>
+  <span class="k">br</span> <span class="kt">label</span> <span class="nv">%ifcont</span>
+
+<span class="nl">else:</span>
+  <span class="nv">%subtmp</span> <span class="p">=</span> <span class="k">fsub</span> <span class="kt">double</span> <span class="nv">%x</span><span class="p">,</span> <span class="m">1.000000e+00</span>
+  <span class="nv">%calltmp</span> <span class="p">=</span> <span class="k">call</span> <span class="kt">double</span> <span class="vg">@fib</span><span class="p">(</span><span class="kt">double</span> <span class="nv">%subtmp</span><span class="p">)</span>
+  <span class="nv">%subtmp5</span> <span class="p">=</span> <span class="k">fsub</span> <span class="kt">double</span> <span class="nv">%x</span><span class="p">,</span> <span class="m">2.000000e+00</span>
+  <span class="nv">%calltmp6</span> <span class="p">=</span> <span class="k">call</span> <span class="kt">double</span> <span class="vg">@fib</span><span class="p">(</span><span class="kt">double</span> <span class="nv">%subtmp5</span><span class="p">)</span>
+  <span class="nv">%addtmp</span> <span class="p">=</span> <span class="k">fadd</span> <span class="kt">double</span> <span class="nv">%calltmp</span><span class="p">,</span> <span class="nv">%calltmp6</span>
+  <span class="k">br</span> <span class="kt">label</span> <span class="nv">%ifcont</span>
+
+<span class="nl">ifcont:</span>    <span class="c">; preds = %else, %then</span>
+  <span class="nv">%iftmp</span> <span class="p">=</span> <span class="k">phi</span> <span class="kt">double</span> <span class="p">[</span> <span class="m">1.000000e+00</span><span class="p">,</span> <span class="nv">%then</span> <span class="p">],</span> <span class="p">[</span> <span class="nv">%addtmp</span><span class="p">,</span> <span class="nv">%else</span> <span class="p">]</span>
+  <span class="k">ret</span> <span class="kt">double</span> <span class="nv">%iftmp</span>
+<span class="p">}</span>
+</pre></div>
+</div>
+<p>This is a trivial case for mem2reg, since there are no redefinitions of
+the variable. The point of showing this is to calm your tension about
+inserting such blatent inefficiencies :).</p>
+<p>After the rest of the optimizers run, we get:</p>
+<div class="highlight-llvm"><div class="highlight"><pre><span class="k">define</span> <span class="kt">double</span> <span class="vg">@fib</span><span class="p">(</span><span class="kt">double</span> <span class="nv">%x</span><span class="p">)</span> <span class="p">{</span>
+<span class="nl">entry:</span>
+  <span class="nv">%cmptmp</span> <span class="p">=</span> <span class="k">fcmp</span> <span class="k">ult</span> <span class="kt">double</span> <span class="nv">%x</span><span class="p">,</span> <span class="m">3.000000e+00</span>
+  <span class="nv">%booltmp</span> <span class="p">=</span> <span class="k">uitofp</span> <span class="k">i1</span> <span class="nv">%cmptmp</span> <span class="k">to</span> <span class="kt">double</span>
+  <span class="nv">%ifcond</span> <span class="p">=</span> <span class="k">fcmp</span> <span class="k">ueq</span> <span class="kt">double</span> <span class="nv">%booltmp</span><span class="p">,</span> <span class="m">0.000000e+00</span>
+  <span class="k">br</span> <span class="k">i1</span> <span class="nv">%ifcond</span><span class="p">,</span> <span class="kt">label</span> <span class="nv">%else</span><span class="p">,</span> <span class="kt">label</span> <span class="nv">%ifcont</span>
+
+<span class="nl">else:</span>
+  <span class="nv">%subtmp</span> <span class="p">=</span> <span class="k">fsub</span> <span class="kt">double</span> <span class="nv">%x</span><span class="p">,</span> <span class="m">1.000000e+00</span>
+  <span class="nv">%calltmp</span> <span class="p">=</span> <span class="k">call</span> <span class="kt">double</span> <span class="vg">@fib</span><span class="p">(</span><span class="kt">double</span> <span class="nv">%subtmp</span><span class="p">)</span>
+  <span class="nv">%subtmp5</span> <span class="p">=</span> <span class="k">fsub</span> <span class="kt">double</span> <span class="nv">%x</span><span class="p">,</span> <span class="m">2.000000e+00</span>
+  <span class="nv">%calltmp6</span> <span class="p">=</span> <span class="k">call</span> <span class="kt">double</span> <span class="vg">@fib</span><span class="p">(</span><span class="kt">double</span> <span class="nv">%subtmp5</span><span class="p">)</span>
+  <span class="nv">%addtmp</span> <span class="p">=</span> <span class="k">fadd</span> <span class="kt">double</span> <span class="nv">%calltmp</span><span class="p">,</span> <span class="nv">%calltmp6</span>
+  <span class="k">ret</span> <span class="kt">double</span> <span class="nv">%addtmp</span>
+
+<span class="nl">ifcont:</span>
+  <span class="k">ret</span> <span class="kt">double</span> <span class="m">1.000000e+00</span>
+<span class="p">}</span>
+</pre></div>
+</div>
+<p>Here we see that the simplifycfg pass decided to clone the return
+instruction into the end of the ‘else’ block. This allowed it to
+eliminate some branches and the PHI node.</p>
+<p>Now that all symbol table references are updated to use stack variables,
+we’ll add the assignment operator.</p>
+</div>
+<div class="section" id="new-assignment-operator">
+<h2><a class="toc-backref" href="#id7">7.6. New Assignment Operator</a><a class="headerlink" href="#new-assignment-operator" title="Permalink to this headline">¶</a></h2>
+<p>With our current framework, adding a new assignment operator is really
+simple. We will parse it just like any other binary operator, but handle
+it internally (instead of allowing the user to define it). The first
+step is to set a precedence:</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="k">let</span> <span class="n">main</span> <span class="bp">()</span> <span class="o">=</span>
+  <span class="c">(* Install standard binary operators.</span>
+<span class="c">   * 1 is the lowest precedence. *)</span>
+  <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">add</span> <span class="nn">Parser</span><span class="p">.</span><span class="n">binop_precedence</span> <span class="sc">'='</span> <span class="mi">2</span><span class="o">;</span>
+  <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">add</span> <span class="nn">Parser</span><span class="p">.</span><span class="n">binop_precedence</span> <span class="sc">'<'</span> <span class="mi">10</span><span class="o">;</span>
+  <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">add</span> <span class="nn">Parser</span><span class="p">.</span><span class="n">binop_precedence</span> <span class="sc">'+'</span> <span class="mi">20</span><span class="o">;</span>
+  <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">add</span> <span class="nn">Parser</span><span class="p">.</span><span class="n">binop_precedence</span> <span class="sc">'-'</span> <span class="mi">20</span><span class="o">;</span>
+  <span class="o">...</span>
+</pre></div>
+</div>
+<p>Now that the parser knows the precedence of the binary operator, it
+takes care of all the parsing and AST generation. We just need to
+implement codegen for the assignment operator. This looks like:</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="k">let</span> <span class="k">rec</span> <span class="n">codegen_expr</span> <span class="o">=</span> <span class="k">function</span>
+      <span class="k">begin</span> <span class="k">match</span> <span class="n">op</span> <span class="k">with</span>
+      <span class="o">|</span> <span class="sc">'='</span> <span class="o">-></span>
+          <span class="c">(* Special case '=' because we don't want to emit the LHS as an</span>
+<span class="c">           * expression. *)</span>
+          <span class="k">let</span> <span class="n">name</span> <span class="o">=</span>
+            <span class="k">match</span> <span class="n">lhs</span> <span class="k">with</span>
+            <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Variable</span> <span class="n">name</span> <span class="o">-></span> <span class="n">name</span>
+            <span class="o">|</span> <span class="o">_</span> <span class="o">-></span> <span class="k">raise</span> <span class="o">(</span><span class="nc">Error</span> <span class="s2">"destination of '=' must be a variable"</span><span class="o">)</span>
+          <span class="k">in</span>
+</pre></div>
+</div>
+<p>Unlike the rest of the binary operators, our assignment operator doesn’t
+follow the “emit LHS, emit RHS, do computation” model. As such, it is
+handled as a special case before the other binary operators are handled.
+The other strange thing is that it requires the LHS to be a variable. It
+is invalid to have “(x+1) = expr” - only things like “x = expr” are
+allowed.</p>
+<div class="highlight-ocaml"><div class="highlight"><pre>    <span class="c">(* Codegen the rhs. *)</span>
+    <span class="k">let</span> <span class="n">val_</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">rhs</span> <span class="k">in</span>
+
+    <span class="c">(* Lookup the name. *)</span>
+    <span class="k">let</span> <span class="n">variable</span> <span class="o">=</span> <span class="k">try</span> <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">find</span> <span class="n">named_values</span> <span class="n">name</span> <span class="k">with</span>
+    <span class="o">|</span> <span class="nc">Not_found</span> <span class="o">-></span> <span class="k">raise</span> <span class="o">(</span><span class="nc">Error</span> <span class="s2">"unknown variable name"</span><span class="o">)</span>
+    <span class="k">in</span>
+    <span class="n">ignore</span><span class="o">(</span><span class="n">build_store</span> <span class="n">val_</span> <span class="n">variable</span> <span class="n">builder</span><span class="o">);</span>
+    <span class="n">val_</span>
+<span class="o">|</span> <span class="o">_</span> <span class="o">-></span>
+      <span class="o">...</span>
+</pre></div>
+</div>
+<p>Once we have the variable, codegen’ing the assignment is
+straightforward: we emit the RHS of the assignment, create a store, and
+return the computed value. Returning a value allows for chained
+assignments like “X = (Y = Z)”.</p>
+<p>Now that we have an assignment operator, we can mutate loop variables
+and arguments. For example, we can now run code like this:</p>
+<div class="highlight-python"><div class="highlight"><pre># Function to print a double.
+extern printd(x);
+
+# Define ':' for sequencing: as a low-precedence operator that ignores operands
+# and just returns the RHS.
+def binary : 1 (x y) y;
+
+def test(x)
+  printd(x) :
+  x = 4 :
+  printd(x);
+
+test(123);
+</pre></div>
+</div>
+<p>When run, this example prints “123” and then “4”, showing that we did
+actually mutate the value! Okay, we have now officially implemented our
+goal: getting this to work requires SSA construction in the general
+case. However, to be really useful, we want the ability to define our
+own local variables, lets add this next!</p>
+</div>
+<div class="section" id="user-defined-local-variables">
+<h2><a class="toc-backref" href="#id8">7.7. User-defined Local Variables</a><a class="headerlink" href="#user-defined-local-variables" title="Permalink to this headline">¶</a></h2>
+<p>Adding var/in is just like any other other extensions we made to
+Kaleidoscope: we extend the lexer, the parser, the AST and the code
+generator. The first step for adding our new ‘var/in’ construct is to
+extend the lexer. As before, this is pretty trivial, the code looks like
+this:</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="k">type</span> <span class="n">token</span> <span class="o">=</span>
+  <span class="o">...</span>
+  <span class="c">(* var definition *)</span>
+  <span class="o">|</span> <span class="nn">Var</span>
+
+<span class="p">...</span>
+
+<span class="n">and</span> <span class="n">lex_ident</span> <span class="n">buffer</span> <span class="o">=</span> <span class="n">parser</span>
+      <span class="o">...</span>
+      <span class="o">|</span> <span class="s2">"in"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">In</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+      <span class="o">|</span> <span class="s2">"binary"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Binary</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+      <span class="o">|</span> <span class="s2">"unary"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Unary</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+      <span class="o">|</span> <span class="s2">"var"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Var</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+      <span class="o">...</span>
+</pre></div>
+</div>
+<p>The next step is to define the AST node that we will construct. For
+var/in, it looks like this:</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="k">type</span> <span class="n">expr</span> <span class="o">=</span>
+  <span class="o">...</span>
+  <span class="c">(* variant for var/in. *)</span>
+  <span class="o">|</span> <span class="nc">Var</span> <span class="k">of</span> <span class="o">(</span><span class="kt">string</span> <span class="o">*</span> <span class="n">expr</span> <span class="n">option</span><span class="o">)</span> <span class="kt">array</span> <span class="o">*</span> <span class="n">expr</span>
+  <span class="o">...</span>
+</pre></div>
+</div>
+<p>var/in allows a list of names to be defined all at once, and each name
+can optionally have an initializer value. As such, we capture this
+information in the VarNames vector. Also, var/in has a body, this body
+is allowed to access the variables defined by the var/in.</p>
+<p>With this in place, we can define the parser pieces. The first thing we
+do is add it as a primary expression:</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="c">(* primary</span>
+<span class="c"> *   ::= identifier</span>
+<span class="c"> *   ::= numberexpr</span>
+<span class="c"> *   ::= parenexpr</span>
+<span class="c"> *   ::= ifexpr</span>
+<span class="c"> *   ::= forexpr</span>
+<span class="c"> *   ::= varexpr *)</span>
+<span class="k">let</span> <span class="k">rec</span> <span class="n">parse_primary</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="o">...</span>
+  <span class="c">(* varexpr</span>
+<span class="c">   *   ::= 'var' identifier ('=' expression?</span>
+<span class="c">   *             (',' identifier ('=' expression)?)* 'in' expression *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Var</span><span class="o">;</span>
+       <span class="c">(* At least one variable name is required. *)</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Ident</span> <span class="n">id</span> <span class="o">??</span> <span class="s2">"expected identifier after var"</span><span class="o">;</span>
+       <span class="n">init</span><span class="o">=</span><span class="n">parse_var_init</span><span class="o">;</span>
+       <span class="n">var_names</span><span class="o">=</span><span class="n">parse_var_names</span> <span class="o">[(</span><span class="n">id</span><span class="o">,</span> <span class="n">init</span><span class="o">)];</span>
+       <span class="c">(* At this point, we have to have 'in'. *)</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">In</span> <span class="o">??</span> <span class="s2">"expected 'in' keyword after 'var'"</span><span class="o">;</span>
+       <span class="n">body</span><span class="o">=</span><span class="n">parse_expr</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="nn">Ast</span><span class="p">.</span><span class="nc">Var</span> <span class="o">(</span><span class="nn">Array</span><span class="p">.</span><span class="n">of_list</span> <span class="o">(</span><span class="nn">List</span><span class="p">.</span><span class="n">rev</span> <span class="n">var_names</span><span class="o">),</span> <span class="n">body</span><span class="o">)</span>
+
+<span class="o">...</span>
+
+<span class="ow">and</span> <span class="n">parse_var_init</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="c">(* read in the optional initializer. *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">'='</span><span class="o">;</span> <span class="n">e</span><span class="o">=</span><span class="n">parse_expr</span> <span class="o">>]</span> <span class="o">-></span> <span class="nc">Some</span> <span class="n">e</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span> <span class="nc">None</span>
+
+<span class="ow">and</span> <span class="n">parse_var_names</span> <span class="n">accumulator</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">','</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Ident</span> <span class="n">id</span> <span class="o">??</span> <span class="s2">"expected identifier list after var"</span><span class="o">;</span>
+       <span class="n">init</span><span class="o">=</span><span class="n">parse_var_init</span><span class="o">;</span>
+       <span class="n">e</span><span class="o">=</span><span class="n">parse_var_names</span> <span class="o">((</span><span class="n">id</span><span class="o">,</span> <span class="n">init</span><span class="o">)</span> <span class="o">::</span> <span class="n">accumulator</span><span class="o">)</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">e</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">accumulator</span>
+</pre></div>
+</div>
+<p>Now that we can parse and represent the code, we need to support
+emission of LLVM IR for it. This code starts out with:</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="k">let</span> <span class="k">rec</span> <span class="n">codegen_expr</span> <span class="o">=</span> <span class="k">function</span>
+  <span class="o">...</span>
+  <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Var</span> <span class="o">(</span><span class="n">var_names</span><span class="o">,</span> <span class="n">body</span><span class="o">)</span>
+      <span class="k">let</span> <span class="n">old_bindings</span> <span class="o">=</span> <span class="n">ref</span> <span class="bp">[]</span> <span class="k">in</span>
+
+      <span class="k">let</span> <span class="n">the_function</span> <span class="o">=</span> <span class="n">block_parent</span> <span class="o">(</span><span class="n">insertion_block</span> <span class="n">builder</span><span class="o">)</span> <span class="k">in</span>
+
+      <span class="c">(* Register all variables and emit their initializer. *)</span>
+      <span class="nn">Array</span><span class="p">.</span><span class="n">iter</span> <span class="o">(</span><span class="k">fun</span> <span class="o">(</span><span class="n">var_name</span><span class="o">,</span> <span class="n">init</span><span class="o">)</span> <span class="o">-></span>
+</pre></div>
+</div>
+<p>Basically it loops over all the variables, installing them one at a
+time. For each variable we put into the symbol table, we remember the
+previous value that we replace in OldBindings.</p>
+<div class="highlight-ocaml"><div class="highlight"><pre>  <span class="c">(* Emit the initializer before adding the variable to scope, this</span>
+<span class="c">   * prevents the initializer from referencing the variable itself, and</span>
+<span class="c">   * permits stuff like this:</span>
+<span class="c">   *   var a = 1 in</span>
+<span class="c">   *     var a = a in ...   # refers to outer 'a'. *)</span>
+  <span class="k">let</span> <span class="n">init_val</span> <span class="o">=</span>
+    <span class="k">match</span> <span class="n">init</span> <span class="k">with</span>
+    <span class="o">|</span> <span class="nc">Some</span> <span class="n">init</span> <span class="o">-></span> <span class="n">codegen_expr</span> <span class="n">init</span>
+    <span class="c">(* If not specified, use 0.0. *)</span>
+    <span class="o">|</span> <span class="nc">None</span> <span class="o">-></span> <span class="n">const_float</span> <span class="n">double_type</span> <span class="mi">0</span><span class="o">.</span><span class="mi">0</span>
+  <span class="k">in</span>
+
+  <span class="k">let</span> <span class="n">alloca</span> <span class="o">=</span> <span class="n">create_entry_block_alloca</span> <span class="n">the_function</span> <span class="n">var_name</span> <span class="k">in</span>
+  <span class="n">ignore</span><span class="o">(</span><span class="n">build_store</span> <span class="n">init_val</span> <span class="n">alloca</span> <span class="n">builder</span><span class="o">);</span>
+
+  <span class="c">(* Remember the old variable binding so that we can restore the binding</span>
+<span class="c">   * when we unrecurse. *)</span>
+
+  <span class="k">begin</span>
+    <span class="k">try</span>
+      <span class="k">let</span> <span class="n">old_value</span> <span class="o">=</span> <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">find</span> <span class="n">named_values</span> <span class="n">var_name</span> <span class="k">in</span>
+      <span class="n">old_bindings</span> <span class="o">:=</span> <span class="o">(</span><span class="n">var_name</span><span class="o">,</span> <span class="n">old_value</span><span class="o">)</span> <span class="o">::</span> <span class="o">!</span><span class="n">old_bindings</span><span class="o">;</span>
+    <span class="k">with</span> <span class="nc">Not_found</span> <span class="o">></span> <span class="bp">()</span>
+  <span class="k">end</span><span class="o">;</span>
+
+  <span class="c">(* Remember this binding. *)</span>
+  <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">add</span> <span class="n">named_values</span> <span class="n">var_name</span> <span class="n">alloca</span><span class="o">;</span>
+<span class="o">)</span> <span class="n">var_names</span><span class="o">;</span>
+</pre></div>
+</div>
+<p>There are more comments here than code. The basic idea is that we emit
+the initializer, create the alloca, then update the symbol table to
+point to it. Once all the variables are installed in the symbol table,
+we evaluate the body of the var/in expression:</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="c">(* Codegen the body, now that all vars are in scope. *)</span>
+<span class="k">let</span> <span class="n">body_val</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">body</span> <span class="k">in</span>
+</pre></div>
+</div>
+<p>Finally, before returning, we restore the previous variable bindings:</p>
+<div class="highlight-ocaml"><div class="highlight"><pre><span class="c">(* Pop all our variables from scope. *)</span>
+<span class="nn">List</span><span class="p">.</span><span class="n">iter</span> <span class="o">(</span><span class="k">fun</span> <span class="o">(</span><span class="n">var_name</span><span class="o">,</span> <span class="n">old_value</span><span class="o">)</span> <span class="o">-></span>
+  <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">add</span> <span class="n">named_values</span> <span class="n">var_name</span> <span class="n">old_value</span>
+<span class="o">)</span> <span class="o">!</span><span class="n">old_bindings</span><span class="o">;</span>
+
+<span class="c">(* Return the body computation. *)</span>
+<span class="n">body_val</span>
+</pre></div>
+</div>
+<p>The end result of all of this is that we get properly scoped variable
+definitions, and we even (trivially) allow mutation of them :).</p>
+<p>With this, we completed what we set out to do. Our nice iterative fib
+example from the intro compiles and runs just fine. The mem2reg pass
+optimizes all of our stack variables into SSA registers, inserting PHI
+nodes where needed, and our front-end remains simple: no “iterated
+dominance frontier” computation anywhere in sight.</p>
+</div>
+<div class="section" id="id1">
+<h2><a class="toc-backref" href="#id9">7.8. Full Code Listing</a><a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h2>
+<p>Here is the complete code listing for our running example, enhanced with
+mutable variables and var/in support. To build this example, use:</p>
+<div class="highlight-bash"><div class="highlight"><pre><span class="c"># Compile</span>
+ocamlbuild toy.byte
+<span class="c"># Run</span>
+./toy.byte
+</pre></div>
+</div>
+<p>Here is the code:</p>
+<dl class="docutils">
+<dt>_tags:</dt>
+<dd><div class="first last highlight-python"><div class="highlight"><pre><{lexer,parser}.ml>: use_camlp4, pp(camlp4of)
+<*.{byte,native}>: g++, use_llvm, use_llvm_analysis
+<*.{byte,native}>: use_llvm_executionengine, use_llvm_target
+<*.{byte,native}>: use_llvm_scalar_opts, use_bindings
+</pre></div>
+</div>
+</dd>
+<dt>myocamlbuild.ml:</dt>
+<dd><div class="first last highlight-ocaml"><div class="highlight"><pre><span class="k">open</span> <span class="nc">Ocamlbuild_plugin</span><span class="o">;;</span>
+
+<span class="n">ocaml_lib</span> <span class="o">~</span><span class="n">extern</span><span class="o">:</span><span class="bp">true</span> <span class="s2">"llvm"</span><span class="o">;;</span>
+<span class="n">ocaml_lib</span> <span class="o">~</span><span class="n">extern</span><span class="o">:</span><span class="bp">true</span> <span class="s2">"llvm_analysis"</span><span class="o">;;</span>
+<span class="n">ocaml_lib</span> <span class="o">~</span><span class="n">extern</span><span class="o">:</span><span class="bp">true</span> <span class="s2">"llvm_executionengine"</span><span class="o">;;</span>
+<span class="n">ocaml_lib</span> <span class="o">~</span><span class="n">extern</span><span class="o">:</span><span class="bp">true</span> <span class="s2">"llvm_target"</span><span class="o">;;</span>
+<span class="n">ocaml_lib</span> <span class="o">~</span><span class="n">extern</span><span class="o">:</span><span class="bp">true</span> <span class="s2">"llvm_scalar_opts"</span><span class="o">;;</span>
+
+<span class="n">flag</span> <span class="o">[</span><span class="s2">"link"</span><span class="o">;</span> <span class="s2">"ocaml"</span><span class="o">;</span> <span class="s2">"g++"</span><span class="o">]</span> <span class="o">(</span><span class="nc">S</span><span class="o">[</span><span class="nc">A</span><span class="s2">"-cc"</span><span class="o">;</span> <span class="nc">A</span><span class="s2">"g++"</span><span class="o">;</span> <span class="nc">A</span><span class="s2">"-cclib"</span><span class="o">;</span> <span class="nc">A</span><span class="s2">"-rdynamic"</span><span class="o">]);;</span>
+<span class="n">dep</span> <span class="o">[</span><span class="s2">"link"</span><span class="o">;</span> <span class="s2">"ocaml"</span><span class="o">;</span> <span class="s2">"use_bindings"</span><span class="o">]</span> <span class="o">[</span><span class="s2">"bindings.o"</span><span class="o">];;</span>
+</pre></div>
+</div>
+</dd>
+<dt>token.ml:</dt>
+<dd><div class="first last highlight-ocaml"><div class="highlight"><pre><span class="c">(*===----------------------------------------------------------------------===</span>
+<span class="c"> * Lexer Tokens</span>
+<span class="c"> *===----------------------------------------------------------------------===*)</span>
+
+<span class="c">(* The lexer returns these 'Kwd' if it is an unknown character, otherwise one of</span>
+<span class="c"> * these others for known things. *)</span>
+<span class="k">type</span> <span class="n">token</span> <span class="o">=</span>
+  <span class="c">(* commands *)</span>
+  <span class="o">|</span> <span class="nc">Def</span> <span class="o">|</span> <span class="nc">Extern</span>
+
+  <span class="c">(* primary *)</span>
+  <span class="o">|</span> <span class="nc">Ident</span> <span class="k">of</span> <span class="kt">string</span> <span class="o">|</span> <span class="nc">Number</span> <span class="k">of</span> <span class="kt">float</span>
+
+  <span class="c">(* unknown *)</span>
+  <span class="o">|</span> <span class="nc">Kwd</span> <span class="k">of</span> <span class="kt">char</span>
+
+  <span class="c">(* control *)</span>
+  <span class="o">|</span> <span class="nc">If</span> <span class="o">|</span> <span class="nc">Then</span> <span class="o">|</span> <span class="nc">Else</span>
+  <span class="o">|</span> <span class="nc">For</span> <span class="o">|</span> <span class="nc">In</span>
+
+  <span class="c">(* operators *)</span>
+  <span class="o">|</span> <span class="nc">Binary</span> <span class="o">|</span> <span class="nc">Unary</span>
+
+  <span class="c">(* var definition *)</span>
+  <span class="o">|</span> <span class="nc">Var</span>
+</pre></div>
+</div>
+</dd>
+<dt>lexer.ml:</dt>
+<dd><div class="first last highlight-ocaml"><div class="highlight"><pre><span class="c">(*===----------------------------------------------------------------------===</span>
+<span class="c"> * Lexer</span>
+<span class="c"> *===----------------------------------------------------------------------===*)</span>
+
+<span class="k">let</span> <span class="k">rec</span> <span class="n">lex</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="c">(* Skip any whitespace. *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span> <span class="o">(</span><span class="sc">' '</span> <span class="o">|</span> <span class="sc">'\n'</span> <span class="o">|</span> <span class="sc">'\r'</span> <span class="o">|</span> <span class="sc">'\t'</span><span class="o">);</span> <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">lex</span> <span class="n">stream</span>
+
+  <span class="c">(* identifier: [a-zA-Z][a-zA-Z0-9] *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span> <span class="o">(</span><span class="sc">'A'</span> <span class="o">..</span> <span class="sc">'Z'</span> <span class="o">|</span> <span class="sc">'a'</span> <span class="o">..</span> <span class="sc">'z'</span> <span class="k">as</span> <span class="n">c</span><span class="o">);</span> <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="k">let</span> <span class="n">buffer</span> <span class="o">=</span> <span class="nn">Buffer</span><span class="p">.</span><span class="n">create</span> <span class="mi">1</span> <span class="k">in</span>
+      <span class="nn">Buffer</span><span class="p">.</span><span class="n">add_char</span> <span class="n">buffer</span> <span class="n">c</span><span class="o">;</span>
+      <span class="n">lex_ident</span> <span class="n">buffer</span> <span class="n">stream</span>
+
+  <span class="c">(* number: [0-9.]+ *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span> <span class="o">(</span><span class="sc">'0'</span> <span class="o">..</span> <span class="sc">'9'</span> <span class="k">as</span> <span class="n">c</span><span class="o">);</span> <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="k">let</span> <span class="n">buffer</span> <span class="o">=</span> <span class="nn">Buffer</span><span class="p">.</span><span class="n">create</span> <span class="mi">1</span> <span class="k">in</span>
+      <span class="nn">Buffer</span><span class="p">.</span><span class="n">add_char</span> <span class="n">buffer</span> <span class="n">c</span><span class="o">;</span>
+      <span class="n">lex_number</span> <span class="n">buffer</span> <span class="n">stream</span>
+
+  <span class="c">(* Comment until end of line. *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span> <span class="o">(</span><span class="sc">'#'</span><span class="o">);</span> <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="n">lex_comment</span> <span class="n">stream</span>
+
+  <span class="c">(* Otherwise, just return the character as its ascii value. *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="n">c</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="n">c</span><span class="o">;</span> <span class="n">lex</span> <span class="n">stream</span> <span class="o">>]</span>
+
+  <span class="c">(* end of stream. *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span> <span class="o">[<</span> <span class="o">>]</span>
+
+<span class="ow">and</span> <span class="n">lex_number</span> <span class="n">buffer</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span> <span class="o">(</span><span class="sc">'0'</span> <span class="o">..</span> <span class="sc">'9'</span> <span class="o">|</span> <span class="sc">'.'</span> <span class="k">as</span> <span class="n">c</span><span class="o">);</span> <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="nn">Buffer</span><span class="p">.</span><span class="n">add_char</span> <span class="n">buffer</span> <span class="n">c</span><span class="o">;</span>
+      <span class="n">lex_number</span> <span class="n">buffer</span> <span class="n">stream</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="n">stream</span><span class="o">=</span><span class="n">lex</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Number</span> <span class="o">(</span><span class="n">float_of_string</span> <span class="o">(</span><span class="nn">Buffer</span><span class="p">.</span><span class="n">contents</span> <span class="n">buffer</span><span class="o">));</span> <span class="n">stream</span> <span class="o">>]</span>
+
+<span class="ow">and</span> <span class="n">lex_ident</span> <span class="n">buffer</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span> <span class="o">(</span><span class="sc">'A'</span> <span class="o">..</span> <span class="sc">'Z'</span> <span class="o">|</span> <span class="sc">'a'</span> <span class="o">..</span> <span class="sc">'z'</span> <span class="o">|</span> <span class="sc">'0'</span> <span class="o">..</span> <span class="sc">'9'</span> <span class="k">as</span> <span class="n">c</span><span class="o">);</span> <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="nn">Buffer</span><span class="p">.</span><span class="n">add_char</span> <span class="n">buffer</span> <span class="n">c</span><span class="o">;</span>
+      <span class="n">lex_ident</span> <span class="n">buffer</span> <span class="n">stream</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="n">stream</span><span class="o">=</span><span class="n">lex</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="k">match</span> <span class="nn">Buffer</span><span class="p">.</span><span class="n">contents</span> <span class="n">buffer</span> <span class="k">with</span>
+      <span class="o">|</span> <span class="s2">"def"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Def</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+      <span class="o">|</span> <span class="s2">"extern"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Extern</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+      <span class="o">|</span> <span class="s2">"if"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">If</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+      <span class="o">|</span> <span class="s2">"then"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Then</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+      <span class="o">|</span> <span class="s2">"else"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Else</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+      <span class="o">|</span> <span class="s2">"for"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">For</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+      <span class="o">|</span> <span class="s2">"in"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">In</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+      <span class="o">|</span> <span class="s2">"binary"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Binary</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+      <span class="o">|</span> <span class="s2">"unary"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Unary</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+      <span class="o">|</span> <span class="s2">"var"</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Var</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+      <span class="o">|</span> <span class="n">id</span> <span class="o">-></span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Ident</span> <span class="n">id</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span>
+
+<span class="ow">and</span> <span class="n">lex_comment</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span> <span class="o">(</span><span class="sc">'\n'</span><span class="o">);</span> <span class="n">stream</span><span class="o">=</span><span class="n">lex</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">stream</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="n">c</span><span class="o">;</span> <span class="n">e</span><span class="o">=</span><span class="n">lex_comment</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">e</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span> <span class="o">[<</span> <span class="o">>]</span>
+</pre></div>
+</div>
+</dd>
+<dt>ast.ml:</dt>
+<dd><div class="first last highlight-ocaml"><div class="highlight"><pre><span class="c">(*===----------------------------------------------------------------------===</span>
+<span class="c"> * Abstract Syntax Tree (aka Parse Tree)</span>
+<span class="c"> *===----------------------------------------------------------------------===*)</span>
+
+<span class="c">(* expr - Base type for all expression nodes. *)</span>
+<span class="k">type</span> <span class="n">expr</span> <span class="o">=</span>
+  <span class="c">(* variant for numeric literals like "1.0". *)</span>
+  <span class="o">|</span> <span class="nc">Number</span> <span class="k">of</span> <span class="kt">float</span>
+
+  <span class="c">(* variant for referencing a variable, like "a". *)</span>
+  <span class="o">|</span> <span class="nc">Variable</span> <span class="k">of</span> <span class="kt">string</span>
+
+  <span class="c">(* variant for a unary operator. *)</span>
+  <span class="o">|</span> <span class="nc">Unary</span> <span class="k">of</span> <span class="kt">char</span> <span class="o">*</span> <span class="n">expr</span>
+
+  <span class="c">(* variant for a binary operator. *)</span>
+  <span class="o">|</span> <span class="nc">Binary</span> <span class="k">of</span> <span class="kt">char</span> <span class="o">*</span> <span class="n">expr</span> <span class="o">*</span> <span class="n">expr</span>
+
+  <span class="c">(* variant for function calls. *)</span>
+  <span class="o">|</span> <span class="nc">Call</span> <span class="k">of</span> <span class="kt">string</span> <span class="o">*</span> <span class="n">expr</span> <span class="kt">array</span>
+
+  <span class="c">(* variant for if/then/else. *)</span>
+  <span class="o">|</span> <span class="nc">If</span> <span class="k">of</span> <span class="n">expr</span> <span class="o">*</span> <span class="n">expr</span> <span class="o">*</span> <span class="n">expr</span>
+
+  <span class="c">(* variant for for/in. *)</span>
+  <span class="o">|</span> <span class="nc">For</span> <span class="k">of</span> <span class="kt">string</span> <span class="o">*</span> <span class="n">expr</span> <span class="o">*</span> <span class="n">expr</span> <span class="o">*</span> <span class="n">expr</span> <span class="n">option</span> <span class="o">*</span> <span class="n">expr</span>
+
+  <span class="c">(* variant for var/in. *)</span>
+  <span class="o">|</span> <span class="nc">Var</span> <span class="k">of</span> <span class="o">(</span><span class="kt">string</span> <span class="o">*</span> <span class="n">expr</span> <span class="n">option</span><span class="o">)</span> <span class="kt">array</span> <span class="o">*</span> <span class="n">expr</span>
+
+<span class="c">(* proto - This type represents the "prototype" for a function, which captures</span>
+<span class="c"> * its name, and its argument names (thus implicitly the number of arguments the</span>
+<span class="c"> * function takes). *)</span>
+<span class="k">type</span> <span class="n">proto</span> <span class="o">=</span>
+  <span class="o">|</span> <span class="nc">Prototype</span> <span class="k">of</span> <span class="kt">string</span> <span class="o">*</span> <span class="kt">string</span> <span class="kt">array</span>
+  <span class="o">|</span> <span class="nc">BinOpPrototype</span> <span class="k">of</span> <span class="kt">string</span> <span class="o">*</span> <span class="kt">string</span> <span class="kt">array</span> <span class="o">*</span> <span class="kt">int</span>
+
+<span class="c">(* func - This type represents a function definition itself. *)</span>
+<span class="k">type</span> <span class="n">func</span> <span class="o">=</span> <span class="nc">Function</span> <span class="k">of</span> <span class="n">proto</span> <span class="o">*</span> <span class="n">expr</span>
+</pre></div>
+</div>
+</dd>
+<dt>parser.ml:</dt>
+<dd><div class="first last highlight-ocaml"><div class="highlight"><pre><span class="c">(*===---------------------------------------------------------------------===</span>
+<span class="c"> * Parser</span>
+<span class="c"> *===---------------------------------------------------------------------===*)</span>
+
+<span class="c">(* binop_precedence - This holds the precedence for each binary operator that is</span>
+<span class="c"> * defined *)</span>
+<span class="k">let</span> <span class="n">binop_precedence</span><span class="o">:(</span><span class="kt">char</span><span class="o">,</span> <span class="kt">int</span><span class="o">)</span> <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">t</span> <span class="o">=</span> <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">create</span> <span class="mi">10</span>
+
+<span class="c">(* precedence - Get the precedence of the pending binary operator token. *)</span>
+<span class="k">let</span> <span class="n">precedence</span> <span class="n">c</span> <span class="o">=</span> <span class="k">try</span> <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">find</span> <span class="n">binop_precedence</span> <span class="n">c</span> <span class="k">with</span> <span class="nc">Not_found</span> <span class="o">-></span> <span class="o">-</span><span class="mi">1</span>
+
+<span class="c">(* primary</span>
+<span class="c"> *   ::= identifier</span>
+<span class="c"> *   ::= numberexpr</span>
+<span class="c"> *   ::= parenexpr</span>
+<span class="c"> *   ::= ifexpr</span>
+<span class="c"> *   ::= forexpr</span>
+<span class="c"> *   ::= varexpr *)</span>
+<span class="k">let</span> <span class="k">rec</span> <span class="n">parse_primary</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="c">(* numberexpr ::= number *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Number</span> <span class="n">n</span> <span class="o">>]</span> <span class="o">-></span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Number</span> <span class="n">n</span>
+
+  <span class="c">(* parenexpr ::= '(' expression ')' *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">'('</span><span class="o">;</span> <span class="n">e</span><span class="o">=</span><span class="n">parse_expr</span><span class="o">;</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">')'</span> <span class="o">??</span> <span class="s2">"expected ')'"</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">e</span>
+
+  <span class="c">(* identifierexpr</span>
+<span class="c">   *   ::= identifier</span>
+<span class="c">   *   ::= identifier '(' argumentexpr ')' *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Ident</span> <span class="n">id</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="k">let</span> <span class="k">rec</span> <span class="n">parse_args</span> <span class="n">accumulator</span> <span class="o">=</span> <span class="n">parser</span>
+        <span class="o">|</span> <span class="o">[<</span> <span class="n">e</span><span class="o">=</span><span class="n">parse_expr</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span>
+            <span class="k">begin</span> <span class="n">parser</span>
+              <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">','</span><span class="o">;</span> <span class="n">e</span><span class="o">=</span><span class="n">parse_args</span> <span class="o">(</span><span class="n">e</span> <span class="o">::</span> <span class="n">accumulator</span><span class="o">)</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">e</span>
+              <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">e</span> <span class="o">::</span> <span class="n">accumulator</span>
+            <span class="k">end</span> <span class="n">stream</span>
+        <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">accumulator</span>
+      <span class="k">in</span>
+      <span class="k">let</span> <span class="k">rec</span> <span class="n">parse_ident</span> <span class="n">id</span> <span class="o">=</span> <span class="n">parser</span>
+        <span class="c">(* Call. *)</span>
+        <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">'('</span><span class="o">;</span>
+             <span class="n">args</span><span class="o">=</span><span class="n">parse_args</span> <span class="bp">[]</span><span class="o">;</span>
+             <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">')'</span> <span class="o">??</span> <span class="s2">"expected ')'"</span><span class="o">>]</span> <span class="o">-></span>
+            <span class="nn">Ast</span><span class="p">.</span><span class="nc">Call</span> <span class="o">(</span><span class="n">id</span><span class="o">,</span> <span class="nn">Array</span><span class="p">.</span><span class="n">of_list</span> <span class="o">(</span><span class="nn">List</span><span class="p">.</span><span class="n">rev</span> <span class="n">args</span><span class="o">))</span>
+
+        <span class="c">(* Simple variable ref. *)</span>
+        <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Variable</span> <span class="n">id</span>
+      <span class="k">in</span>
+      <span class="n">parse_ident</span> <span class="n">id</span> <span class="n">stream</span>
+
+  <span class="c">(* ifexpr ::= 'if' expr 'then' expr 'else' expr *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">If</span><span class="o">;</span> <span class="n">c</span><span class="o">=</span><span class="n">parse_expr</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Then</span> <span class="o">??</span> <span class="s2">"expected 'then'"</span><span class="o">;</span> <span class="n">t</span><span class="o">=</span><span class="n">parse_expr</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Else</span> <span class="o">??</span> <span class="s2">"expected 'else'"</span><span class="o">;</span> <span class="n">e</span><span class="o">=</span><span class="n">parse_expr</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="nn">Ast</span><span class="p">.</span><span class="nc">If</span> <span class="o">(</span><span class="n">c</span><span class="o">,</span> <span class="n">t</span><span class="o">,</span> <span class="n">e</span><span class="o">)</span>
+
+  <span class="c">(* forexpr</span>
+<span class="c">        ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">For</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Ident</span> <span class="n">id</span> <span class="o">??</span> <span class="s2">"expected identifier after for"</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">'='</span> <span class="o">??</span> <span class="s2">"expected '=' after for"</span><span class="o">;</span>
+       <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="k">begin</span> <span class="n">parser</span>
+        <span class="o">|</span> <span class="o">[<</span>
+             <span class="n">start</span><span class="o">=</span><span class="n">parse_expr</span><span class="o">;</span>
+             <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">','</span> <span class="o">??</span> <span class="s2">"expected ',' after for"</span><span class="o">;</span>
+             <span class="n">end_</span><span class="o">=</span><span class="n">parse_expr</span><span class="o">;</span>
+             <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span>
+            <span class="k">let</span> <span class="n">step</span> <span class="o">=</span>
+              <span class="k">begin</span> <span class="n">parser</span>
+              <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">','</span><span class="o">;</span> <span class="n">step</span><span class="o">=</span><span class="n">parse_expr</span> <span class="o">>]</span> <span class="o">-></span> <span class="nc">Some</span> <span class="n">step</span>
+              <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span> <span class="nc">None</span>
+              <span class="k">end</span> <span class="n">stream</span>
+            <span class="k">in</span>
+            <span class="k">begin</span> <span class="n">parser</span>
+            <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">In</span><span class="o">;</span> <span class="n">body</span><span class="o">=</span><span class="n">parse_expr</span> <span class="o">>]</span> <span class="o">-></span>
+                <span class="nn">Ast</span><span class="p">.</span><span class="nc">For</span> <span class="o">(</span><span class="n">id</span><span class="o">,</span> <span class="n">start</span><span class="o">,</span> <span class="n">end_</span><span class="o">,</span> <span class="n">step</span><span class="o">,</span> <span class="n">body</span><span class="o">)</span>
+            <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span>
+                <span class="k">raise</span> <span class="o">(</span><span class="nn">Stream</span><span class="p">.</span><span class="nc">Error</span> <span class="s2">"expected 'in' after for"</span><span class="o">)</span>
+            <span class="k">end</span> <span class="n">stream</span>
+        <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span>
+            <span class="k">raise</span> <span class="o">(</span><span class="nn">Stream</span><span class="p">.</span><span class="nc">Error</span> <span class="s2">"expected '=' after for"</span><span class="o">)</span>
+      <span class="k">end</span> <span class="n">stream</span>
+
+  <span class="c">(* varexpr</span>
+<span class="c">   *   ::= 'var' identifier ('=' expression?</span>
+<span class="c">   *             (',' identifier ('=' expression)?)* 'in' expression *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Var</span><span class="o">;</span>
+       <span class="c">(* At least one variable name is required. *)</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Ident</span> <span class="n">id</span> <span class="o">??</span> <span class="s2">"expected identifier after var"</span><span class="o">;</span>
+       <span class="n">init</span><span class="o">=</span><span class="n">parse_var_init</span><span class="o">;</span>
+       <span class="n">var_names</span><span class="o">=</span><span class="n">parse_var_names</span> <span class="o">[(</span><span class="n">id</span><span class="o">,</span> <span class="n">init</span><span class="o">)];</span>
+       <span class="c">(* At this point, we have to have 'in'. *)</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">In</span> <span class="o">??</span> <span class="s2">"expected 'in' keyword after 'var'"</span><span class="o">;</span>
+       <span class="n">body</span><span class="o">=</span><span class="n">parse_expr</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="nn">Ast</span><span class="p">.</span><span class="nc">Var</span> <span class="o">(</span><span class="nn">Array</span><span class="p">.</span><span class="n">of_list</span> <span class="o">(</span><span class="nn">List</span><span class="p">.</span><span class="n">rev</span> <span class="n">var_names</span><span class="o">),</span> <span class="n">body</span><span class="o">)</span>
+
+  <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span> <span class="k">raise</span> <span class="o">(</span><span class="nn">Stream</span><span class="p">.</span><span class="nc">Error</span> <span class="s2">"unknown token when expecting an expression."</span><span class="o">)</span>
+
+<span class="c">(* unary</span>
+<span class="c"> *   ::= primary</span>
+<span class="c"> *   ::= '!' unary *)</span>
+<span class="ow">and</span> <span class="n">parse_unary</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="c">(* If this is a unary operator, read it. *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="n">op</span> <span class="k">when</span> <span class="n">op</span> <span class="o">!=</span> <span class="sc">'('</span> <span class="o">&&</span> <span class="n">op</span> <span class="o">!=</span> <span class="sc">')'</span><span class="o">;</span> <span class="n">operand</span><span class="o">=</span><span class="n">parse_expr</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="nn">Ast</span><span class="p">.</span><span class="nc">Unary</span> <span class="o">(</span><span class="n">op</span><span class="o">,</span> <span class="n">operand</span><span class="o">)</span>
+
+  <span class="c">(* If the current token is not an operator, it must be a primary expr. *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">parse_primary</span> <span class="n">stream</span>
+
+<span class="c">(* binoprhs</span>
+<span class="c"> *   ::= ('+' primary)* *)</span>
+<span class="ow">and</span> <span class="n">parse_bin_rhs</span> <span class="n">expr_prec</span> <span class="n">lhs</span> <span class="n">stream</span> <span class="o">=</span>
+  <span class="k">match</span> <span class="nn">Stream</span><span class="p">.</span><span class="n">peek</span> <span class="n">stream</span> <span class="k">with</span>
+  <span class="c">(* If this is a binop, find its precedence. *)</span>
+  <span class="o">|</span> <span class="nc">Some</span> <span class="o">(</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="n">c</span><span class="o">)</span> <span class="k">when</span> <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">mem</span> <span class="n">binop_precedence</span> <span class="n">c</span> <span class="o">-></span>
+      <span class="k">let</span> <span class="n">token_prec</span> <span class="o">=</span> <span class="n">precedence</span> <span class="n">c</span> <span class="k">in</span>
+
+      <span class="c">(* If this is a binop that binds at least as tightly as the current binop,</span>
+<span class="c">       * consume it, otherwise we are done. *)</span>
+      <span class="k">if</span> <span class="n">token_prec</span> <span class="o"><</span> <span class="n">expr_prec</span> <span class="k">then</span> <span class="n">lhs</span> <span class="k">else</span> <span class="k">begin</span>
+        <span class="c">(* Eat the binop. *)</span>
+        <span class="nn">Stream</span><span class="p">.</span><span class="n">junk</span> <span class="n">stream</span><span class="o">;</span>
+
+        <span class="c">(* Parse the primary expression after the binary operator. *)</span>
+        <span class="k">let</span> <span class="n">rhs</span> <span class="o">=</span> <span class="n">parse_unary</span> <span class="n">stream</span> <span class="k">in</span>
+
+        <span class="c">(* Okay, we know this is a binop. *)</span>
+        <span class="k">let</span> <span class="n">rhs</span> <span class="o">=</span>
+          <span class="k">match</span> <span class="nn">Stream</span><span class="p">.</span><span class="n">peek</span> <span class="n">stream</span> <span class="k">with</span>
+          <span class="o">|</span> <span class="nc">Some</span> <span class="o">(</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="n">c2</span><span class="o">)</span> <span class="o">-></span>
+              <span class="c">(* If BinOp binds less tightly with rhs than the operator after</span>
+<span class="c">               * rhs, let the pending operator take rhs as its lhs. *)</span>
+              <span class="k">let</span> <span class="n">next_prec</span> <span class="o">=</span> <span class="n">precedence</span> <span class="n">c2</span> <span class="k">in</span>
+              <span class="k">if</span> <span class="n">token_prec</span> <span class="o"><</span> <span class="n">next_prec</span>
+              <span class="k">then</span> <span class="n">parse_bin_rhs</span> <span class="o">(</span><span class="n">token_prec</span> <span class="o">+</span> <span class="mi">1</span><span class="o">)</span> <span class="n">rhs</span> <span class="n">stream</span>
+              <span class="k">else</span> <span class="n">rhs</span>
+          <span class="o">|</span> <span class="o">_</span> <span class="o">-></span> <span class="n">rhs</span>
+        <span class="k">in</span>
+
+        <span class="c">(* Merge lhs/rhs. *)</span>
+        <span class="k">let</span> <span class="n">lhs</span> <span class="o">=</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Binary</span> <span class="o">(</span><span class="n">c</span><span class="o">,</span> <span class="n">lhs</span><span class="o">,</span> <span class="n">rhs</span><span class="o">)</span> <span class="k">in</span>
+        <span class="n">parse_bin_rhs</span> <span class="n">expr_prec</span> <span class="n">lhs</span> <span class="n">stream</span>
+      <span class="k">end</span>
+  <span class="o">|</span> <span class="o">_</span> <span class="o">-></span> <span class="n">lhs</span>
+
+<span class="ow">and</span> <span class="n">parse_var_init</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="c">(* read in the optional initializer. *)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">'='</span><span class="o">;</span> <span class="n">e</span><span class="o">=</span><span class="n">parse_expr</span> <span class="o">>]</span> <span class="o">-></span> <span class="nc">Some</span> <span class="n">e</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span> <span class="nc">None</span>
+
+<span class="ow">and</span> <span class="n">parse_var_names</span> <span class="n">accumulator</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">','</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Ident</span> <span class="n">id</span> <span class="o">??</span> <span class="s2">"expected identifier list after var"</span><span class="o">;</span>
+       <span class="n">init</span><span class="o">=</span><span class="n">parse_var_init</span><span class="o">;</span>
+       <span class="n">e</span><span class="o">=</span><span class="n">parse_var_names</span> <span class="o">((</span><span class="n">id</span><span class="o">,</span> <span class="n">init</span><span class="o">)</span> <span class="o">::</span> <span class="n">accumulator</span><span class="o">)</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">e</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">accumulator</span>
+
+<span class="c">(* expression</span>
+<span class="c"> *   ::= primary binoprhs *)</span>
+<span class="ow">and</span> <span class="n">parse_expr</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="n">lhs</span><span class="o">=</span><span class="n">parse_unary</span><span class="o">;</span> <span class="n">stream</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">parse_bin_rhs</span> <span class="mi">0</span> <span class="n">lhs</span> <span class="n">stream</span>
+
+<span class="c">(* prototype</span>
+<span class="c"> *   ::= id '(' id* ')'</span>
+<span class="c"> *   ::= binary LETTER number? (id, id)</span>
+<span class="c"> *   ::= unary LETTER number? (id) *)</span>
+<span class="k">let</span> <span class="n">parse_prototype</span> <span class="o">=</span>
+  <span class="k">let</span> <span class="k">rec</span> <span class="n">parse_args</span> <span class="n">accumulator</span> <span class="o">=</span> <span class="n">parser</span>
+    <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Ident</span> <span class="n">id</span><span class="o">;</span> <span class="n">e</span><span class="o">=</span><span class="n">parse_args</span> <span class="o">(</span><span class="n">id</span><span class="o">::</span><span class="n">accumulator</span><span class="o">)</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">e</span>
+    <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">accumulator</span>
+  <span class="k">in</span>
+  <span class="k">let</span> <span class="n">parse_operator</span> <span class="o">=</span> <span class="n">parser</span>
+    <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Unary</span> <span class="o">>]</span> <span class="o">-></span> <span class="s2">"unary"</span><span class="o">,</span> <span class="mi">1</span>
+    <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Binary</span> <span class="o">>]</span> <span class="o">-></span> <span class="s2">"binary"</span><span class="o">,</span> <span class="mi">2</span>
+  <span class="k">in</span>
+  <span class="k">let</span> <span class="n">parse_binary_precedence</span> <span class="o">=</span> <span class="n">parser</span>
+    <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Number</span> <span class="n">n</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">int_of_float</span> <span class="n">n</span>
+    <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span> <span class="mi">30</span>
+  <span class="k">in</span>
+  <span class="n">parser</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Ident</span> <span class="n">id</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">'('</span> <span class="o">??</span> <span class="s2">"expected '(' in prototype"</span><span class="o">;</span>
+       <span class="n">args</span><span class="o">=</span><span class="n">parse_args</span> <span class="bp">[]</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">')'</span> <span class="o">??</span> <span class="s2">"expected ')' in prototype"</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="c">(* success. *)</span>
+      <span class="nn">Ast</span><span class="p">.</span><span class="nc">Prototype</span> <span class="o">(</span><span class="n">id</span><span class="o">,</span> <span class="nn">Array</span><span class="p">.</span><span class="n">of_list</span> <span class="o">(</span><span class="nn">List</span><span class="p">.</span><span class="n">rev</span> <span class="n">args</span><span class="o">))</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="o">(</span><span class="n">prefix</span><span class="o">,</span> <span class="n">kind</span><span class="o">)=</span><span class="n">parse_operator</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="n">op</span> <span class="o">??</span> <span class="s2">"expected an operator"</span><span class="o">;</span>
+       <span class="c">(* Read the precedence if present. *)</span>
+       <span class="n">binary_precedence</span><span class="o">=</span><span class="n">parse_binary_precedence</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">'('</span> <span class="o">??</span> <span class="s2">"expected '(' in prototype"</span><span class="o">;</span>
+        <span class="n">args</span><span class="o">=</span><span class="n">parse_args</span> <span class="bp">[]</span><span class="o">;</span>
+       <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">')'</span> <span class="o">??</span> <span class="s2">"expected ')' in prototype"</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="k">let</span> <span class="n">name</span> <span class="o">=</span> <span class="n">prefix</span> <span class="o">^</span> <span class="o">(</span><span class="nn">String</span><span class="p">.</span><span class="n">make</span> <span class="mi">1</span> <span class="n">op</span><span class="o">)</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">args</span> <span class="o">=</span> <span class="nn">Array</span><span class="p">.</span><span class="n">of_list</span> <span class="o">(</span><span class="nn">List</span><span class="p">.</span><span class="n">rev</span> <span class="n">args</span><span class="o">)</span> <span class="k">in</span>
+
+      <span class="c">(* Verify right number of arguments for operator. *)</span>
+      <span class="k">if</span> <span class="nn">Array</span><span class="p">.</span><span class="n">length</span> <span class="n">args</span> <span class="o">!=</span> <span class="n">kind</span>
+      <span class="k">then</span> <span class="k">raise</span> <span class="o">(</span><span class="nn">Stream</span><span class="p">.</span><span class="nc">Error</span> <span class="s2">"invalid number of operands for operator"</span><span class="o">)</span>
+      <span class="k">else</span>
+        <span class="k">if</span> <span class="n">kind</span> <span class="o">==</span> <span class="mi">1</span> <span class="k">then</span>
+          <span class="nn">Ast</span><span class="p">.</span><span class="nc">Prototype</span> <span class="o">(</span><span class="n">name</span><span class="o">,</span> <span class="n">args</span><span class="o">)</span>
+        <span class="k">else</span>
+          <span class="nn">Ast</span><span class="p">.</span><span class="nc">BinOpPrototype</span> <span class="o">(</span><span class="n">name</span><span class="o">,</span> <span class="n">args</span><span class="o">,</span> <span class="n">binary_precedence</span><span class="o">)</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="k">raise</span> <span class="o">(</span><span class="nn">Stream</span><span class="p">.</span><span class="nc">Error</span> <span class="s2">"expected function name in prototype"</span><span class="o">)</span>
+
+<span class="c">(* definition ::= 'def' prototype expression *)</span>
+<span class="k">let</span> <span class="n">parse_definition</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Def</span><span class="o">;</span> <span class="n">p</span><span class="o">=</span><span class="n">parse_prototype</span><span class="o">;</span> <span class="n">e</span><span class="o">=</span><span class="n">parse_expr</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="nn">Ast</span><span class="p">.</span><span class="nc">Function</span> <span class="o">(</span><span class="n">p</span><span class="o">,</span> <span class="n">e</span><span class="o">)</span>
+
+<span class="c">(* toplevelexpr ::= expression *)</span>
+<span class="k">let</span> <span class="n">parse_toplevel</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="n">e</span><span class="o">=</span><span class="n">parse_expr</span> <span class="o">>]</span> <span class="o">-></span>
+      <span class="c">(* Make an anonymous proto. *)</span>
+      <span class="nn">Ast</span><span class="p">.</span><span class="nc">Function</span> <span class="o">(</span><span class="nn">Ast</span><span class="p">.</span><span class="nc">Prototype</span> <span class="o">(</span><span class="s2">""</span><span class="o">,</span> <span class="o">[||]),</span> <span class="n">e</span><span class="o">)</span>
+
+<span class="c">(*  external ::= 'extern' prototype *)</span>
+<span class="k">let</span> <span class="n">parse_extern</span> <span class="o">=</span> <span class="n">parser</span>
+  <span class="o">|</span> <span class="o">[<</span> <span class="k">'</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Extern</span><span class="o">;</span> <span class="n">e</span><span class="o">=</span><span class="n">parse_prototype</span> <span class="o">>]</span> <span class="o">-></span> <span class="n">e</span>
+</pre></div>
+</div>
+</dd>
+<dt>codegen.ml:</dt>
+<dd><div class="first last highlight-ocaml"><div class="highlight"><pre><span class="c">(*===----------------------------------------------------------------------===</span>
+<span class="c"> * Code Generation</span>
+<span class="c"> *===----------------------------------------------------------------------===*)</span>
+
+<span class="k">open</span> <span class="nc">Llvm</span>
+
+<span class="k">exception</span> <span class="nc">Error</span> <span class="k">of</span> <span class="kt">string</span>
+
+<span class="k">let</span> <span class="n">context</span> <span class="o">=</span> <span class="n">global_context</span> <span class="bp">()</span>
+<span class="k">let</span> <span class="n">the_module</span> <span class="o">=</span> <span class="n">create_module</span> <span class="n">context</span> <span class="s2">"my cool jit"</span>
+<span class="k">let</span> <span class="n">builder</span> <span class="o">=</span> <span class="n">builder</span> <span class="n">context</span>
+<span class="k">let</span> <span class="n">named_values</span><span class="o">:(</span><span class="kt">string</span><span class="o">,</span> <span class="n">llvalue</span><span class="o">)</span> <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">t</span> <span class="o">=</span> <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">create</span> <span class="mi">10</span>
+<span class="k">let</span> <span class="n">double_type</span> <span class="o">=</span> <span class="n">double_type</span> <span class="n">context</span>
+
+<span class="c">(* Create an alloca instruction in the entry block of the function. This</span>
+<span class="c"> * is used for mutable variables etc. *)</span>
+<span class="k">let</span> <span class="n">create_entry_block_alloca</span> <span class="n">the_function</span> <span class="n">var_name</span> <span class="o">=</span>
+  <span class="k">let</span> <span class="n">builder</span> <span class="o">=</span> <span class="n">builder_at</span> <span class="n">context</span> <span class="o">(</span><span class="n">instr_begin</span> <span class="o">(</span><span class="n">entry_block</span> <span class="n">the_function</span><span class="o">))</span> <span class="k">in</span>
+  <span class="n">build_alloca</span> <span class="n">double_type</span> <span class="n">var_name</span> <span class="n">builder</span>
+
+<span class="k">let</span> <span class="k">rec</span> <span class="n">codegen_expr</span> <span class="o">=</span> <span class="k">function</span>
+  <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Number</span> <span class="n">n</span> <span class="o">-></span> <span class="n">const_float</span> <span class="n">double_type</span> <span class="n">n</span>
+  <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Variable</span> <span class="n">name</span> <span class="o">-></span>
+      <span class="k">let</span> <span class="n">v</span> <span class="o">=</span> <span class="k">try</span> <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">find</span> <span class="n">named_values</span> <span class="n">name</span> <span class="k">with</span>
+        <span class="o">|</span> <span class="nc">Not_found</span> <span class="o">-></span> <span class="k">raise</span> <span class="o">(</span><span class="nc">Error</span> <span class="s2">"unknown variable name"</span><span class="o">)</span>
+      <span class="k">in</span>
+      <span class="c">(* Load the value. *)</span>
+      <span class="n">build_load</span> <span class="n">v</span> <span class="n">name</span> <span class="n">builder</span>
+  <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Unary</span> <span class="o">(</span><span class="n">op</span><span class="o">,</span> <span class="n">operand</span><span class="o">)</span> <span class="o">-></span>
+      <span class="k">let</span> <span class="n">operand</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">operand</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">callee</span> <span class="o">=</span> <span class="s2">"unary"</span> <span class="o">^</span> <span class="o">(</span><span class="nn">String</span><span class="p">.</span><span class="n">make</span> <span class="mi">1</span> <span class="n">op</span><span class="o">)</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">callee</span> <span class="o">=</span>
+        <span class="k">match</span> <span class="n">lookup_function</span> <span class="n">callee</span> <span class="n">the_module</span> <span class="k">with</span>
+        <span class="o">|</span> <span class="nc">Some</span> <span class="n">callee</span> <span class="o">-></span> <span class="n">callee</span>
+        <span class="o">|</span> <span class="nc">None</span> <span class="o">-></span> <span class="k">raise</span> <span class="o">(</span><span class="nc">Error</span> <span class="s2">"unknown unary operator"</span><span class="o">)</span>
+      <span class="k">in</span>
+      <span class="n">build_call</span> <span class="n">callee</span> <span class="o">[|</span><span class="n">operand</span><span class="o">|]</span> <span class="s2">"unop"</span> <span class="n">builder</span>
+  <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Binary</span> <span class="o">(</span><span class="n">op</span><span class="o">,</span> <span class="n">lhs</span><span class="o">,</span> <span class="n">rhs</span><span class="o">)</span> <span class="o">-></span>
+      <span class="k">begin</span> <span class="k">match</span> <span class="n">op</span> <span class="k">with</span>
+      <span class="o">|</span> <span class="sc">'='</span> <span class="o">-></span>
+          <span class="c">(* Special case '=' because we don't want to emit the LHS as an</span>
+<span class="c">           * expression. *)</span>
+          <span class="k">let</span> <span class="n">name</span> <span class="o">=</span>
+            <span class="k">match</span> <span class="n">lhs</span> <span class="k">with</span>
+            <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Variable</span> <span class="n">name</span> <span class="o">-></span> <span class="n">name</span>
+            <span class="o">|</span> <span class="o">_</span> <span class="o">-></span> <span class="k">raise</span> <span class="o">(</span><span class="nc">Error</span> <span class="s2">"destination of '=' must be a variable"</span><span class="o">)</span>
+          <span class="k">in</span>
+
+          <span class="c">(* Codegen the rhs. *)</span>
+          <span class="k">let</span> <span class="n">val_</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">rhs</span> <span class="k">in</span>
+
+          <span class="c">(* Lookup the name. *)</span>
+          <span class="k">let</span> <span class="n">variable</span> <span class="o">=</span> <span class="k">try</span> <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">find</span> <span class="n">named_values</span> <span class="n">name</span> <span class="k">with</span>
+          <span class="o">|</span> <span class="nc">Not_found</span> <span class="o">-></span> <span class="k">raise</span> <span class="o">(</span><span class="nc">Error</span> <span class="s2">"unknown variable name"</span><span class="o">)</span>
+          <span class="k">in</span>
+          <span class="n">ignore</span><span class="o">(</span><span class="n">build_store</span> <span class="n">val_</span> <span class="n">variable</span> <span class="n">builder</span><span class="o">);</span>
+          <span class="n">val_</span>
+      <span class="o">|</span> <span class="o">_</span> <span class="o">-></span>
+          <span class="k">let</span> <span class="n">lhs_val</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">lhs</span> <span class="k">in</span>
+          <span class="k">let</span> <span class="n">rhs_val</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">rhs</span> <span class="k">in</span>
+          <span class="k">begin</span>
+            <span class="k">match</span> <span class="n">op</span> <span class="k">with</span>
+            <span class="o">|</span> <span class="sc">'+'</span> <span class="o">-></span> <span class="n">build_add</span> <span class="n">lhs_val</span> <span class="n">rhs_val</span> <span class="s2">"addtmp"</span> <span class="n">builder</span>
+            <span class="o">|</span> <span class="sc">'-'</span> <span class="o">-></span> <span class="n">build_sub</span> <span class="n">lhs_val</span> <span class="n">rhs_val</span> <span class="s2">"subtmp"</span> <span class="n">builder</span>
+            <span class="o">|</span> <span class="sc">'*'</span> <span class="o">-></span> <span class="n">build_mul</span> <span class="n">lhs_val</span> <span class="n">rhs_val</span> <span class="s2">"multmp"</span> <span class="n">builder</span>
+            <span class="o">|</span> <span class="sc">'<'</span> <span class="o">-></span>
+                <span class="c">(* Convert bool 0/1 to double 0.0 or 1.0 *)</span>
+                <span class="k">let</span> <span class="n">i</span> <span class="o">=</span> <span class="n">build_fcmp</span> <span class="nn">Fcmp</span><span class="p">.</span><span class="nc">Ult</span> <span class="n">lhs_val</span> <span class="n">rhs_val</span> <span class="s2">"cmptmp"</span> <span class="n">builder</span> <span class="k">in</span>
+                <span class="n">build_uitofp</span> <span class="n">i</span> <span class="n">double_type</span> <span class="s2">"booltmp"</span> <span class="n">builder</span>
+            <span class="o">|</span> <span class="o">_</span> <span class="o">-></span>
+                <span class="c">(* If it wasn't a builtin binary operator, it must be a user defined</span>
+<span class="c">                 * one. Emit a call to it. *)</span>
+                <span class="k">let</span> <span class="n">callee</span> <span class="o">=</span> <span class="s2">"binary"</span> <span class="o">^</span> <span class="o">(</span><span class="nn">String</span><span class="p">.</span><span class="n">make</span> <span class="mi">1</span> <span class="n">op</span><span class="o">)</span> <span class="k">in</span>
+                <span class="k">let</span> <span class="n">callee</span> <span class="o">=</span>
+                  <span class="k">match</span> <span class="n">lookup_function</span> <span class="n">callee</span> <span class="n">the_module</span> <span class="k">with</span>
+                  <span class="o">|</span> <span class="nc">Some</span> <span class="n">callee</span> <span class="o">-></span> <span class="n">callee</span>
+                  <span class="o">|</span> <span class="nc">None</span> <span class="o">-></span> <span class="k">raise</span> <span class="o">(</span><span class="nc">Error</span> <span class="s2">"binary operator not found!"</span><span class="o">)</span>
+                <span class="k">in</span>
+                <span class="n">build_call</span> <span class="n">callee</span> <span class="o">[|</span><span class="n">lhs_val</span><span class="o">;</span> <span class="n">rhs_val</span><span class="o">|]</span> <span class="s2">"binop"</span> <span class="n">builder</span>
+          <span class="k">end</span>
+      <span class="k">end</span>
+  <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Call</span> <span class="o">(</span><span class="n">callee</span><span class="o">,</span> <span class="n">args</span><span class="o">)</span> <span class="o">-></span>
+      <span class="c">(* Look up the name in the module table. *)</span>
+      <span class="k">let</span> <span class="n">callee</span> <span class="o">=</span>
+        <span class="k">match</span> <span class="n">lookup_function</span> <span class="n">callee</span> <span class="n">the_module</span> <span class="k">with</span>
+        <span class="o">|</span> <span class="nc">Some</span> <span class="n">callee</span> <span class="o">-></span> <span class="n">callee</span>
+        <span class="o">|</span> <span class="nc">None</span> <span class="o">-></span> <span class="k">raise</span> <span class="o">(</span><span class="nc">Error</span> <span class="s2">"unknown function referenced"</span><span class="o">)</span>
+      <span class="k">in</span>
+      <span class="k">let</span> <span class="n">params</span> <span class="o">=</span> <span class="n">params</span> <span class="n">callee</span> <span class="k">in</span>
+
+      <span class="c">(* If argument mismatch error. *)</span>
+      <span class="k">if</span> <span class="nn">Array</span><span class="p">.</span><span class="n">length</span> <span class="n">params</span> <span class="o">==</span> <span class="nn">Array</span><span class="p">.</span><span class="n">length</span> <span class="n">args</span> <span class="k">then</span> <span class="bp">()</span> <span class="k">else</span>
+        <span class="k">raise</span> <span class="o">(</span><span class="nc">Error</span> <span class="s2">"incorrect # arguments passed"</span><span class="o">);</span>
+      <span class="k">let</span> <span class="n">args</span> <span class="o">=</span> <span class="nn">Array</span><span class="p">.</span><span class="n">map</span> <span class="n">codegen_expr</span> <span class="n">args</span> <span class="k">in</span>
+      <span class="n">build_call</span> <span class="n">callee</span> <span class="n">args</span> <span class="s2">"calltmp"</span> <span class="n">builder</span>
+  <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">If</span> <span class="o">(</span><span class="n">cond</span><span class="o">,</span> <span class="n">then_</span><span class="o">,</span> <span class="n">else_</span><span class="o">)</span> <span class="o">-></span>
+      <span class="k">let</span> <span class="n">cond</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">cond</span> <span class="k">in</span>
+
+      <span class="c">(* Convert condition to a bool by comparing equal to 0.0 *)</span>
+      <span class="k">let</span> <span class="n">zero</span> <span class="o">=</span> <span class="n">const_float</span> <span class="n">double_type</span> <span class="mi">0</span><span class="o">.</span><span class="mi">0</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">cond_val</span> <span class="o">=</span> <span class="n">build_fcmp</span> <span class="nn">Fcmp</span><span class="p">.</span><span class="nc">One</span> <span class="n">cond</span> <span class="n">zero</span> <span class="s2">"ifcond"</span> <span class="n">builder</span> <span class="k">in</span>
+
+      <span class="c">(* Grab the first block so that we might later add the conditional branch</span>
+<span class="c">       * to it at the end of the function. *)</span>
+      <span class="k">let</span> <span class="n">start_bb</span> <span class="o">=</span> <span class="n">insertion_block</span> <span class="n">builder</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">the_function</span> <span class="o">=</span> <span class="n">block_parent</span> <span class="n">start_bb</span> <span class="k">in</span>
+
+      <span class="k">let</span> <span class="n">then_bb</span> <span class="o">=</span> <span class="n">append_block</span> <span class="n">context</span> <span class="s2">"then"</span> <span class="n">the_function</span> <span class="k">in</span>
+
+      <span class="c">(* Emit 'then' value. *)</span>
+      <span class="n">position_at_end</span> <span class="n">then_bb</span> <span class="n">builder</span><span class="o">;</span>
+      <span class="k">let</span> <span class="n">then_val</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">then_</span> <span class="k">in</span>
+
+      <span class="c">(* Codegen of 'then' can change the current block, update then_bb for the</span>
+<span class="c">       * phi. We create a new name because one is used for the phi node, and the</span>
+<span class="c">       * other is used for the conditional branch. *)</span>
+      <span class="k">let</span> <span class="n">new_then_bb</span> <span class="o">=</span> <span class="n">insertion_block</span> <span class="n">builder</span> <span class="k">in</span>
+
+      <span class="c">(* Emit 'else' value. *)</span>
+      <span class="k">let</span> <span class="n">else_bb</span> <span class="o">=</span> <span class="n">append_block</span> <span class="n">context</span> <span class="s2">"else"</span> <span class="n">the_function</span> <span class="k">in</span>
+      <span class="n">position_at_end</span> <span class="n">else_bb</span> <span class="n">builder</span><span class="o">;</span>
+      <span class="k">let</span> <span class="n">else_val</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">else_</span> <span class="k">in</span>
+
+      <span class="c">(* Codegen of 'else' can change the current block, update else_bb for the</span>
+<span class="c">       * phi. *)</span>
+      <span class="k">let</span> <span class="n">new_else_bb</span> <span class="o">=</span> <span class="n">insertion_block</span> <span class="n">builder</span> <span class="k">in</span>
+
+      <span class="c">(* Emit merge block. *)</span>
+      <span class="k">let</span> <span class="n">merge_bb</span> <span class="o">=</span> <span class="n">append_block</span> <span class="n">context</span> <span class="s2">"ifcont"</span> <span class="n">the_function</span> <span class="k">in</span>
+      <span class="n">position_at_end</span> <span class="n">merge_bb</span> <span class="n">builder</span><span class="o">;</span>
+      <span class="k">let</span> <span class="n">incoming</span> <span class="o">=</span> <span class="o">[(</span><span class="n">then_val</span><span class="o">,</span> <span class="n">new_then_bb</span><span class="o">);</span> <span class="o">(</span><span class="n">else_val</span><span class="o">,</span> <span class="n">new_else_bb</span><span class="o">)]</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">phi</span> <span class="o">=</span> <span class="n">build_phi</span> <span class="n">incoming</span> <span class="s2">"iftmp"</span> <span class="n">builder</span> <span class="k">in</span>
+
+      <span class="c">(* Return to the start block to add the conditional branch. *)</span>
+      <span class="n">position_at_end</span> <span class="n">start_bb</span> <span class="n">builder</span><span class="o">;</span>
+      <span class="n">ignore</span> <span class="o">(</span><span class="n">build_cond_br</span> <span class="n">cond_val</span> <span class="n">then_bb</span> <span class="n">else_bb</span> <span class="n">builder</span><span class="o">);</span>
+
+      <span class="c">(* Set a unconditional branch at the end of the 'then' block and the</span>
+<span class="c">       * 'else' block to the 'merge' block. *)</span>
+      <span class="n">position_at_end</span> <span class="n">new_then_bb</span> <span class="n">builder</span><span class="o">;</span> <span class="n">ignore</span> <span class="o">(</span><span class="n">build_br</span> <span class="n">merge_bb</span> <span class="n">builder</span><span class="o">);</span>
+      <span class="n">position_at_end</span> <span class="n">new_else_bb</span> <span class="n">builder</span><span class="o">;</span> <span class="n">ignore</span> <span class="o">(</span><span class="n">build_br</span> <span class="n">merge_bb</span> <span class="n">builder</span><span class="o">);</span>
+
+      <span class="c">(* Finally, set the builder to the end of the merge block. *)</span>
+      <span class="n">position_at_end</span> <span class="n">merge_bb</span> <span class="n">builder</span><span class="o">;</span>
+
+      <span class="n">phi</span>
+  <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">For</span> <span class="o">(</span><span class="n">var_name</span><span class="o">,</span> <span class="n">start</span><span class="o">,</span> <span class="n">end_</span><span class="o">,</span> <span class="n">step</span><span class="o">,</span> <span class="n">body</span><span class="o">)</span> <span class="o">-></span>
+      <span class="c">(* Output this as:</span>
+<span class="c">       *   var = alloca double</span>
+<span class="c">       *   ...</span>
+<span class="c">       *   start = startexpr</span>
+<span class="c">       *   store start -> var</span>
+<span class="c">       *   goto loop</span>
+<span class="c">       * loop:</span>
+<span class="c">       *   ...</span>
+<span class="c">       *   bodyexpr</span>
+<span class="c">       *   ...</span>
+<span class="c">       * loopend:</span>
+<span class="c">       *   step = stepexpr</span>
+<span class="c">       *   endcond = endexpr</span>
+<span class="c">       *</span>
+<span class="c">       *   curvar = load var</span>
+<span class="c">       *   nextvar = curvar + step</span>
+<span class="c">       *   store nextvar -> var</span>
+<span class="c">       *   br endcond, loop, endloop</span>
+<span class="c">       * outloop: *)</span>
+
+      <span class="k">let</span> <span class="n">the_function</span> <span class="o">=</span> <span class="n">block_parent</span> <span class="o">(</span><span class="n">insertion_block</span> <span class="n">builder</span><span class="o">)</span> <span class="k">in</span>
+
+      <span class="c">(* Create an alloca for the variable in the entry block. *)</span>
+      <span class="k">let</span> <span class="n">alloca</span> <span class="o">=</span> <span class="n">create_entry_block_alloca</span> <span class="n">the_function</span> <span class="n">var_name</span> <span class="k">in</span>
+
+      <span class="c">(* Emit the start code first, without 'variable' in scope. *)</span>
+      <span class="k">let</span> <span class="n">start_val</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">start</span> <span class="k">in</span>
+
+      <span class="c">(* Store the value into the alloca. *)</span>
+      <span class="n">ignore</span><span class="o">(</span><span class="n">build_store</span> <span class="n">start_val</span> <span class="n">alloca</span> <span class="n">builder</span><span class="o">);</span>
+
+      <span class="c">(* Make the new basic block for the loop header, inserting after current</span>
+<span class="c">       * block. *)</span>
+      <span class="k">let</span> <span class="n">loop_bb</span> <span class="o">=</span> <span class="n">append_block</span> <span class="n">context</span> <span class="s2">"loop"</span> <span class="n">the_function</span> <span class="k">in</span>
+
+      <span class="c">(* Insert an explicit fall through from the current block to the</span>
+<span class="c">       * loop_bb. *)</span>
+      <span class="n">ignore</span> <span class="o">(</span><span class="n">build_br</span> <span class="n">loop_bb</span> <span class="n">builder</span><span class="o">);</span>
+
+      <span class="c">(* Start insertion in loop_bb. *)</span>
+      <span class="n">position_at_end</span> <span class="n">loop_bb</span> <span class="n">builder</span><span class="o">;</span>
+
+      <span class="c">(* Within the loop, the variable is defined equal to the PHI node. If it</span>
+<span class="c">       * shadows an existing variable, we have to restore it, so save it</span>
+<span class="c">       * now. *)</span>
+      <span class="k">let</span> <span class="n">old_val</span> <span class="o">=</span>
+        <span class="k">try</span> <span class="nc">Some</span> <span class="o">(</span><span class="nn">Hashtbl</span><span class="p">.</span><span class="n">find</span> <span class="n">named_values</span> <span class="n">var_name</span><span class="o">)</span> <span class="k">with</span> <span class="nc">Not_found</span> <span class="o">-></span> <span class="nc">None</span>
+      <span class="k">in</span>
+      <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">add</span> <span class="n">named_values</span> <span class="n">var_name</span> <span class="n">alloca</span><span class="o">;</span>
+
+      <span class="c">(* Emit the body of the loop.  This, like any other expr, can change the</span>
+<span class="c">       * current BB.  Note that we ignore the value computed by the body, but</span>
+<span class="c">       * don't allow an error *)</span>
+      <span class="n">ignore</span> <span class="o">(</span><span class="n">codegen_expr</span> <span class="n">body</span><span class="o">);</span>
+
+      <span class="c">(* Emit the step value. *)</span>
+      <span class="k">let</span> <span class="n">step_val</span> <span class="o">=</span>
+        <span class="k">match</span> <span class="n">step</span> <span class="k">with</span>
+        <span class="o">|</span> <span class="nc">Some</span> <span class="n">step</span> <span class="o">-></span> <span class="n">codegen_expr</span> <span class="n">step</span>
+        <span class="c">(* If not specified, use 1.0. *)</span>
+        <span class="o">|</span> <span class="nc">None</span> <span class="o">-></span> <span class="n">const_float</span> <span class="n">double_type</span> <span class="mi">1</span><span class="o">.</span><span class="mi">0</span>
+      <span class="k">in</span>
+
+      <span class="c">(* Compute the end condition. *)</span>
+      <span class="k">let</span> <span class="n">end_cond</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">end_</span> <span class="k">in</span>
+
+      <span class="c">(* Reload, increment, and restore the alloca. This handles the case where</span>
+<span class="c">       * the body of the loop mutates the variable. *)</span>
+      <span class="k">let</span> <span class="n">cur_var</span> <span class="o">=</span> <span class="n">build_load</span> <span class="n">alloca</span> <span class="n">var_name</span> <span class="n">builder</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">next_var</span> <span class="o">=</span> <span class="n">build_add</span> <span class="n">cur_var</span> <span class="n">step_val</span> <span class="s2">"nextvar"</span> <span class="n">builder</span> <span class="k">in</span>
+      <span class="n">ignore</span><span class="o">(</span><span class="n">build_store</span> <span class="n">next_var</span> <span class="n">alloca</span> <span class="n">builder</span><span class="o">);</span>
+
+      <span class="c">(* Convert condition to a bool by comparing equal to 0.0. *)</span>
+      <span class="k">let</span> <span class="n">zero</span> <span class="o">=</span> <span class="n">const_float</span> <span class="n">double_type</span> <span class="mi">0</span><span class="o">.</span><span class="mi">0</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">end_cond</span> <span class="o">=</span> <span class="n">build_fcmp</span> <span class="nn">Fcmp</span><span class="p">.</span><span class="nc">One</span> <span class="n">end_cond</span> <span class="n">zero</span> <span class="s2">"loopcond"</span> <span class="n">builder</span> <span class="k">in</span>
+
+      <span class="c">(* Create the "after loop" block and insert it. *)</span>
+      <span class="k">let</span> <span class="n">after_bb</span> <span class="o">=</span> <span class="n">append_block</span> <span class="n">context</span> <span class="s2">"afterloop"</span> <span class="n">the_function</span> <span class="k">in</span>
+
+      <span class="c">(* Insert the conditional branch into the end of loop_end_bb. *)</span>
+      <span class="n">ignore</span> <span class="o">(</span><span class="n">build_cond_br</span> <span class="n">end_cond</span> <span class="n">loop_bb</span> <span class="n">after_bb</span> <span class="n">builder</span><span class="o">);</span>
+
+      <span class="c">(* Any new code will be inserted in after_bb. *)</span>
+      <span class="n">position_at_end</span> <span class="n">after_bb</span> <span class="n">builder</span><span class="o">;</span>
+
+      <span class="c">(* Restore the unshadowed variable. *)</span>
+      <span class="k">begin</span> <span class="k">match</span> <span class="n">old_val</span> <span class="k">with</span>
+      <span class="o">|</span> <span class="nc">Some</span> <span class="n">old_val</span> <span class="o">-></span> <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">add</span> <span class="n">named_values</span> <span class="n">var_name</span> <span class="n">old_val</span>
+      <span class="o">|</span> <span class="nc">None</span> <span class="o">-></span> <span class="bp">()</span>
+      <span class="k">end</span><span class="o">;</span>
+
+      <span class="c">(* for expr always returns 0.0. *)</span>
+      <span class="n">const_null</span> <span class="n">double_type</span>
+  <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Var</span> <span class="o">(</span><span class="n">var_names</span><span class="o">,</span> <span class="n">body</span><span class="o">)</span> <span class="o">-></span>
+      <span class="k">let</span> <span class="n">old_bindings</span> <span class="o">=</span> <span class="n">ref</span> <span class="bp">[]</span> <span class="k">in</span>
+
+      <span class="k">let</span> <span class="n">the_function</span> <span class="o">=</span> <span class="n">block_parent</span> <span class="o">(</span><span class="n">insertion_block</span> <span class="n">builder</span><span class="o">)</span> <span class="k">in</span>
+
+      <span class="c">(* Register all variables and emit their initializer. *)</span>
+      <span class="nn">Array</span><span class="p">.</span><span class="n">iter</span> <span class="o">(</span><span class="k">fun</span> <span class="o">(</span><span class="n">var_name</span><span class="o">,</span> <span class="n">init</span><span class="o">)</span> <span class="o">-></span>
+        <span class="c">(* Emit the initializer before adding the variable to scope, this</span>
+<span class="c">         * prevents the initializer from referencing the variable itself, and</span>
+<span class="c">         * permits stuff like this:</span>
+<span class="c">         *   var a = 1 in</span>
+<span class="c">         *     var a = a in ...   # refers to outer 'a'. *)</span>
+        <span class="k">let</span> <span class="n">init_val</span> <span class="o">=</span>
+          <span class="k">match</span> <span class="n">init</span> <span class="k">with</span>
+          <span class="o">|</span> <span class="nc">Some</span> <span class="n">init</span> <span class="o">-></span> <span class="n">codegen_expr</span> <span class="n">init</span>
+          <span class="c">(* If not specified, use 0.0. *)</span>
+          <span class="o">|</span> <span class="nc">None</span> <span class="o">-></span> <span class="n">const_float</span> <span class="n">double_type</span> <span class="mi">0</span><span class="o">.</span><span class="mi">0</span>
+        <span class="k">in</span>
+
+        <span class="k">let</span> <span class="n">alloca</span> <span class="o">=</span> <span class="n">create_entry_block_alloca</span> <span class="n">the_function</span> <span class="n">var_name</span> <span class="k">in</span>
+        <span class="n">ignore</span><span class="o">(</span><span class="n">build_store</span> <span class="n">init_val</span> <span class="n">alloca</span> <span class="n">builder</span><span class="o">);</span>
+
+        <span class="c">(* Remember the old variable binding so that we can restore the binding</span>
+<span class="c">         * when we unrecurse. *)</span>
+        <span class="k">begin</span>
+          <span class="k">try</span>
+            <span class="k">let</span> <span class="n">old_value</span> <span class="o">=</span> <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">find</span> <span class="n">named_values</span> <span class="n">var_name</span> <span class="k">in</span>
+            <span class="n">old_bindings</span> <span class="o">:=</span> <span class="o">(</span><span class="n">var_name</span><span class="o">,</span> <span class="n">old_value</span><span class="o">)</span> <span class="o">::</span> <span class="o">!</span><span class="n">old_bindings</span><span class="o">;</span>
+          <span class="k">with</span> <span class="nc">Not_found</span> <span class="o">-></span> <span class="bp">()</span>
+        <span class="k">end</span><span class="o">;</span>
+
+        <span class="c">(* Remember this binding. *)</span>
+        <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">add</span> <span class="n">named_values</span> <span class="n">var_name</span> <span class="n">alloca</span><span class="o">;</span>
+      <span class="o">)</span> <span class="n">var_names</span><span class="o">;</span>
+
+      <span class="c">(* Codegen the body, now that all vars are in scope. *)</span>
+      <span class="k">let</span> <span class="n">body_val</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">body</span> <span class="k">in</span>
+
+      <span class="c">(* Pop all our variables from scope. *)</span>
+      <span class="nn">List</span><span class="p">.</span><span class="n">iter</span> <span class="o">(</span><span class="k">fun</span> <span class="o">(</span><span class="n">var_name</span><span class="o">,</span> <span class="n">old_value</span><span class="o">)</span> <span class="o">-></span>
+        <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">add</span> <span class="n">named_values</span> <span class="n">var_name</span> <span class="n">old_value</span>
+      <span class="o">)</span> <span class="o">!</span><span class="n">old_bindings</span><span class="o">;</span>
+
+      <span class="c">(* Return the body computation. *)</span>
+      <span class="n">body_val</span>
+
+<span class="k">let</span> <span class="n">codegen_proto</span> <span class="o">=</span> <span class="k">function</span>
+  <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Prototype</span> <span class="o">(</span><span class="n">name</span><span class="o">,</span> <span class="n">args</span><span class="o">)</span> <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">BinOpPrototype</span> <span class="o">(</span><span class="n">name</span><span class="o">,</span> <span class="n">args</span><span class="o">,</span> <span class="o">_)</span> <span class="o">-></span>
+      <span class="c">(* Make the function type: double(double,double) etc. *)</span>
+      <span class="k">let</span> <span class="n">doubles</span> <span class="o">=</span> <span class="nn">Array</span><span class="p">.</span><span class="n">make</span> <span class="o">(</span><span class="nn">Array</span><span class="p">.</span><span class="n">length</span> <span class="n">args</span><span class="o">)</span> <span class="n">double_type</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">ft</span> <span class="o">=</span> <span class="n">function_type</span> <span class="n">double_type</span> <span class="n">doubles</span> <span class="k">in</span>
+      <span class="k">let</span> <span class="n">f</span> <span class="o">=</span>
+        <span class="k">match</span> <span class="n">lookup_function</span> <span class="n">name</span> <span class="n">the_module</span> <span class="k">with</span>
+        <span class="o">|</span> <span class="nc">None</span> <span class="o">-></span> <span class="n">declare_function</span> <span class="n">name</span> <span class="n">ft</span> <span class="n">the_module</span>
+
+        <span class="c">(* If 'f' conflicted, there was already something named 'name'. If it</span>
+<span class="c">         * has a body, don't allow redefinition or reextern. *)</span>
+        <span class="o">|</span> <span class="nc">Some</span> <span class="n">f</span> <span class="o">-></span>
+            <span class="c">(* If 'f' already has a body, reject this. *)</span>
+            <span class="k">if</span> <span class="n">block_begin</span> <span class="n">f</span> <span class="o"><></span> <span class="nc">At_end</span> <span class="n">f</span> <span class="k">then</span>
+              <span class="k">raise</span> <span class="o">(</span><span class="nc">Error</span> <span class="s2">"redefinition of function"</span><span class="o">);</span>
+
+            <span class="c">(* If 'f' took a different number of arguments, reject. *)</span>
+            <span class="k">if</span> <span class="n">element_type</span> <span class="o">(</span><span class="n">type_of</span> <span class="n">f</span><span class="o">)</span> <span class="o"><></span> <span class="n">ft</span> <span class="k">then</span>
+              <span class="k">raise</span> <span class="o">(</span><span class="nc">Error</span> <span class="s2">"redefinition of function with different # args"</span><span class="o">);</span>
+            <span class="n">f</span>
+      <span class="k">in</span>
+
+      <span class="c">(* Set names for all arguments. *)</span>
+      <span class="nn">Array</span><span class="p">.</span><span class="n">iteri</span> <span class="o">(</span><span class="k">fun</span> <span class="n">i</span> <span class="n">a</span> <span class="o">-></span>
+        <span class="k">let</span> <span class="n">n</span> <span class="o">=</span> <span class="n">args</span><span class="o">.(</span><span class="n">i</span><span class="o">)</span> <span class="k">in</span>
+        <span class="n">set_value_name</span> <span class="n">n</span> <span class="n">a</span><span class="o">;</span>
+        <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">add</span> <span class="n">named_values</span> <span class="n">n</span> <span class="n">a</span><span class="o">;</span>
+      <span class="o">)</span> <span class="o">(</span><span class="n">params</span> <span class="n">f</span><span class="o">);</span>
+      <span class="n">f</span>
+
+<span class="c">(* Create an alloca for each argument and register the argument in the symbol</span>
+<span class="c"> * table so that references to it will succeed. *)</span>
+<span class="k">let</span> <span class="n">create_argument_allocas</span> <span class="n">the_function</span> <span class="n">proto</span> <span class="o">=</span>
+  <span class="k">let</span> <span class="n">args</span> <span class="o">=</span> <span class="k">match</span> <span class="n">proto</span> <span class="k">with</span>
+    <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Prototype</span> <span class="o">(_,</span> <span class="n">args</span><span class="o">)</span> <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">BinOpPrototype</span> <span class="o">(_,</span> <span class="n">args</span><span class="o">,</span> <span class="o">_)</span> <span class="o">-></span> <span class="n">args</span>
+  <span class="k">in</span>
+  <span class="nn">Array</span><span class="p">.</span><span class="n">iteri</span> <span class="o">(</span><span class="k">fun</span> <span class="n">i</span> <span class="n">ai</span> <span class="o">-></span>
+    <span class="k">let</span> <span class="n">var_name</span> <span class="o">=</span> <span class="n">args</span><span class="o">.(</span><span class="n">i</span><span class="o">)</span> <span class="k">in</span>
+    <span class="c">(* Create an alloca for this variable. *)</span>
+    <span class="k">let</span> <span class="n">alloca</span> <span class="o">=</span> <span class="n">create_entry_block_alloca</span> <span class="n">the_function</span> <span class="n">var_name</span> <span class="k">in</span>
+
+    <span class="c">(* Store the initial value into the alloca. *)</span>
+    <span class="n">ignore</span><span class="o">(</span><span class="n">build_store</span> <span class="n">ai</span> <span class="n">alloca</span> <span class="n">builder</span><span class="o">);</span>
+
+    <span class="c">(* Add arguments to variable symbol table. *)</span>
+    <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">add</span> <span class="n">named_values</span> <span class="n">var_name</span> <span class="n">alloca</span><span class="o">;</span>
+  <span class="o">)</span> <span class="o">(</span><span class="n">params</span> <span class="n">the_function</span><span class="o">)</span>
+
+<span class="k">let</span> <span class="n">codegen_func</span> <span class="n">the_fpm</span> <span class="o">=</span> <span class="k">function</span>
+  <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">Function</span> <span class="o">(</span><span class="n">proto</span><span class="o">,</span> <span class="n">body</span><span class="o">)</span> <span class="o">-></span>
+      <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">clear</span> <span class="n">named_values</span><span class="o">;</span>
+      <span class="k">let</span> <span class="n">the_function</span> <span class="o">=</span> <span class="n">codegen_proto</span> <span class="n">proto</span> <span class="k">in</span>
+
+      <span class="c">(* If this is an operator, install it. *)</span>
+      <span class="k">begin</span> <span class="k">match</span> <span class="n">proto</span> <span class="k">with</span>
+      <span class="o">|</span> <span class="nn">Ast</span><span class="p">.</span><span class="nc">BinOpPrototype</span> <span class="o">(</span><span class="n">name</span><span class="o">,</span> <span class="n">args</span><span class="o">,</span> <span class="n">prec</span><span class="o">)</span> <span class="o">-></span>
+          <span class="k">let</span> <span class="n">op</span> <span class="o">=</span> <span class="n">name</span><span class="o">.[</span><span class="nn">String</span><span class="p">.</span><span class="n">length</span> <span class="n">name</span> <span class="o">-</span> <span class="mi">1</span><span class="o">]</span> <span class="k">in</span>
+          <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">add</span> <span class="nn">Parser</span><span class="p">.</span><span class="n">binop_precedence</span> <span class="n">op</span> <span class="n">prec</span><span class="o">;</span>
+      <span class="o">|</span> <span class="o">_</span> <span class="o">-></span> <span class="bp">()</span>
+      <span class="k">end</span><span class="o">;</span>
+
+      <span class="c">(* Create a new basic block to start insertion into. *)</span>
+      <span class="k">let</span> <span class="n">bb</span> <span class="o">=</span> <span class="n">append_block</span> <span class="n">context</span> <span class="s2">"entry"</span> <span class="n">the_function</span> <span class="k">in</span>
+      <span class="n">position_at_end</span> <span class="n">bb</span> <span class="n">builder</span><span class="o">;</span>
+
+      <span class="k">try</span>
+        <span class="c">(* Add all arguments to the symbol table and create their allocas. *)</span>
+        <span class="n">create_argument_allocas</span> <span class="n">the_function</span> <span class="n">proto</span><span class="o">;</span>
+
+        <span class="k">let</span> <span class="n">ret_val</span> <span class="o">=</span> <span class="n">codegen_expr</span> <span class="n">body</span> <span class="k">in</span>
+
+        <span class="c">(* Finish off the function. *)</span>
+        <span class="k">let</span> <span class="o">_</span> <span class="o">=</span> <span class="n">build_ret</span> <span class="n">ret_val</span> <span class="n">builder</span> <span class="k">in</span>
+
+        <span class="c">(* Validate the generated code, checking for consistency. *)</span>
+        <span class="nn">Llvm_analysis</span><span class="p">.</span><span class="n">assert_valid_function</span> <span class="n">the_function</span><span class="o">;</span>
+
+        <span class="c">(* Optimize the function. *)</span>
+        <span class="k">let</span> <span class="o">_</span> <span class="o">=</span> <span class="nn">PassManager</span><span class="p">.</span><span class="n">run_function</span> <span class="n">the_function</span> <span class="n">the_fpm</span> <span class="k">in</span>
+
+        <span class="n">the_function</span>
+      <span class="k">with</span> <span class="n">e</span> <span class="o">-></span>
+        <span class="n">delete_function</span> <span class="n">the_function</span><span class="o">;</span>
+        <span class="k">raise</span> <span class="n">e</span>
+</pre></div>
+</div>
+</dd>
+<dt>toplevel.ml:</dt>
+<dd><div class="first last highlight-ocaml"><div class="highlight"><pre><span class="c">(*===----------------------------------------------------------------------===</span>
+<span class="c"> * Top-Level parsing and JIT Driver</span>
+<span class="c"> *===----------------------------------------------------------------------===*)</span>
+
+<span class="k">open</span> <span class="nc">Llvm</span>
+<span class="k">open</span> <span class="nc">Llvm_executionengine</span>
+
+<span class="c">(* top ::= definition | external | expression | ';' *)</span>
+<span class="k">let</span> <span class="k">rec</span> <span class="n">main_loop</span> <span class="n">the_fpm</span> <span class="n">the_execution_engine</span> <span class="n">stream</span> <span class="o">=</span>
+  <span class="k">match</span> <span class="nn">Stream</span><span class="p">.</span><span class="n">peek</span> <span class="n">stream</span> <span class="k">with</span>
+  <span class="o">|</span> <span class="nc">None</span> <span class="o">-></span> <span class="bp">()</span>
+
+  <span class="c">(* ignore top-level semicolons. *)</span>
+  <span class="o">|</span> <span class="nc">Some</span> <span class="o">(</span><span class="nn">Token</span><span class="p">.</span><span class="nc">Kwd</span> <span class="sc">';'</span><span class="o">)</span> <span class="o">-></span>
+      <span class="nn">Stream</span><span class="p">.</span><span class="n">junk</span> <span class="n">stream</span><span class="o">;</span>
+      <span class="n">main_loop</span> <span class="n">the_fpm</span> <span class="n">the_execution_engine</span> <span class="n">stream</span>
+
+  <span class="o">|</span> <span class="nc">Some</span> <span class="n">token</span> <span class="o">-></span>
+      <span class="k">begin</span>
+        <span class="k">try</span> <span class="k">match</span> <span class="n">token</span> <span class="k">with</span>
+        <span class="o">|</span> <span class="nn">Token</span><span class="p">.</span><span class="nc">Def</span> <span class="o">-></span>
+            <span class="k">let</span> <span class="n">e</span> <span class="o">=</span> <span class="nn">Parser</span><span class="p">.</span><span class="n">parse_definition</span> <span class="n">stream</span> <span class="k">in</span>
+            <span class="n">print_endline</span> <span class="s2">"parsed a function definition."</span><span class="o">;</span>
+            <span class="n">dump_value</span> <span class="o">(</span><span class="nn">Codegen</span><span class="p">.</span><span class="n">codegen_func</span> <span class="n">the_fpm</span> <span class="n">e</span><span class="o">);</span>
+        <span class="o">|</span> <span class="nn">Token</span><span class="p">.</span><span class="nc">Extern</span> <span class="o">-></span>
+            <span class="k">let</span> <span class="n">e</span> <span class="o">=</span> <span class="nn">Parser</span><span class="p">.</span><span class="n">parse_extern</span> <span class="n">stream</span> <span class="k">in</span>
+            <span class="n">print_endline</span> <span class="s2">"parsed an extern."</span><span class="o">;</span>
+            <span class="n">dump_value</span> <span class="o">(</span><span class="nn">Codegen</span><span class="p">.</span><span class="n">codegen_proto</span> <span class="n">e</span><span class="o">);</span>
+        <span class="o">|</span> <span class="o">_</span> <span class="o">-></span>
+            <span class="c">(* Evaluate a top-level expression into an anonymous function. *)</span>
+            <span class="k">let</span> <span class="n">e</span> <span class="o">=</span> <span class="nn">Parser</span><span class="p">.</span><span class="n">parse_toplevel</span> <span class="n">stream</span> <span class="k">in</span>
+            <span class="n">print_endline</span> <span class="s2">"parsed a top-level expr"</span><span class="o">;</span>
+            <span class="k">let</span> <span class="n">the_function</span> <span class="o">=</span> <span class="nn">Codegen</span><span class="p">.</span><span class="n">codegen_func</span> <span class="n">the_fpm</span> <span class="n">e</span> <span class="k">in</span>
+            <span class="n">dump_value</span> <span class="n">the_function</span><span class="o">;</span>
+
+            <span class="c">(* JIT the function, returning a function pointer. *)</span>
+            <span class="k">let</span> <span class="n">result</span> <span class="o">=</span> <span class="nn">ExecutionEngine</span><span class="p">.</span><span class="n">run_function</span> <span class="n">the_function</span> <span class="o">[||]</span>
+              <span class="n">the_execution_engine</span> <span class="k">in</span>
+
+            <span class="n">print_string</span> <span class="s2">"Evaluated to "</span><span class="o">;</span>
+            <span class="n">print_float</span> <span class="o">(</span><span class="nn">GenericValue</span><span class="p">.</span><span class="n">as_float</span> <span class="nn">Codegen</span><span class="p">.</span><span class="n">double_type</span> <span class="n">result</span><span class="o">);</span>
+            <span class="n">print_newline</span> <span class="bp">()</span><span class="o">;</span>
+        <span class="k">with</span> <span class="nn">Stream</span><span class="p">.</span><span class="nc">Error</span> <span class="n">s</span> <span class="o">|</span> <span class="nn">Codegen</span><span class="p">.</span><span class="nc">Error</span> <span class="n">s</span> <span class="o">-></span>
+          <span class="c">(* Skip token for error recovery. *)</span>
+          <span class="nn">Stream</span><span class="p">.</span><span class="n">junk</span> <span class="n">stream</span><span class="o">;</span>
+          <span class="n">print_endline</span> <span class="n">s</span><span class="o">;</span>
+      <span class="k">end</span><span class="o">;</span>
+      <span class="n">print_string</span> <span class="s2">"ready> "</span><span class="o">;</span> <span class="n">flush</span> <span class="n">stdout</span><span class="o">;</span>
+      <span class="n">main_loop</span> <span class="n">the_fpm</span> <span class="n">the_execution_engine</span> <span class="n">stream</span>
+</pre></div>
+</div>
+</dd>
+<dt>toy.ml:</dt>
+<dd><div class="first last highlight-ocaml"><div class="highlight"><pre><span class="c">(*===----------------------------------------------------------------------===</span>
+<span class="c"> * Main driver code.</span>
+<span class="c"> *===----------------------------------------------------------------------===*)</span>
+
+<span class="k">open</span> <span class="nc">Llvm</span>
+<span class="k">open</span> <span class="nc">Llvm_executionengine</span>
+<span class="k">open</span> <span class="nc">Llvm_target</span>
+<span class="k">open</span> <span class="nc">Llvm_scalar_opts</span>
+
+<span class="k">let</span> <span class="n">main</span> <span class="bp">()</span> <span class="o">=</span>
+  <span class="n">ignore</span> <span class="o">(</span><span class="n">initialize_native_target</span> <span class="bp">()</span><span class="o">);</span>
+
+  <span class="c">(* Install standard binary operators.</span>
+<span class="c">   * 1 is the lowest precedence. *)</span>
+  <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">add</span> <span class="nn">Parser</span><span class="p">.</span><span class="n">binop_precedence</span> <span class="sc">'='</span> <span class="mi">2</span><span class="o">;</span>
+  <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">add</span> <span class="nn">Parser</span><span class="p">.</span><span class="n">binop_precedence</span> <span class="sc">'<'</span> <span class="mi">10</span><span class="o">;</span>
+  <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">add</span> <span class="nn">Parser</span><span class="p">.</span><span class="n">binop_precedence</span> <span class="sc">'+'</span> <span class="mi">20</span><span class="o">;</span>
+  <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">add</span> <span class="nn">Parser</span><span class="p">.</span><span class="n">binop_precedence</span> <span class="sc">'-'</span> <span class="mi">20</span><span class="o">;</span>
+  <span class="nn">Hashtbl</span><span class="p">.</span><span class="n">add</span> <span class="nn">Parser</span><span class="p">.</span><span class="n">binop_precedence</span> <span class="sc">'*'</span> <span class="mi">40</span><span class="o">;</span>    <span class="c">(* highest. *)</span>
+
+  <span class="c">(* Prime the first token. *)</span>
+  <span class="n">print_string</span> <span class="s2">"ready> "</span><span class="o">;</span> <span class="n">flush</span> <span class="n">stdout</span><span class="o">;</span>
+  <span class="k">let</span> <span class="n">stream</span> <span class="o">=</span> <span class="nn">Lexer</span><span class="p">.</span><span class="n">lex</span> <span class="o">(</span><span class="nn">Stream</span><span class="p">.</span><span class="n">of_channel</span> <span class="n">stdin</span><span class="o">)</span> <span class="k">in</span>
+
+  <span class="c">(* Create the JIT. *)</span>
+  <span class="k">let</span> <span class="n">the_execution_engine</span> <span class="o">=</span> <span class="nn">ExecutionEngine</span><span class="p">.</span><span class="n">create</span> <span class="nn">Codegen</span><span class="p">.</span><span class="n">the_module</span> <span class="k">in</span>
+  <span class="k">let</span> <span class="n">the_fpm</span> <span class="o">=</span> <span class="nn">PassManager</span><span class="p">.</span><span class="n">create_function</span> <span class="nn">Codegen</span><span class="p">.</span><span class="n">the_module</span> <span class="k">in</span>
+
+  <span class="c">(* Set up the optimizer pipeline.  Start with registering info about how the</span>
+<span class="c">   * target lays out data structures. *)</span>
+  <span class="nn">DataLayout</span><span class="p">.</span><span class="n">add</span> <span class="o">(</span><span class="nn">ExecutionEngine</span><span class="p">.</span><span class="n">target_data</span> <span class="n">the_execution_engine</span><span class="o">)</span> <span class="n">the_fpm</span><span class="o">;</span>
+
+  <span class="c">(* Promote allocas to registers. *)</span>
+  <span class="n">add_memory_to_register_promotion</span> <span class="n">the_fpm</span><span class="o">;</span>
+
+  <span class="c">(* Do simple "peephole" optimizations and bit-twiddling optzn. *)</span>
+  <span class="n">add_instruction_combination</span> <span class="n">the_fpm</span><span class="o">;</span>
+
+  <span class="c">(* reassociate expressions. *)</span>
+  <span class="n">add_reassociation</span> <span class="n">the_fpm</span><span class="o">;</span>
+
+  <span class="c">(* Eliminate Common SubExpressions. *)</span>
+  <span class="n">add_gvn</span> <span class="n">the_fpm</span><span class="o">;</span>
+
+  <span class="c">(* Simplify the control flow graph (deleting unreachable blocks, etc). *)</span>
+  <span class="n">add_cfg_simplification</span> <span class="n">the_fpm</span><span class="o">;</span>
+
+  <span class="n">ignore</span> <span class="o">(</span><span class="nn">PassManager</span><span class="p">.</span><span class="n">initialize</span> <span class="n">the_fpm</span><span class="o">);</span>
+
+  <span class="c">(* Run the main "interpreter loop" now. *)</span>
+  <span class="nn">Toplevel</span><span class="p">.</span><span class="n">main_loop</span> <span class="n">the_fpm</span> <span class="n">the_execution_engine</span> <span class="n">stream</span><span class="o">;</span>
+
+  <span class="c">(* Print out all the generated code. *)</span>
+  <span class="n">dump_module</span> <span class="nn">Codegen</span><span class="p">.</span><span class="n">the_module</span>
+<span class="o">;;</span>
+
+<span class="n">main</span> <span class="bp">()</span>
+</pre></div>
+</div>
+</dd>
+<dt>bindings.c</dt>
+<dd><div class="first last highlight-c"><div class="highlight"><pre><span class="cp">#include <stdio.h></span>
+
+<span class="cm">/* putchard - putchar that takes a double and returns 0. */</span>
+<span class="k">extern</span> <span class="kt">double</span> <span class="nf">putchard</span><span class="p">(</span><span class="kt">double</span> <span class="n">X</span><span class="p">)</span> <span class="p">{</span>
+  <span class="n">putchar</span><span class="p">((</span><span class="kt">char</span><span class="p">)</span><span class="n">X</span><span class="p">);</span>
+  <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
+<span class="p">}</span>
+
+<span class="cm">/* printd - printf that takes a double prints it as "%f\n", returning 0. */</span>
+<span class="k">extern</span> <span class="kt">double</span> <span class="nf">printd</span><span class="p">(</span><span class="kt">double</span> <span class="n">X</span><span class="p">)</span> <span class="p">{</span>
+  <span class="n">printf</span><span class="p">(</span><span class="s">"%f</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">X</span><span class="p">);</span>
+  <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
+<span class="p">}</span>
+</pre></div>
+</div>
+</dd>
+</dl>
+<p><a class="reference external" href="OCamlLangImpl8.html">Next: Conclusion and other useful LLVM tidbits</a></p>
+</div>
+</div>
+
+
+          </div>
+      </div>
+      <div class="clearer"></div>
+    </div>
+    <div class="related">
+      <h3>Navigation</h3>
+      <ul>
+        <li class="right" style="margin-right: 10px">
+          <a href="../genindex.html" title="General Index"
+             >index</a></li>
+        <li class="right" >
+          <a href="OCamlLangImpl8.html" title="8. Kaleidoscope: Conclusion and other useful LLVM tidbits"
+             >next</a> |</li>
+        <li class="right" >
+          <a href="OCamlLangImpl6.html" title="6. Kaleidoscope: Extending the Language: User-defined Operators"
+             >previous</a> |</li>
+  <li><a href="http://llvm.org/">LLVM Home</a> | </li>
+  <li><a href="../index.html">Documentation</a>»</li>
+
+          <li><a href="index.html" >LLVM Tutorial: Table of Contents</a> »</li> 
+      </ul>
+    </div>
+    <div class="footer">
+        © Copyright 2003-2016, LLVM Project.
+      Last updated on 2016-01-04.
+      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.3.
+    </div>
+  </body>
+</html>
\ No newline at end of file

Added: www-releases/trunk/3.7.1/docs/tutorial/OCamlLangImpl8.html
URL: http://llvm.org/viewvc/llvm-project/www-releases/trunk/3.7.1/docs/tutorial/OCamlLangImpl8.html?rev=257950&view=auto
==============================================================================
--- www-releases/trunk/3.7.1/docs/tutorial/OCamlLangImpl8.html (added)
+++ www-releases/trunk/3.7.1/docs/tutorial/OCamlLangImpl8.html Fri Jan 15 17:13:16 2016
@@ -0,0 +1,352 @@
+
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+
+
+<html xmlns="http://www.w3.org/1999/xhtml">
+  <head>
+    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+    
+    <title>8. Kaleidoscope: Conclusion and other useful LLVM tidbits — LLVM 3.7 documentation</title>
+    
+    <link rel="stylesheet" href="../_static/llvm-theme.css" type="text/css" />
+    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
+    
+    <script type="text/javascript">
+      var DOCUMENTATION_OPTIONS = {
+        URL_ROOT:    '../',
+        VERSION:     '3.7',
+        COLLAPSE_INDEX: false,
+        FILE_SUFFIX: '.html',
+        HAS_SOURCE:  true
+      };
+    </script>
+    <script type="text/javascript" src="../_static/jquery.js"></script>
+    <script type="text/javascript" src="../_static/underscore.js"></script>
+    <script type="text/javascript" src="../_static/doctools.js"></script>
+    <link rel="top" title="LLVM 3.7 documentation" href="../index.html" />
+    <link rel="up" title="LLVM Tutorial: Table of Contents" href="index.html" />
+    <link rel="next" title="LLVM 3.7 Release Notes" href="../ReleaseNotes.html" />
+    <link rel="prev" title="7. Kaleidoscope: Extending the Language: Mutable Variables" href="OCamlLangImpl7.html" />
+<style type="text/css">
+  table.right { float: right; margin-left: 20px; }
+  table.right td { border: 1px solid #ccc; }
+</style>
+
+  </head>
+  <body>
+<div class="logo">
+  <a href="../index.html">
+    <img src="../_static/logo.png"
+         alt="LLVM Logo" width="250" height="88"/></a>
+</div>
+
+    <div class="related">
+      <h3>Navigation</h3>
+      <ul>
+        <li class="right" style="margin-right: 10px">
+          <a href="../genindex.html" title="General Index"
+             accesskey="I">index</a></li>
+        <li class="right" >
+          <a href="../ReleaseNotes.html" title="LLVM 3.7 Release Notes"
+             accesskey="N">next</a> |</li>
+        <li class="right" >
+          <a href="OCamlLangImpl7.html" title="7. Kaleidoscope: Extending the Language: Mutable Variables"
+             accesskey="P">previous</a> |</li>
+  <li><a href="http://llvm.org/">LLVM Home</a> | </li>
+  <li><a href="../index.html">Documentation</a>»</li>
+
+          <li><a href="index.html" accesskey="U">LLVM Tutorial: Table of Contents</a> »</li> 
+      </ul>
+    </div>
+
+
+    <div class="document">
+      <div class="documentwrapper">
+          <div class="body">
+            
+  <div class="section" id="kaleidoscope-conclusion-and-other-useful-llvm-tidbits">
+<h1>8. Kaleidoscope: Conclusion and other useful LLVM tidbits<a class="headerlink" href="#kaleidoscope-conclusion-and-other-useful-llvm-tidbits" title="Permalink to this headline">¶</a></h1>
+<div class="contents local topic" id="contents">
+<ul class="simple">
+<li><a class="reference internal" href="#tutorial-conclusion" id="id2">Tutorial Conclusion</a></li>
+<li><a class="reference internal" href="#properties-of-the-llvm-ir" id="id3">Properties of the LLVM IR</a><ul>
+<li><a class="reference internal" href="#target-independence" id="id4">Target Independence</a></li>
+<li><a class="reference internal" href="#safety-guarantees" id="id5">Safety Guarantees</a></li>
+<li><a class="reference internal" href="#language-specific-optimizations" id="id6">Language-Specific Optimizations</a></li>
+</ul>
+</li>
+<li><a class="reference internal" href="#tips-and-tricks" id="id7">Tips and Tricks</a><ul>
+<li><a class="reference internal" href="#implementing-portable-offsetof-sizeof" id="id8">Implementing portable offsetof/sizeof</a></li>
+<li><a class="reference internal" href="#garbage-collected-stack-frames" id="id9">Garbage Collected Stack Frames</a></li>
+</ul>
+</li>
+</ul>
+</div>
+<div class="section" id="tutorial-conclusion">
+<h2><a class="toc-backref" href="#id2">8.1. Tutorial Conclusion</a><a class="headerlink" href="#tutorial-conclusion" title="Permalink to this headline">¶</a></h2>
+<p>Welcome to the final chapter of the “<a class="reference external" href="index.html">Implementing a language with
+LLVM</a>” tutorial. In the course of this tutorial, we have
+grown our little Kaleidoscope language from being a useless toy, to
+being a semi-interesting (but probably still useless) toy. :)</p>
+<p>It is interesting to see how far we’ve come, and how little code it has
+taken. We built the entire lexer, parser, AST, code generator, and an
+interactive run-loop (with a JIT!) by-hand in under 700 lines of
+(non-comment/non-blank) code.</p>
+<p>Our little language supports a couple of interesting features: it
+supports user defined binary and unary operators, it uses JIT
+compilation for immediate evaluation, and it supports a few control flow
+constructs with SSA construction.</p>
+<p>Part of the idea of this tutorial was to show you how easy and fun it
+can be to define, build, and play with languages. Building a compiler
+need not be a scary or mystical process! Now that you’ve seen some of
+the basics, I strongly encourage you to take the code and hack on it.
+For example, try adding:</p>
+<ul class="simple">
+<li><strong>global variables</strong> - While global variables have questional value
+in modern software engineering, they are often useful when putting
+together quick little hacks like the Kaleidoscope compiler itself.
+Fortunately, our current setup makes it very easy to add global
+variables: just have value lookup check to see if an unresolved
+variable is in the global variable symbol table before rejecting it.
+To create a new global variable, make an instance of the LLVM
+<tt class="docutils literal"><span class="pre">GlobalVariable</span></tt> class.</li>
+<li><strong>typed variables</strong> - Kaleidoscope currently only supports variables
+of type double. This gives the language a very nice elegance, because
+only supporting one type means that you never have to specify types.
+Different languages have different ways of handling this. The easiest
+way is to require the user to specify types for every variable
+definition, and record the type of the variable in the symbol table
+along with its Value*.</li>
+<li><strong>arrays, structs, vectors, etc</strong> - Once you add types, you can start
+extending the type system in all sorts of interesting ways. Simple
+arrays are very easy and are quite useful for many different
+applications. Adding them is mostly an exercise in learning how the
+LLVM <a class="reference external" href="../LangRef.html#i_getelementptr">getelementptr</a> instruction
+works: it is so nifty/unconventional, it <a class="reference external" href="../GetElementPtr.html">has its own
+FAQ</a>! If you add support for recursive types
+(e.g. linked lists), make sure to read the <a class="reference external" href="../ProgrammersManual.html#TypeResolve">section in the LLVM
+Programmer’s Manual</a> that
+describes how to construct them.</li>
+<li><strong>standard runtime</strong> - Our current language allows the user to access
+arbitrary external functions, and we use it for things like “printd”
+and “putchard”. As you extend the language to add higher-level
+constructs, often these constructs make the most sense if they are
+lowered to calls into a language-supplied runtime. For example, if
+you add hash tables to the language, it would probably make sense to
+add the routines to a runtime, instead of inlining them all the way.</li>
+<li><strong>memory management</strong> - Currently we can only access the stack in
+Kaleidoscope. It would also be useful to be able to allocate heap
+memory, either with calls to the standard libc malloc/free interface
+or with a garbage collector. If you would like to use garbage
+collection, note that LLVM fully supports <a class="reference external" href="../GarbageCollection.html">Accurate Garbage
+Collection</a> including algorithms that
+move objects and need to scan/update the stack.</li>
+<li><strong>debugger support</strong> - LLVM supports generation of <a class="reference external" href="../SourceLevelDebugging.html">DWARF Debug
+info</a> which is understood by common
+debuggers like GDB. Adding support for debug info is fairly
+straightforward. The best way to understand it is to compile some
+C/C++ code with “<tt class="docutils literal"><span class="pre">clang</span> <span class="pre">-g</span> <span class="pre">-O0</span></tt>” and taking a look at what it
+produces.</li>
+<li><strong>exception handling support</strong> - LLVM supports generation of <a class="reference external" href="../ExceptionHandling.html">zero
+cost exceptions</a> which interoperate with
+code compiled in other languages. You could also generate code by
+implicitly making every function return an error value and checking
+it. You could also make explicit use of setjmp/longjmp. There are
+many different ways to go here.</li>
+<li><strong>object orientation, generics, database access, complex numbers,
+geometric programming, ...</strong> - Really, there is no end of crazy
+features that you can add to the language.</li>
+<li><strong>unusual domains</strong> - We’ve been talking about applying LLVM to a
+domain that many people are interested in: building a compiler for a
+specific language. However, there are many other domains that can use
+compiler technology that are not typically considered. For example,
+LLVM has been used to implement OpenGL graphics acceleration,
+translate C++ code to ActionScript, and many other cute and clever
+things. Maybe you will be the first to JIT compile a regular
+expression interpreter into native code with LLVM?</li>
+</ul>
+<p>Have fun - try doing something crazy and unusual. Building a language
+like everyone else always has, is much less fun than trying something a
+little crazy or off the wall and seeing how it turns out. If you get
+stuck or want to talk about it, feel free to email the <a class="reference external" href="http://lists.llvm.org/mailman/listinfo/llvm-dev">llvm-dev mailing
+list</a>: it has lots
+of people who are interested in languages and are often willing to help
+out.</p>
+<p>Before we end this tutorial, I want to talk about some “tips and tricks”
+for generating LLVM IR. These are some of the more subtle things that
+may not be obvious, but are very useful if you want to take advantage of
+LLVM’s capabilities.</p>
+</div>
+<div class="section" id="properties-of-the-llvm-ir">
+<h2><a class="toc-backref" href="#id3">8.2. Properties of the LLVM IR</a><a class="headerlink" href="#properties-of-the-llvm-ir" title="Permalink to this headline">¶</a></h2>
+<p>We have a couple common questions about code in the LLVM IR form - lets
+just get these out of the way right now, shall we?</p>
+<div class="section" id="target-independence">
+<h3><a class="toc-backref" href="#id4">8.2.1. Target Independence</a><a class="headerlink" href="#target-independence" title="Permalink to this headline">¶</a></h3>
+<p>Kaleidoscope is an example of a “portable language”: any program written
+in Kaleidoscope will work the same way on any target that it runs on.
+Many other languages have this property, e.g. lisp, java, haskell,
+javascript, python, etc (note that while these languages are portable,
+not all their libraries are).</p>
+<p>One nice aspect of LLVM is that it is often capable of preserving target
+independence in the IR: you can take the LLVM IR for a
+Kaleidoscope-compiled program and run it on any target that LLVM
+supports, even emitting C code and compiling that on targets that LLVM
+doesn’t support natively. You can trivially tell that the Kaleidoscope
+compiler generates target-independent code because it never queries for
+any target-specific information when generating code.</p>
+<p>The fact that LLVM provides a compact, target-independent,
+representation for code gets a lot of people excited. Unfortunately,
+these people are usually thinking about C or a language from the C
+family when they are asking questions about language portability. I say
+“unfortunately”, because there is really no way to make (fully general)
+C code portable, other than shipping the source code around (and of
+course, C source code is not actually portable in general either - ever
+port a really old application from 32- to 64-bits?).</p>
+<p>The problem with C (again, in its full generality) is that it is heavily
+laden with target specific assumptions. As one simple example, the
+preprocessor often destructively removes target-independence from the
+code when it processes the input text:</p>
+<div class="highlight-c"><div class="highlight"><pre><span class="cp">#ifdef __i386__</span>
+  <span class="kt">int</span> <span class="n">X</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
+<span class="cp">#else</span>
+  <span class="kt">int</span> <span class="n">X</span> <span class="o">=</span> <span class="mi">42</span><span class="p">;</span>
+<span class="cp">#endif</span>
+</pre></div>
+</div>
+<p>While it is possible to engineer more and more complex solutions to
+problems like this, it cannot be solved in full generality in a way that
+is better than shipping the actual source code.</p>
+<p>That said, there are interesting subsets of C that can be made portable.
+If you are willing to fix primitive types to a fixed size (say int =
+32-bits, and long = 64-bits), don’t care about ABI compatibility with
+existing binaries, and are willing to give up some other minor features,
+you can have portable code. This can make sense for specialized domains
+such as an in-kernel language.</p>
+</div>
+<div class="section" id="safety-guarantees">
+<h3><a class="toc-backref" href="#id5">8.2.2. Safety Guarantees</a><a class="headerlink" href="#safety-guarantees" title="Permalink to this headline">¶</a></h3>
+<p>Many of the languages above are also “safe” languages: it is impossible
+for a program written in Java to corrupt its address space and crash the
+process (assuming the JVM has no bugs). Safety is an interesting
+property that requires a combination of language design, runtime
+support, and often operating system support.</p>
+<p>It is certainly possible to implement a safe language in LLVM, but LLVM
+IR does not itself guarantee safety. The LLVM IR allows unsafe pointer
+casts, use after free bugs, buffer over-runs, and a variety of other
+problems. Safety needs to be implemented as a layer on top of LLVM and,
+conveniently, several groups have investigated this. Ask on the <a class="reference external" href="http://lists.llvm.org/mailman/listinfo/llvm-dev">llvm-dev
+mailing list</a> if
+you are interested in more details.</p>
+</div>
+<div class="section" id="language-specific-optimizations">
+<h3><a class="toc-backref" href="#id6">8.2.3. Language-Specific Optimizations</a><a class="headerlink" href="#language-specific-optimizations" title="Permalink to this headline">¶</a></h3>
+<p>One thing about LLVM that turns off many people is that it does not
+solve all the world’s problems in one system (sorry ‘world hunger’,
+someone else will have to solve you some other day). One specific
+complaint is that people perceive LLVM as being incapable of performing
+high-level language-specific optimization: LLVM “loses too much
+information”.</p>
+<p>Unfortunately, this is really not the place to give you a full and
+unified version of “Chris Lattner’s theory of compiler design”. Instead,
+I’ll make a few observations:</p>
+<p>First, you’re right that LLVM does lose information. For example, as of
+this writing, there is no way to distinguish in the LLVM IR whether an
+SSA-value came from a C “int” or a C “long” on an ILP32 machine (other
+than debug info). Both get compiled down to an ‘i32’ value and the
+information about what it came from is lost. The more general issue
+here, is that the LLVM type system uses “structural equivalence” instead
+of “name equivalence”. Another place this surprises people is if you
+have two types in a high-level language that have the same structure
+(e.g. two different structs that have a single int field): these types
+will compile down into a single LLVM type and it will be impossible to
+tell what it came from.</p>
+<p>Second, while LLVM does lose information, LLVM is not a fixed target: we
+continue to enhance and improve it in many different ways. In addition
+to adding new features (LLVM did not always support exceptions or debug
+info), we also extend the IR to capture important information for
+optimization (e.g. whether an argument is sign or zero extended,
+information about pointers aliasing, etc). Many of the enhancements are
+user-driven: people want LLVM to include some specific feature, so they
+go ahead and extend it.</p>
+<p>Third, it is <em>possible and easy</em> to add language-specific optimizations,
+and you have a number of choices in how to do it. As one trivial
+example, it is easy to add language-specific optimization passes that
+“know” things about code compiled for a language. In the case of the C
+family, there is an optimization pass that “knows” about the standard C
+library functions. If you call “exit(0)” in main(), it knows that it is
+safe to optimize that into “return 0;” because C specifies what the
+‘exit’ function does.</p>
+<p>In addition to simple library knowledge, it is possible to embed a
+variety of other language-specific information into the LLVM IR. If you
+have a specific need and run into a wall, please bring the topic up on
+the llvm-dev list. At the very worst, you can always treat LLVM as if it
+were a “dumb code generator” and implement the high-level optimizations
+you desire in your front-end, on the language-specific AST.</p>
+</div>
+</div>
+<div class="section" id="tips-and-tricks">
+<h2><a class="toc-backref" href="#id7">8.3. Tips and Tricks</a><a class="headerlink" href="#tips-and-tricks" title="Permalink to this headline">¶</a></h2>
+<p>There is a variety of useful tips and tricks that you come to know after
+working on/with LLVM that aren’t obvious at first glance. Instead of
+letting everyone rediscover them, this section talks about some of these
+issues.</p>
+<div class="section" id="implementing-portable-offsetof-sizeof">
+<h3><a class="toc-backref" href="#id8">8.3.1. Implementing portable offsetof/sizeof</a><a class="headerlink" href="#implementing-portable-offsetof-sizeof" title="Permalink to this headline">¶</a></h3>
+<p>One interesting thing that comes up, if you are trying to keep the code
+generated by your compiler “target independent”, is that you often need
+to know the size of some LLVM type or the offset of some field in an
+llvm structure. For example, you might need to pass the size of a type
+into a function that allocates memory.</p>
+<p>Unfortunately, this can vary widely across targets: for example the
+width of a pointer is trivially target-specific. However, there is a
+<a class="reference external" href="http://nondot.org/sabre/LLVMNotes/SizeOf-OffsetOf-VariableSizedStructs.txt">clever way to use the getelementptr
+instruction</a>
+that allows you to compute this in a portable way.</p>
+</div>
+<div class="section" id="garbage-collected-stack-frames">
+<h3><a class="toc-backref" href="#id9">8.3.2. Garbage Collected Stack Frames</a><a class="headerlink" href="#garbage-collected-stack-frames" title="Permalink to this headline">¶</a></h3>
+<p>Some languages want to explicitly manage their stack frames, often so
+that they are garbage collected or to allow easy implementation of
+closures. There are often better ways to implement these features than
+explicit stack frames, but <a class="reference external" href="http://nondot.org/sabre/LLVMNotes/ExplicitlyManagedStackFrames.txt">LLVM does support
+them,</a>
+if you want. It requires your front-end to convert the code into
+<a class="reference external" href="http://en.wikipedia.org/wiki/Continuation-passing_style">Continuation Passing
+Style</a> and
+the use of tail calls (which LLVM also supports).</p>
+</div>
+</div>
+</div>
+
+
+          </div>
+      </div>
+      <div class="clearer"></div>
+    </div>
+    <div class="related">
+      <h3>Navigation</h3>
+      <ul>
+        <li class="right" style="margin-right: 10px">
+          <a href="../genindex.html" title="General Index"
+             >index</a></li>
+        <li class="right" >
+          <a href="../ReleaseNotes.html" title="LLVM 3.7 Release Notes"
+             >next</a> |</li>
+        <li class="right" >
+          <a href="OCamlLangImpl7.html" title="7. Kaleidoscope: Extending the Language: Mutable Variables"
+             >previous</a> |</li>
+  <li><a href="http://llvm.org/">LLVM Home</a> | </li>
+  <li><a href="../index.html">Documentation</a>»</li>
+
+          <li><a href="index.html" >LLVM Tutorial: Table of Contents</a> »</li> 
+      </ul>
+    </div>
+    <div class="footer">
+        © Copyright 2003-2016, LLVM Project.
+      Last updated on 2016-01-04.
+      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.3.
+    </div>
+  </body>
+</html>
\ No newline at end of file

Added: www-releases/trunk/3.7.1/docs/tutorial/index.html
URL: http://llvm.org/viewvc/llvm-project/www-releases/trunk/3.7.1/docs/tutorial/index.html?rev=257950&view=auto
==============================================================================
--- www-releases/trunk/3.7.1/docs/tutorial/index.html (added)
+++ www-releases/trunk/3.7.1/docs/tutorial/index.html Fri Jan 15 17:13:16 2016
@@ -0,0 +1,181 @@
+
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+
+
+<html xmlns="http://www.w3.org/1999/xhtml">
+  <head>
+    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+    
+    <title>LLVM Tutorial: Table of Contents — LLVM 3.7 documentation</title>
+    
+    <link rel="stylesheet" href="../_static/llvm-theme.css" type="text/css" />
+    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
+    
+    <script type="text/javascript">
+      var DOCUMENTATION_OPTIONS = {
+        URL_ROOT:    '../',
+        VERSION:     '3.7',
+        COLLAPSE_INDEX: false,
+        FILE_SUFFIX: '.html',
+        HAS_SOURCE:  true
+      };
+    </script>
+    <script type="text/javascript" src="../_static/jquery.js"></script>
+    <script type="text/javascript" src="../_static/underscore.js"></script>
+    <script type="text/javascript" src="../_static/doctools.js"></script>
+    <link rel="top" title="LLVM 3.7 documentation" href="../index.html" />
+    <link rel="next" title="1. Kaleidoscope: Tutorial Introduction and the Lexer" href="LangImpl1.html" />
+    <link rel="prev" title="LLVM test-suite Makefile Guide" href="../TestSuiteMakefileGuide.html" />
+<style type="text/css">
+  table.right { float: right; margin-left: 20px; }
+  table.right td { border: 1px solid #ccc; }
+</style>
+
+  </head>
+  <body>
+<div class="logo">
+  <a href="../index.html">
+    <img src="../_static/logo.png"
+         alt="LLVM Logo" width="250" height="88"/></a>
+</div>
+
+    <div class="related">
+      <h3>Navigation</h3>
+      <ul>
+        <li class="right" style="margin-right: 10px">
+          <a href="../genindex.html" title="General Index"
+             accesskey="I">index</a></li>
+        <li class="right" >
+          <a href="LangImpl1.html" title="1. Kaleidoscope: Tutorial Introduction and the Lexer"
+             accesskey="N">next</a> |</li>
+        <li class="right" >
+          <a href="../TestSuiteMakefileGuide.html" title="LLVM test-suite Makefile Guide"
+             accesskey="P">previous</a> |</li>
+  <li><a href="http://llvm.org/">LLVM Home</a> | </li>
+  <li><a href="../index.html">Documentation</a>»</li>
+ 
+      </ul>
+    </div>
+
+
+    <div class="document">
+      <div class="documentwrapper">
+          <div class="body">
+            
+  <div class="section" id="llvm-tutorial-table-of-contents">
+<h1>LLVM Tutorial: Table of Contents<a class="headerlink" href="#llvm-tutorial-table-of-contents" title="Permalink to this headline">¶</a></h1>
+<div class="section" id="kaleidoscope-implementing-a-language-with-llvm">
+<h2>Kaleidoscope: Implementing a Language with LLVM<a class="headerlink" href="#kaleidoscope-implementing-a-language-with-llvm" title="Permalink to this headline">¶</a></h2>
+<div class="toctree-wrapper compound">
+<ul>
+<li class="toctree-l1"><a class="reference internal" href="LangImpl1.html">1. Kaleidoscope: Tutorial Introduction and the Lexer</a><ul class="simple">
+</ul>
+</li>
+<li class="toctree-l1"><a class="reference internal" href="LangImpl2.html">2. Kaleidoscope: Implementing a Parser and AST</a><ul class="simple">
+</ul>
+</li>
+<li class="toctree-l1"><a class="reference internal" href="LangImpl3.html">3. Kaleidoscope: Code generation to LLVM IR</a><ul class="simple">
+</ul>
+</li>
+<li class="toctree-l1"><a class="reference internal" href="LangImpl4.html">4. Kaleidoscope: Adding JIT and Optimizer Support</a><ul class="simple">
+</ul>
+</li>
+<li class="toctree-l1"><a class="reference internal" href="LangImpl5.html">5. Kaleidoscope: Extending the Language: Control Flow</a><ul class="simple">
+</ul>
+</li>
+<li class="toctree-l1"><a class="reference internal" href="LangImpl6.html">6. Kaleidoscope: Extending the Language: User-defined Operators</a><ul class="simple">
+</ul>
+</li>
+<li class="toctree-l1"><a class="reference internal" href="LangImpl7.html">7. Kaleidoscope: Extending the Language: Mutable Variables</a><ul class="simple">
+</ul>
+</li>
+<li class="toctree-l1"><a class="reference internal" href="LangImpl8.html">8. Kaleidoscope: Adding Debug Information</a><ul class="simple">
+</ul>
+</li>
+<li class="toctree-l1"><a class="reference internal" href="LangImpl9.html">9. Kaleidoscope: Conclusion and other useful LLVM tidbits</a><ul class="simple">
+</ul>
+</li>
+</ul>
+</div>
+</div>
+<div class="section" id="kaleidoscope-implementing-a-language-with-llvm-in-objective-caml">
+<h2>Kaleidoscope: Implementing a Language with LLVM in Objective Caml<a class="headerlink" href="#kaleidoscope-implementing-a-language-with-llvm-in-objective-caml" title="Permalink to this headline">¶</a></h2>
+<div class="toctree-wrapper compound">
+<ul>
+<li class="toctree-l1"><a class="reference internal" href="OCamlLangImpl1.html">1. Kaleidoscope: Tutorial Introduction and the Lexer</a><ul class="simple">
+</ul>
+</li>
+<li class="toctree-l1"><a class="reference internal" href="OCamlLangImpl2.html">2. Kaleidoscope: Implementing a Parser and AST</a><ul class="simple">
+</ul>
+</li>
+<li class="toctree-l1"><a class="reference internal" href="OCamlLangImpl3.html">3. Kaleidoscope: Code generation to LLVM IR</a><ul class="simple">
+</ul>
+</li>
+<li class="toctree-l1"><a class="reference internal" href="OCamlLangImpl4.html">4. Kaleidoscope: Adding JIT and Optimizer Support</a><ul class="simple">
+</ul>
+</li>
+<li class="toctree-l1"><a class="reference internal" href="OCamlLangImpl5.html">5. Kaleidoscope: Extending the Language: Control Flow</a><ul class="simple">
+</ul>
+</li>
+<li class="toctree-l1"><a class="reference internal" href="OCamlLangImpl6.html">6. Kaleidoscope: Extending the Language: User-defined Operators</a><ul class="simple">
+</ul>
+</li>
+<li class="toctree-l1"><a class="reference internal" href="OCamlLangImpl7.html">7. Kaleidoscope: Extending the Language: Mutable Variables</a><ul class="simple">
+</ul>
+</li>
+<li class="toctree-l1"><a class="reference internal" href="OCamlLangImpl8.html">8. Kaleidoscope: Conclusion and other useful LLVM tidbits</a><ul class="simple">
+</ul>
+</li>
+</ul>
+</div>
+</div>
+<div class="section" id="external-tutorials">
+<h2>External Tutorials<a class="headerlink" href="#external-tutorials" title="Permalink to this headline">¶</a></h2>
+<dl class="docutils">
+<dt><a class="reference external" href="http://jonathan2251.github.com/lbd/">Tutorial: Creating an LLVM Backend for the Cpu0 Architecture</a></dt>
+<dd>A step-by-step tutorial for developing an LLVM backend. Under
+active development at <a class="reference external" href="https://github.com/Jonathan2251/lbd">https://github.com/Jonathan2251/lbd</a> (please
+contribute!).</dd>
+<dt><a class="reference external" href="http://www.embecosm.com/appnotes/ean10/ean10-howto-llvmas-1.0.html">Howto: Implementing LLVM Integrated Assembler</a></dt>
+<dd>A simple guide for how to implement an LLVM integrated assembler for an
+architecture.</dd>
+</dl>
+</div>
+<div class="section" id="advanced-topics">
+<h2>Advanced Topics<a class="headerlink" href="#advanced-topics" title="Permalink to this headline">¶</a></h2>
+<ol class="arabic simple">
+<li><a class="reference external" href="http://llvm.org/pubs/2004-09-22-LCPCLLVMTutorial.html">Writing an Optimization for LLVM</a></li>
+</ol>
+</div>
+</div>
+
+
+          </div>
+      </div>
+      <div class="clearer"></div>
+    </div>
+    <div class="related">
+      <h3>Navigation</h3>
+      <ul>
+        <li class="right" style="margin-right: 10px">
+          <a href="../genindex.html" title="General Index"
+             >index</a></li>
+        <li class="right" >
+          <a href="LangImpl1.html" title="1. Kaleidoscope: Tutorial Introduction and the Lexer"
+             >next</a> |</li>
+        <li class="right" >
+          <a href="../TestSuiteMakefileGuide.html" title="LLVM test-suite Makefile Guide"
+             >previous</a> |</li>
+  <li><a href="http://llvm.org/">LLVM Home</a> | </li>
+  <li><a href="../index.html">Documentation</a>»</li>
+ 
+      </ul>
+    </div>
+    <div class="footer">
+        © Copyright 2003-2016, LLVM Project.
+      Last updated on 2016-01-04.
+      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.3.
+    </div>
+  </body>
+</html>
\ No newline at end of file

Added: www-releases/trunk/3.7.1/docs/yaml2obj.html
URL: http://llvm.org/viewvc/llvm-project/www-releases/trunk/3.7.1/docs/yaml2obj.html?rev=257950&view=auto
==============================================================================
--- www-releases/trunk/3.7.1/docs/yaml2obj.html (added)
+++ www-releases/trunk/3.7.1/docs/yaml2obj.html Fri Jan 15 17:13:16 2016
@@ -0,0 +1,306 @@
+
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+
+
+<html xmlns="http://www.w3.org/1999/xhtml">
+  <head>
+    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+    
+    <title>yaml2obj — LLVM 3.7 documentation</title>
+    
+    <link rel="stylesheet" href="_static/llvm-theme.css" type="text/css" />
+    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
+    
+    <script type="text/javascript">
+      var DOCUMENTATION_OPTIONS = {
+        URL_ROOT:    './',
+        VERSION:     '3.7',
+        COLLAPSE_INDEX: false,
+        FILE_SUFFIX: '.html',
+        HAS_SOURCE:  true
+      };
+    </script>
+    <script type="text/javascript" src="_static/jquery.js"></script>
+    <script type="text/javascript" src="_static/underscore.js"></script>
+    <script type="text/javascript" src="_static/doctools.js"></script>
+    <link rel="top" title="LLVM 3.7 documentation" href="index.html" />
+    <link rel="next" title="How to submit an LLVM bug report" href="HowToSubmitABug.html" />
+    <link rel="prev" title="How To Add Your Build Configuration To LLVM Buildbot Infrastructure" href="HowToAddABuilder.html" />
+<style type="text/css">
+  table.right { float: right; margin-left: 20px; }
+  table.right td { border: 1px solid #ccc; }
+</style>
+
+  </head>
+  <body>
+<div class="logo">
+  <a href="index.html">
+    <img src="_static/logo.png"
+         alt="LLVM Logo" width="250" height="88"/></a>
+</div>
+
+    <div class="related">
+      <h3>Navigation</h3>
+      <ul>
+        <li class="right" style="margin-right: 10px">
+          <a href="genindex.html" title="General Index"
+             accesskey="I">index</a></li>
+        <li class="right" >
+          <a href="HowToSubmitABug.html" title="How to submit an LLVM bug report"
+             accesskey="N">next</a> |</li>
+        <li class="right" >
+          <a href="HowToAddABuilder.html" title="How To Add Your Build Configuration To LLVM Buildbot Infrastructure"
+             accesskey="P">previous</a> |</li>
+  <li><a href="http://llvm.org/">LLVM Home</a> | </li>
+  <li><a href="index.html">Documentation</a>»</li>
+ 
+      </ul>
+    </div>
+
+
+    <div class="document">
+      <div class="documentwrapper">
+          <div class="body">
+            
+  <div class="section" id="yaml2obj">
+<h1>yaml2obj<a class="headerlink" href="#yaml2obj" title="Permalink to this headline">¶</a></h1>
+<p>yaml2obj takes a YAML description of an object file and converts it to a binary
+file.</p>
+<blockquote>
+<div>$ yaml2obj input-file</div></blockquote>
+<p>Outputs the binary to stdout.</p>
+<div class="section" id="coff-syntax">
+<h2>COFF Syntax<a class="headerlink" href="#coff-syntax" title="Permalink to this headline">¶</a></h2>
+<p>Here’s a sample COFF file.</p>
+<div class="highlight-yaml"><div class="highlight"><pre><span class="l-Scalar-Plain">header</span><span class="p-Indicator">:</span>
+  <span class="l-Scalar-Plain">Machine</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">IMAGE_FILE_MACHINE_I386</span> <span class="c1"># (0x14C)</span>
+
+<span class="l-Scalar-Plain">sections</span><span class="p-Indicator">:</span>
+  <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">Name</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">.text</span>
+    <span class="l-Scalar-Plain">Characteristics</span><span class="p-Indicator">:</span> <span class="p-Indicator">[</span> <span class="nv">IMAGE_SCN_CNT_CODE</span>
+                     <span class="p-Indicator">,</span> <span class="nv">IMAGE_SCN_ALIGN_16BYTES</span>
+                     <span class="p-Indicator">,</span> <span class="nv">IMAGE_SCN_MEM_EXECUTE</span>
+                     <span class="p-Indicator">,</span> <span class="nv">IMAGE_SCN_MEM_READ</span>
+                     <span class="p-Indicator">]</span> <span class="c1"># 0x60500020</span>
+    <span class="l-Scalar-Plain">SectionData</span><span class="p-Indicator">:</span>
+      <span class="s">"</span><span class="se">\x83\xEC\x0C\xC7\x44\x24\x08\x00\x00\x00\x00\xC7\x04\x24\x00\x00\x00\x00\xE8\x00\x00\x00\x00\xE8\x00\x00\x00\x00\x8B\x44\x24\x08\x83\xC4\x0C\xC3</span><span class="s">"</span> <span class="c1"># |....D$.......$...............D$.....|</span>
+
+<span class="l-Scalar-Plain">symbols</span><span class="p-Indicator">:</span>
+  <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">Name</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">.text</span>
+    <span class="l-Scalar-Plain">Value</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">0</span>
+    <span class="l-Scalar-Plain">SectionNumber</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">1</span>
+    <span class="l-Scalar-Plain">SimpleType</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">IMAGE_SYM_TYPE_NULL</span> <span class="c1"># (0)</span>
+    <span class="l-Scalar-Plain">ComplexType</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">IMAGE_SYM_DTYPE_NULL</span> <span class="c1"># (0)</span>
+    <span class="l-Scalar-Plain">StorageClass</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">IMAGE_SYM_CLASS_STATIC</span> <span class="c1"># (3)</span>
+    <span class="l-Scalar-Plain">NumberOfAuxSymbols</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">1</span>
+    <span class="l-Scalar-Plain">AuxiliaryData</span><span class="p-Indicator">:</span>
+      <span class="s">"</span><span class="se">\x24\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00</span><span class="s">"</span> <span class="c1"># |$.................|</span>
+
+  <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">Name</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">_main</span>
+    <span class="l-Scalar-Plain">Value</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">0</span>
+    <span class="l-Scalar-Plain">SectionNumber</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">1</span>
+    <span class="l-Scalar-Plain">SimpleType</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">IMAGE_SYM_TYPE_NULL</span> <span class="c1"># (0)</span>
+    <span class="l-Scalar-Plain">ComplexType</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">IMAGE_SYM_DTYPE_NULL</span> <span class="c1"># (0)</span>
+    <span class="l-Scalar-Plain">StorageClass</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">IMAGE_SYM_CLASS_EXTERNAL</span> <span class="c1"># (2)</span>
+</pre></div>
+</div>
+<p>Here’s a simplified <a class="reference external" href="http://www.kuwata-lab.com/kwalify/ruby/users-guide.html">Kwalify</a> schema with an extension to allow alternate types.</p>
+<div class="highlight-yaml"><div class="highlight"><pre><span class="l-Scalar-Plain">type</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">map</span>
+  <span class="l-Scalar-Plain">mapping</span><span class="p-Indicator">:</span>
+    <span class="l-Scalar-Plain">header</span><span class="p-Indicator">:</span>
+      <span class="l-Scalar-Plain">type</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">map</span>
+      <span class="l-Scalar-Plain">mapping</span><span class="p-Indicator">:</span>
+        <span class="l-Scalar-Plain">Machine</span><span class="p-Indicator">:</span> <span class="p-Indicator">[</span> <span class="p-Indicator">{</span><span class="nv">type</span><span class="p-Indicator">:</span> <span class="nv">str</span><span class="p-Indicator">,</span> <span class="nv">enum</span><span class="p-Indicator">:</span>
+                               <span class="p-Indicator">[</span> <span class="nv">IMAGE_FILE_MACHINE_UNKNOWN</span>
+                               <span class="p-Indicator">,</span> <span class="nv">IMAGE_FILE_MACHINE_AM33</span>
+                               <span class="p-Indicator">,</span> <span class="nv">IMAGE_FILE_MACHINE_AMD64</span>
+                               <span class="p-Indicator">,</span> <span class="nv">IMAGE_FILE_MACHINE_ARM</span>
+                               <span class="p-Indicator">,</span> <span class="nv">IMAGE_FILE_MACHINE_ARMNT</span>
+                               <span class="p-Indicator">,</span> <span class="nv">IMAGE_FILE_MACHINE_EBC</span>
+                               <span class="p-Indicator">,</span> <span class="nv">IMAGE_FILE_MACHINE_I386</span>
+                               <span class="p-Indicator">,</span> <span class="nv">IMAGE_FILE_MACHINE_IA64</span>
+                               <span class="p-Indicator">,</span> <span class="nv">IMAGE_FILE_MACHINE_M32R</span>
+                               <span class="p-Indicator">,</span> <span class="nv">IMAGE_FILE_MACHINE_MIPS16</span>
+                               <span class="p-Indicator">,</span> <span class="nv">IMAGE_FILE_MACHINE_MIPSFPU</span>
+                               <span class="p-Indicator">,</span> <span class="nv">IMAGE_FILE_MACHINE_MIPSFPU16</span>
+                               <span class="p-Indicator">,</span> <span class="nv">IMAGE_FILE_MACHINE_POWERPC</span>
+                               <span class="p-Indicator">,</span> <span class="nv">IMAGE_FILE_MACHINE_POWERPCFP</span>
+                               <span class="p-Indicator">,</span> <span class="nv">IMAGE_FILE_MACHINE_R4000</span>
+                               <span class="p-Indicator">,</span> <span class="nv">IMAGE_FILE_MACHINE_SH3</span>
+                               <span class="p-Indicator">,</span> <span class="nv">IMAGE_FILE_MACHINE_SH3DSP</span>
+                               <span class="p-Indicator">,</span> <span class="nv">IMAGE_FILE_MACHINE_SH4</span>
+                               <span class="p-Indicator">,</span> <span class="nv">IMAGE_FILE_MACHINE_SH5</span>
+                               <span class="p-Indicator">,</span> <span class="nv">IMAGE_FILE_MACHINE_THUMB</span>
+                               <span class="p-Indicator">,</span> <span class="nv">IMAGE_FILE_MACHINE_WCEMIPSV2</span>
+                               <span class="p-Indicator">]}</span>
+                 <span class="p-Indicator">,</span> <span class="p-Indicator">{</span><span class="nv">type</span><span class="p-Indicator">:</span> <span class="nv">int</span><span class="p-Indicator">}</span>
+                 <span class="p-Indicator">]</span>
+        <span class="l-Scalar-Plain">Characteristics</span><span class="p-Indicator">:</span>
+          <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">type</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">seq</span>
+            <span class="l-Scalar-Plain">sequence</span><span class="p-Indicator">:</span>
+              <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">type</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">str</span>
+                <span class="l-Scalar-Plain">enum</span><span class="p-Indicator">:</span> <span class="p-Indicator">[</span> <span class="nv">IMAGE_FILE_RELOCS_STRIPPED</span>
+                      <span class="p-Indicator">,</span> <span class="nv">IMAGE_FILE_EXECUTABLE_IMAGE</span>
+                      <span class="p-Indicator">,</span> <span class="nv">IMAGE_FILE_LINE_NUMS_STRIPPED</span>
+                      <span class="p-Indicator">,</span> <span class="nv">IMAGE_FILE_LOCAL_SYMS_STRIPPED</span>
+                      <span class="p-Indicator">,</span> <span class="nv">IMAGE_FILE_AGGRESSIVE_WS_TRIM</span>
+                      <span class="p-Indicator">,</span> <span class="nv">IMAGE_FILE_LARGE_ADDRESS_AWARE</span>
+                      <span class="p-Indicator">,</span> <span class="nv">IMAGE_FILE_BYTES_REVERSED_LO</span>
+                      <span class="p-Indicator">,</span> <span class="nv">IMAGE_FILE_32BIT_MACHINE</span>
+                      <span class="p-Indicator">,</span> <span class="nv">IMAGE_FILE_DEBUG_STRIPPED</span>
+                      <span class="p-Indicator">,</span> <span class="nv">IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP</span>
+                      <span class="p-Indicator">,</span> <span class="nv">IMAGE_FILE_NET_RUN_FROM_SWAP</span>
+                      <span class="p-Indicator">,</span> <span class="nv">IMAGE_FILE_SYSTEM</span>
+                      <span class="p-Indicator">,</span> <span class="nv">IMAGE_FILE_DLL</span>
+                      <span class="p-Indicator">,</span> <span class="nv">IMAGE_FILE_UP_SYSTEM_ONLY</span>
+                      <span class="p-Indicator">,</span> <span class="nv">IMAGE_FILE_BYTES_REVERSED_HI</span>
+                      <span class="p-Indicator">]</span>
+          <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">type</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">int</span>
+    <span class="l-Scalar-Plain">sections</span><span class="p-Indicator">:</span>
+      <span class="l-Scalar-Plain">type</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">seq</span>
+      <span class="l-Scalar-Plain">sequence</span><span class="p-Indicator">:</span>
+        <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">type</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">map</span>
+          <span class="l-Scalar-Plain">mapping</span><span class="p-Indicator">:</span>
+            <span class="l-Scalar-Plain">Name</span><span class="p-Indicator">:</span> <span class="p-Indicator">{</span><span class="nv">type</span><span class="p-Indicator">:</span> <span class="nv">str</span><span class="p-Indicator">}</span>
+            <span class="l-Scalar-Plain">Characteristics</span><span class="p-Indicator">:</span>
+              <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">type</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">seq</span>
+                <span class="l-Scalar-Plain">sequence</span><span class="p-Indicator">:</span>
+                  <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">type</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">str</span>
+                    <span class="l-Scalar-Plain">enum</span><span class="p-Indicator">:</span> <span class="p-Indicator">[</span> <span class="nv">IMAGE_SCN_TYPE_NO_PAD</span>
+                          <span class="p-Indicator">,</span> <span class="nv">IMAGE_SCN_CNT_CODE</span>
+                          <span class="p-Indicator">,</span> <span class="nv">IMAGE_SCN_CNT_INITIALIZED_DATA</span>
+                          <span class="p-Indicator">,</span> <span class="nv">IMAGE_SCN_CNT_UNINITIALIZED_DATA</span>
+                          <span class="p-Indicator">,</span> <span class="nv">IMAGE_SCN_LNK_OTHER</span>
+                          <span class="p-Indicator">,</span> <span class="nv">IMAGE_SCN_LNK_INFO</span>
+                          <span class="p-Indicator">,</span> <span class="nv">IMAGE_SCN_LNK_REMOVE</span>
+                          <span class="p-Indicator">,</span> <span class="nv">IMAGE_SCN_LNK_COMDAT</span>
+                          <span class="p-Indicator">,</span> <span class="nv">IMAGE_SCN_GPREL</span>
+                          <span class="p-Indicator">,</span> <span class="nv">IMAGE_SCN_MEM_PURGEABLE</span>
+                          <span class="p-Indicator">,</span> <span class="nv">IMAGE_SCN_MEM_16BIT</span>
+                          <span class="p-Indicator">,</span> <span class="nv">IMAGE_SCN_MEM_LOCKED</span>
+                          <span class="p-Indicator">,</span> <span class="nv">IMAGE_SCN_MEM_PRELOAD</span>
+                          <span class="p-Indicator">,</span> <span class="nv">IMAGE_SCN_ALIGN_1BYTES</span>
+                          <span class="p-Indicator">,</span> <span class="nv">IMAGE_SCN_ALIGN_2BYTES</span>
+                          <span class="p-Indicator">,</span> <span class="nv">IMAGE_SCN_ALIGN_4BYTES</span>
+                          <span class="p-Indicator">,</span> <span class="nv">IMAGE_SCN_ALIGN_8BYTES</span>
+                          <span class="p-Indicator">,</span> <span class="nv">IMAGE_SCN_ALIGN_16BYTES</span>
+                          <span class="p-Indicator">,</span> <span class="nv">IMAGE_SCN_ALIGN_32BYTES</span>
+                          <span class="p-Indicator">,</span> <span class="nv">IMAGE_SCN_ALIGN_64BYTES</span>
+                          <span class="p-Indicator">,</span> <span class="nv">IMAGE_SCN_ALIGN_128BYTES</span>
+                          <span class="p-Indicator">,</span> <span class="nv">IMAGE_SCN_ALIGN_256BYTES</span>
+                          <span class="p-Indicator">,</span> <span class="nv">IMAGE_SCN_ALIGN_512BYTES</span>
+                          <span class="p-Indicator">,</span> <span class="nv">IMAGE_SCN_ALIGN_1024BYTES</span>
+                          <span class="p-Indicator">,</span> <span class="nv">IMAGE_SCN_ALIGN_2048BYTES</span>
+                          <span class="p-Indicator">,</span> <span class="nv">IMAGE_SCN_ALIGN_4096BYTES</span>
+                          <span class="p-Indicator">,</span> <span class="nv">IMAGE_SCN_ALIGN_8192BYTES</span>
+                          <span class="p-Indicator">,</span> <span class="nv">IMAGE_SCN_LNK_NRELOC_OVFL</span>
+                          <span class="p-Indicator">,</span> <span class="nv">IMAGE_SCN_MEM_DISCARDABLE</span>
+                          <span class="p-Indicator">,</span> <span class="nv">IMAGE_SCN_MEM_NOT_CACHED</span>
+                          <span class="p-Indicator">,</span> <span class="nv">IMAGE_SCN_MEM_NOT_PAGED</span>
+                          <span class="p-Indicator">,</span> <span class="nv">IMAGE_SCN_MEM_SHARED</span>
+                          <span class="p-Indicator">,</span> <span class="nv">IMAGE_SCN_MEM_EXECUTE</span>
+                          <span class="p-Indicator">,</span> <span class="nv">IMAGE_SCN_MEM_READ</span>
+                          <span class="p-Indicator">,</span> <span class="nv">IMAGE_SCN_MEM_WRITE</span>
+                          <span class="p-Indicator">]</span>
+              <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">type</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">int</span>
+            <span class="l-Scalar-Plain">SectionData</span><span class="p-Indicator">:</span> <span class="p-Indicator">{</span><span class="nv">type</span><span class="p-Indicator">:</span> <span class="nv">str</span><span class="p-Indicator">}</span>
+    <span class="l-Scalar-Plain">symbols</span><span class="p-Indicator">:</span>
+      <span class="l-Scalar-Plain">type</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">seq</span>
+      <span class="l-Scalar-Plain">sequence</span><span class="p-Indicator">:</span>
+        <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">type</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">map</span>
+          <span class="l-Scalar-Plain">mapping</span><span class="p-Indicator">:</span>
+            <span class="l-Scalar-Plain">Name</span><span class="p-Indicator">:</span> <span class="p-Indicator">{</span><span class="nv">type</span><span class="p-Indicator">:</span> <span class="nv">str</span><span class="p-Indicator">}</span>
+            <span class="l-Scalar-Plain">Value</span><span class="p-Indicator">:</span> <span class="p-Indicator">{</span><span class="nv">type</span><span class="p-Indicator">:</span> <span class="nv">int</span><span class="p-Indicator">}</span>
+            <span class="l-Scalar-Plain">SectionNumber</span><span class="p-Indicator">:</span> <span class="p-Indicator">{</span><span class="nv">type</span><span class="p-Indicator">:</span> <span class="nv">int</span><span class="p-Indicator">}</span>
+            <span class="l-Scalar-Plain">SimpleType</span><span class="p-Indicator">:</span> <span class="p-Indicator">[</span> <span class="p-Indicator">{</span><span class="nv">type</span><span class="p-Indicator">:</span> <span class="nv">str</span><span class="p-Indicator">,</span> <span class="nv">enum</span><span class="p-Indicator">:</span> <span class="p-Indicator">[</span> <span class="nv">IMAGE_SYM_TYPE_NULL</span>
+                                            <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_TYPE_VOID</span>
+                                            <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_TYPE_CHAR</span>
+                                            <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_TYPE_SHORT</span>
+                                            <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_TYPE_INT</span>
+                                            <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_TYPE_LONG</span>
+                                            <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_TYPE_FLOAT</span>
+                                            <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_TYPE_DOUBLE</span>
+                                            <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_TYPE_STRUCT</span>
+                                            <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_TYPE_UNION</span>
+                                            <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_TYPE_ENUM</span>
+                                            <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_TYPE_MOE</span>
+                                            <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_TYPE_BYTE</span>
+                                            <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_TYPE_WORD</span>
+                                            <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_TYPE_UINT</span>
+                                            <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_TYPE_DWORD</span>
+                                            <span class="p-Indicator">]}</span>
+                        <span class="p-Indicator">,</span> <span class="p-Indicator">{</span><span class="nv">type</span><span class="p-Indicator">:</span> <span class="nv">int</span><span class="p-Indicator">}</span>
+                        <span class="p-Indicator">]</span>
+            <span class="l-Scalar-Plain">ComplexType</span><span class="p-Indicator">:</span> <span class="p-Indicator">[</span> <span class="p-Indicator">{</span><span class="nv">type</span><span class="p-Indicator">:</span> <span class="nv">str</span><span class="p-Indicator">,</span> <span class="nv">enum</span><span class="p-Indicator">:</span> <span class="p-Indicator">[</span> <span class="nv">IMAGE_SYM_DTYPE_NULL</span>
+                                             <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_DTYPE_POINTER</span>
+                                             <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_DTYPE_FUNCTION</span>
+                                             <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_DTYPE_ARRAY</span>
+                                             <span class="p-Indicator">]}</span>
+                         <span class="p-Indicator">,</span> <span class="p-Indicator">{</span><span class="nv">type</span><span class="p-Indicator">:</span> <span class="nv">int</span><span class="p-Indicator">}</span>
+                         <span class="p-Indicator">]</span>
+            <span class="l-Scalar-Plain">StorageClass</span><span class="p-Indicator">:</span> <span class="p-Indicator">[</span> <span class="p-Indicator">{</span><span class="nv">type</span><span class="p-Indicator">:</span> <span class="nv">str</span><span class="p-Indicator">,</span> <span class="nv">enum</span><span class="p-Indicator">:</span>
+                                        <span class="p-Indicator">[</span> <span class="nv">IMAGE_SYM_CLASS_END_OF_FUNCTION</span>
+                                        <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_CLASS_NULL</span>
+                                        <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_CLASS_AUTOMATIC</span>
+                                        <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_CLASS_EXTERNAL</span>
+                                        <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_CLASS_STATIC</span>
+                                        <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_CLASS_REGISTER</span>
+                                        <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_CLASS_EXTERNAL_DEF</span>
+                                        <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_CLASS_LABEL</span>
+                                        <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_CLASS_UNDEFINED_LABEL</span>
+                                        <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_CLASS_MEMBER_OF_STRUCT</span>
+                                        <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_CLASS_ARGUMENT</span>
+                                        <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_CLASS_STRUCT_TAG</span>
+                                        <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_CLASS_MEMBER_OF_UNION</span>
+                                        <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_CLASS_UNION_TAG</span>
+                                        <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_CLASS_TYPE_DEFINITION</span>
+                                        <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_CLASS_UNDEFINED_STATIC</span>
+                                        <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_CLASS_ENUM_TAG</span>
+                                        <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_CLASS_MEMBER_OF_ENUM</span>
+                                        <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_CLASS_REGISTER_PARAM</span>
+                                        <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_CLASS_BIT_FIELD</span>
+                                        <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_CLASS_BLOCK</span>
+                                        <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_CLASS_FUNCTION</span>
+                                        <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_CLASS_END_OF_STRUCT</span>
+                                        <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_CLASS_FILE</span>
+                                        <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_CLASS_SECTION</span>
+                                        <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_CLASS_WEAK_EXTERNAL</span>
+                                        <span class="p-Indicator">,</span> <span class="nv">IMAGE_SYM_CLASS_CLR_TOKEN</span>
+                                        <span class="p-Indicator">]}</span>
+                          <span class="p-Indicator">,</span> <span class="p-Indicator">{</span><span class="nv">type</span><span class="p-Indicator">:</span> <span class="nv">int</span><span class="p-Indicator">}</span>
+                          <span class="p-Indicator">]</span>
+</pre></div>
+</div>
+</div>
+</div>
+
+
+          </div>
+      </div>
+      <div class="clearer"></div>
+    </div>
+    <div class="related">
+      <h3>Navigation</h3>
+      <ul>
+        <li class="right" style="margin-right: 10px">
+          <a href="genindex.html" title="General Index"
+             >index</a></li>
+        <li class="right" >
+          <a href="HowToSubmitABug.html" title="How to submit an LLVM bug report"
+             >next</a> |</li>
+        <li class="right" >
+          <a href="HowToAddABuilder.html" title="How To Add Your Build Configuration To LLVM Buildbot Infrastructure"
+             >previous</a> |</li>
+  <li><a href="http://llvm.org/">LLVM Home</a> | </li>
+  <li><a href="index.html">Documentation</a>»</li>
+ 
+      </ul>
+    </div>
+    <div class="footer">
+        © Copyright 2003-2016, LLVM Project.
+      Last updated on 2016-01-04.
+      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.3.
+    </div>
+  </body>
+</html>
\ No newline at end of file

Added: www-releases/trunk/3.7.1/tools/docs/AddressSanitizer.html
URL: http://llvm.org/viewvc/llvm-project/www-releases/trunk/3.7.1/tools/docs/AddressSanitizer.html?rev=257950&view=auto
==============================================================================
--- www-releases/trunk/3.7.1/tools/docs/AddressSanitizer.html (added)
+++ www-releases/trunk/3.7.1/tools/docs/AddressSanitizer.html Fri Jan 15 17:13:16 2016
@@ -0,0 +1,335 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+
+
+<html xmlns="http://www.w3.org/1999/xhtml">
+  <head>
+    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+    
+    <title>AddressSanitizer — Clang 3.7 documentation</title>
+    
+    <link rel="stylesheet" href="_static/haiku.css" type="text/css" />
+    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
+    
+    <script type="text/javascript">
+      var DOCUMENTATION_OPTIONS = {
+        URL_ROOT:    './',
+        VERSION:     '3.7',
+        COLLAPSE_INDEX: false,
+        FILE_SUFFIX: '.html',
+        HAS_SOURCE:  true
+      };
+    </script>
+    <script type="text/javascript" src="_static/jquery.js"></script>
+    <script type="text/javascript" src="_static/underscore.js"></script>
+    <script type="text/javascript" src="_static/doctools.js"></script>
+    <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
+    <link rel="top" title="Clang 3.7 documentation" href="index.html" />
+    <link rel="next" title="ThreadSanitizer" href="ThreadSanitizer.html" />
+    <link rel="prev" title="Thread Safety Analysis" href="ThreadSafetyAnalysis.html" /> 
+  </head>
+  <body>
+      <div class="header"><h1 class="heading"><a href="index.html">
+          <span>Clang 3.7 documentation</span></a></h1>
+        <h2 class="heading"><span>AddressSanitizer</span></h2>
+      </div>
+      <div class="topnav">
+      
+        <p>
+        «  <a href="ThreadSafetyAnalysis.html">Thread Safety Analysis</a>
+          ::  
+        <a class="uplink" href="index.html">Contents</a>
+          ::  
+        <a href="ThreadSanitizer.html">ThreadSanitizer</a>  Â»
+        </p>
+
+      </div>
+      <div class="content">
+        
+        
+  <div class="section" id="addresssanitizer">
+<h1>AddressSanitizer<a class="headerlink" href="#addresssanitizer" title="Permalink to this headline">¶</a></h1>
+<div class="contents local topic" id="contents">
+<ul class="simple">
+<li><a class="reference internal" href="#introduction" id="id1">Introduction</a></li>
+<li><a class="reference internal" href="#how-to-build" id="id2">How to build</a></li>
+<li><a class="reference internal" href="#usage" id="id3">Usage</a></li>
+<li><a class="reference internal" href="#symbolizing-the-reports" id="id4">Symbolizing the Reports</a></li>
+<li><a class="reference internal" href="#additional-checks" id="id5">Additional Checks</a><ul>
+<li><a class="reference internal" href="#initialization-order-checking" id="id6">Initialization order checking</a></li>
+<li><a class="reference internal" href="#memory-leak-detection" id="id7">Memory leak detection</a></li>
+</ul>
+</li>
+<li><a class="reference internal" href="#issue-suppression" id="id8">Issue Suppression</a><ul>
+<li><a class="reference internal" href="#suppressing-reports-in-external-libraries" id="id9">Suppressing Reports in External Libraries</a></li>
+<li><a class="reference internal" href="#conditional-compilation-with-has-feature-address-sanitizer" id="id10">Conditional Compilation with <tt class="docutils literal"><span class="pre">__has_feature(address_sanitizer)</span></tt></a></li>
+<li><a class="reference internal" href="#disabling-instrumentation-with-attribute-no-sanitize-address" id="id11">Disabling Instrumentation with <tt class="docutils literal"><span class="pre">__attribute__((no_sanitize("address")))</span></tt></a></li>
+<li><a class="reference internal" href="#suppressing-errors-in-recompiled-code-blacklist" id="id12">Suppressing Errors in Recompiled Code (Blacklist)</a></li>
+</ul>
+</li>
+<li><a class="reference internal" href="#limitations" id="id13">Limitations</a></li>
+<li><a class="reference internal" href="#supported-platforms" id="id14">Supported Platforms</a></li>
+<li><a class="reference internal" href="#current-status" id="id15">Current Status</a></li>
+<li><a class="reference internal" href="#more-information" id="id16">More Information</a></li>
+</ul>
+</div>
+<div class="section" id="introduction">
+<h2><a class="toc-backref" href="#id1">Introduction</a><a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2>
+<p>AddressSanitizer is a fast memory error detector. It consists of a compiler
+instrumentation module and a run-time library. The tool can detect the
+following types of bugs:</p>
+<ul class="simple">
+<li>Out-of-bounds accesses to heap, stack and globals</li>
+<li>Use-after-free</li>
+<li>Use-after-return (to some extent)</li>
+<li>Double-free, invalid free</li>
+<li>Memory leaks (experimental)</li>
+</ul>
+<p>Typical slowdown introduced by AddressSanitizer is <strong>2x</strong>.</p>
+</div>
+<div class="section" id="how-to-build">
+<h2><a class="toc-backref" href="#id2">How to build</a><a class="headerlink" href="#how-to-build" title="Permalink to this headline">¶</a></h2>
+<p>Build LLVM/Clang with <a class="reference external" href="http://llvm.org/docs/CMake.html">CMake</a>.</p>
+</div>
+<div class="section" id="usage">
+<h2><a class="toc-backref" href="#id3">Usage</a><a class="headerlink" href="#usage" title="Permalink to this headline">¶</a></h2>
+<p>Simply compile and link your program with <tt class="docutils literal"><span class="pre">-fsanitize=address</span></tt> flag.  The
+AddressSanitizer run-time library should be linked to the final executable, so
+make sure to use <tt class="docutils literal"><span class="pre">clang</span></tt> (not <tt class="docutils literal"><span class="pre">ld</span></tt>) for the final link step.  When linking
+shared libraries, the AddressSanitizer run-time is not linked, so
+<tt class="docutils literal"><span class="pre">-Wl,-z,defs</span></tt> may cause link errors (don’t use it with AddressSanitizer).  To
+get a reasonable performance add <tt class="docutils literal"><span class="pre">-O1</span></tt> or higher.  To get nicer stack traces
+in error messages add <tt class="docutils literal"><span class="pre">-fno-omit-frame-pointer</span></tt>.  To get perfect stack traces
+you may need to disable inlining (just use <tt class="docutils literal"><span class="pre">-O1</span></tt>) and tail call elimination
+(<tt class="docutils literal"><span class="pre">-fno-optimize-sibling-calls</span></tt>).</p>
+<div class="highlight-console"><div class="highlight"><pre><span class="gp">%</span> cat example_UseAfterFree.cc
+<span class="go">int main(int argc, char **argv) {</span>
+<span class="go">  int *array = new int[100];</span>
+<span class="go">  delete [] array;</span>
+<span class="go">  return array[argc];  // BOOM</span>
+<span class="go">}</span>
+
+<span class="gp">#</span> Compile and link
+<span class="gp">%</span> clang -O1 -g -fsanitize<span class="o">=</span>address -fno-omit-frame-pointer example_UseAfterFree.cc
+</pre></div>
+</div>
+<p>or:</p>
+<div class="highlight-console"><div class="highlight"><pre><span class="gp">#</span> Compile
+<span class="gp">%</span> clang -O1 -g -fsanitize<span class="o">=</span>address -fno-omit-frame-pointer -c example_UseAfterFree.cc
+<span class="gp">#</span> Link
+<span class="gp">%</span> clang -g -fsanitize<span class="o">=</span>address example_UseAfterFree.o
+</pre></div>
+</div>
+<p>If a bug is detected, the program will print an error message to stderr and
+exit with a non-zero exit code. AddressSanitizer exits on the first detected error.
+This is by design:</p>
+<ul class="simple">
+<li>This approach allows AddressSanitizer to produce faster and smaller generated code
+(both by ~5%).</li>
+<li>Fixing bugs becomes unavoidable. AddressSanitizer does not produce
+false alarms. Once a memory corruption occurs, the program is in an inconsistent
+state, which could lead to confusing results and potentially misleading
+subsequent reports.</li>
+</ul>
+<p>If your process is sandboxed and you are running on OS X 10.10 or earlier, you
+will need to set <tt class="docutils literal"><span class="pre">DYLD_INSERT_LIBRARIES</span></tt> environment variable and point it to
+the ASan library that is packaged with the compiler used to build the
+executable. (You can find the library by searching for dynamic libraries with
+<tt class="docutils literal"><span class="pre">asan</span></tt> in their name.) If the environment variable is not set, the process will
+try to re-exec. Also keep in mind that when moving the executable to another machine,
+the ASan library will also need to be copied over.</p>
+</div>
+<div class="section" id="symbolizing-the-reports">
+<h2><a class="toc-backref" href="#id4">Symbolizing the Reports</a><a class="headerlink" href="#symbolizing-the-reports" title="Permalink to this headline">¶</a></h2>
+<p>To make AddressSanitizer symbolize its output
+you need to set the <tt class="docutils literal"><span class="pre">ASAN_SYMBOLIZER_PATH</span></tt> environment variable to point to
+the <tt class="docutils literal"><span class="pre">llvm-symbolizer</span></tt> binary (or make sure <tt class="docutils literal"><span class="pre">llvm-symbolizer</span></tt> is in your
+<tt class="docutils literal"><span class="pre">$PATH</span></tt>):</p>
+<div class="highlight-console"><div class="highlight"><pre><span class="gp">%</span> <span class="nv">ASAN_SYMBOLIZER_PATH</span><span class="o">=</span>/usr/local/bin/llvm-symbolizer ./a.out
+<span class="go">==9442== ERROR: AddressSanitizer heap-use-after-free on address 0x7f7ddab8c084 at pc 0x403c8c bp 0x7fff87fb82d0 sp 0x7fff87fb82c8</span>
+<span class="go">READ of size 4 at 0x7f7ddab8c084 thread T0</span>
+<span class="go">    #0 0x403c8c in main example_UseAfterFree.cc:4</span>
+<span class="go">    #1 0x7f7ddabcac4d in __libc_start_main ??:0</span>
+<span class="go">0x7f7ddab8c084 is located 4 bytes inside of 400-byte region [0x7f7ddab8c080,0x7f7ddab8c210)</span>
+<span class="go">freed by thread T0 here:</span>
+<span class="go">    #0 0x404704 in operator delete[](void*) ??:0</span>
+<span class="go">    #1 0x403c53 in main example_UseAfterFree.cc:4</span>
+<span class="go">    #2 0x7f7ddabcac4d in __libc_start_main ??:0</span>
+<span class="go">previously allocated by thread T0 here:</span>
+<span class="go">    #0 0x404544 in operator new[](unsigned long) ??:0</span>
+<span class="go">    #1 0x403c43 in main example_UseAfterFree.cc:2</span>
+<span class="go">    #2 0x7f7ddabcac4d in __libc_start_main ??:0</span>
+<span class="go">==9442== ABORTING</span>
+</pre></div>
+</div>
+<p>If that does not work for you (e.g. your process is sandboxed), you can use a
+separate script to symbolize the result offline (online symbolization can be
+force disabled by setting <tt class="docutils literal"><span class="pre">ASAN_OPTIONS=symbolize=0</span></tt>):</p>
+<div class="highlight-console"><div class="highlight"><pre><span class="gp">%</span> <span class="nv">ASAN_OPTIONS</span><span class="o">=</span><span class="nv">symbolize</span><span class="o">=</span>0 ./a.out 2> log
+<span class="gp">%</span> projects/compiler-rt/lib/asan/scripts/asan_symbolize.py / < log | c++filt
+<span class="go">==9442== ERROR: AddressSanitizer heap-use-after-free on address 0x7f7ddab8c084 at pc 0x403c8c bp 0x7fff87fb82d0 sp 0x7fff87fb82c8</span>
+<span class="go">READ of size 4 at 0x7f7ddab8c084 thread T0</span>
+<span class="go">    #0 0x403c8c in main example_UseAfterFree.cc:4</span>
+<span class="go">    #1 0x7f7ddabcac4d in __libc_start_main ??:0</span>
+<span class="go">...</span>
+</pre></div>
+</div>
+<p>Note that on OS X you may need to run <tt class="docutils literal"><span class="pre">dsymutil</span></tt> on your binary to have the
+file:line info in the AddressSanitizer reports.</p>
+</div>
+<div class="section" id="additional-checks">
+<h2><a class="toc-backref" href="#id5">Additional Checks</a><a class="headerlink" href="#additional-checks" title="Permalink to this headline">¶</a></h2>
+<div class="section" id="initialization-order-checking">
+<h3><a class="toc-backref" href="#id6">Initialization order checking</a><a class="headerlink" href="#initialization-order-checking" title="Permalink to this headline">¶</a></h3>
+<p>AddressSanitizer can optionally detect dynamic initialization order problems,
+when initialization of globals defined in one translation unit uses
+globals defined in another translation unit. To enable this check at runtime,
+you should set environment variable
+<tt class="docutils literal"><span class="pre">ASAN_OPTIONS=check_initialization_order=1</span></tt>.</p>
+<p>Note that this option is not supported on OS X.</p>
+</div>
+<div class="section" id="memory-leak-detection">
+<h3><a class="toc-backref" href="#id7">Memory leak detection</a><a class="headerlink" href="#memory-leak-detection" title="Permalink to this headline">¶</a></h3>
+<p>For more information on leak detector in AddressSanitizer, see
+<a class="reference internal" href="LeakSanitizer.html"><em>LeakSanitizer</em></a>. The leak detection is turned on by default on Linux;
+however, it is not yet supported on other platforms.</p>
+</div>
+</div>
+<div class="section" id="issue-suppression">
+<h2><a class="toc-backref" href="#id8">Issue Suppression</a><a class="headerlink" href="#issue-suppression" title="Permalink to this headline">¶</a></h2>
+<p>AddressSanitizer is not expected to produce false positives. If you see one,
+look again; most likely it is a true positive!</p>
+<div class="section" id="suppressing-reports-in-external-libraries">
+<h3><a class="toc-backref" href="#id9">Suppressing Reports in External Libraries</a><a class="headerlink" href="#suppressing-reports-in-external-libraries" title="Permalink to this headline">¶</a></h3>
+<p>Runtime interposition allows AddressSanitizer to find bugs in code that is
+not being recompiled. If you run into an issue in external libraries, we
+recommend immediately reporting it to the library maintainer so that it
+gets addressed. However, you can use the following suppression mechanism
+to unblock yourself and continue on with the testing. This suppression
+mechanism should only be used for suppressing issues in external code; it
+does not work on code recompiled with AddressSanitizer. To suppress errors
+in external libraries, set the <tt class="docutils literal"><span class="pre">ASAN_OPTIONS</span></tt> environment variable to point
+to a suppression file. You can either specify the full path to the file or the
+path of the file relative to the location of your executable.</p>
+<div class="highlight-bash"><div class="highlight"><pre><span class="nv">ASAN_OPTIONS</span><span class="o">=</span><span class="nv">suppressions</span><span class="o">=</span>MyASan.supp
+</pre></div>
+</div>
+<p>Use the following format to specify the names of the functions or libraries
+you want to suppress. You can see these in the error report. Remember that
+the narrower the scope of the suppression, the more bugs you will be able to
+catch.</p>
+<div class="highlight-bash"><div class="highlight"><pre>interceptor_via_fun:NameOfCFunctionToSuppress
+interceptor_via_fun:-<span class="o">[</span>ClassName objCMethodToSuppress:<span class="o">]</span>
+interceptor_via_lib:NameOfTheLibraryToSuppress
+</pre></div>
+</div>
+</div>
+<div class="section" id="conditional-compilation-with-has-feature-address-sanitizer">
+<h3><a class="toc-backref" href="#id10">Conditional Compilation with <tt class="docutils literal"><span class="pre">__has_feature(address_sanitizer)</span></tt></a><a class="headerlink" href="#conditional-compilation-with-has-feature-address-sanitizer" title="Permalink to this headline">¶</a></h3>
+<p>In some cases one may need to execute different code depending on whether
+AddressSanitizer is enabled.
+<a class="reference internal" href="LanguageExtensions.html#langext-has-feature-has-extension"><em>__has_feature</em></a> can be used for
+this purpose.</p>
+<div class="highlight-c"><div class="highlight"><pre><span class="cp">#if defined(__has_feature)</span>
+<span class="cp">#  if __has_feature(address_sanitizer)</span>
+<span class="c1">// code that builds only under AddressSanitizer</span>
+<span class="cp">#  endif</span>
+<span class="cp">#endif</span>
+</pre></div>
+</div>
+</div>
+<div class="section" id="disabling-instrumentation-with-attribute-no-sanitize-address">
+<h3><a class="toc-backref" href="#id11">Disabling Instrumentation with <tt class="docutils literal"><span class="pre">__attribute__((no_sanitize("address")))</span></tt></a><a class="headerlink" href="#disabling-instrumentation-with-attribute-no-sanitize-address" title="Permalink to this headline">¶</a></h3>
+<p>Some code should not be instrumented by AddressSanitizer. One may use the
+function attribute <tt class="docutils literal"><span class="pre">__attribute__((no_sanitize("address")))</span></tt>
+(which has deprecated synonyms
+<a class="reference internal" href="AttributeReference.html#langext-address-sanitizer"><em>no_sanitize_address</em></a> and
+<cite>no_address_safety_analysis</cite>) to disable instrumentation of a particular
+function. This attribute may not be supported by other compilers, so we suggest
+to use it together with <tt class="docutils literal"><span class="pre">__has_feature(address_sanitizer)</span></tt>.</p>
+</div>
+<div class="section" id="suppressing-errors-in-recompiled-code-blacklist">
+<h3><a class="toc-backref" href="#id12">Suppressing Errors in Recompiled Code (Blacklist)</a><a class="headerlink" href="#suppressing-errors-in-recompiled-code-blacklist" title="Permalink to this headline">¶</a></h3>
+<p>AddressSanitizer supports <tt class="docutils literal"><span class="pre">src</span></tt> and <tt class="docutils literal"><span class="pre">fun</span></tt> entity types in
+<a class="reference internal" href="SanitizerSpecialCaseList.html"><em>Sanitizer special case list</em></a>, that can be used to suppress error reports
+in the specified source files or functions. Additionally, AddressSanitizer
+introduces <tt class="docutils literal"><span class="pre">global</span></tt> and <tt class="docutils literal"><span class="pre">type</span></tt> entity types that can be used to
+suppress error reports for out-of-bound access to globals with certain
+names and types (you may only specify class or struct types).</p>
+<p>You may use an <tt class="docutils literal"><span class="pre">init</span></tt> category to suppress reports about initialization-order
+problems happening in certain source files or with certain global variables.</p>
+<div class="highlight-bash"><div class="highlight"><pre><span class="c"># Suppress error reports for code in a file or in a function:</span>
+src:bad_file.cpp
+<span class="c"># Ignore all functions with names containing MyFooBar:</span>
+fun:*MyFooBar*
+<span class="c"># Disable out-of-bound checks for global:</span>
+global:bad_array
+<span class="c"># Disable out-of-bound checks for global instances of a given class ...</span>
+<span class="nb">type</span>:Namespace::BadClassName
+<span class="c"># ... or a given struct. Use wildcard to deal with anonymous namespace.</span>
+<span class="nb">type</span>:Namespace2::*::BadStructName
+<span class="c"># Disable initialization-order checks for globals:</span>
+global:bad_init_global<span class="o">=</span>init
+<span class="nb">type</span>:*BadInitClassSubstring*<span class="o">=</span>init
+src:bad/init/files/*<span class="o">=</span>init
+</pre></div>
+</div>
+</div>
+</div>
+<div class="section" id="limitations">
+<h2><a class="toc-backref" href="#id13">Limitations</a><a class="headerlink" href="#limitations" title="Permalink to this headline">¶</a></h2>
+<ul class="simple">
+<li>AddressSanitizer uses more real memory than a native run. Exact overhead
+depends on the allocations sizes. The smaller the allocations you make the
+bigger the overhead is.</li>
+<li>AddressSanitizer uses more stack memory. We have seen up to 3x increase.</li>
+<li>On 64-bit platforms AddressSanitizer maps (but not reserves) 16+ Terabytes of
+virtual address space. This means that tools like <tt class="docutils literal"><span class="pre">ulimit</span></tt> may not work as
+usually expected.</li>
+<li>Static linking is not supported.</li>
+</ul>
+</div>
+<div class="section" id="supported-platforms">
+<h2><a class="toc-backref" href="#id14">Supported Platforms</a><a class="headerlink" href="#supported-platforms" title="Permalink to this headline">¶</a></h2>
+<p>AddressSanitizer is supported on:</p>
+<ul class="simple">
+<li>Linux i386/x86_64 (tested on Ubuntu 12.04)</li>
+<li>OS X 10.7 - 10.11 (i386/x86_64)</li>
+<li>iOS Simulator</li>
+<li>Android ARM</li>
+<li>FreeBSD i386/x86_64 (tested on FreeBSD 11-current)</li>
+</ul>
+<p>Ports to various other platforms are in progress.</p>
+</div>
+<div class="section" id="current-status">
+<h2><a class="toc-backref" href="#id15">Current Status</a><a class="headerlink" href="#current-status" title="Permalink to this headline">¶</a></h2>
+<p>AddressSanitizer is fully functional on supported platforms starting from LLVM
+3.1. The test suite is integrated into CMake build and can be run with <tt class="docutils literal"><span class="pre">make</span>
+<span class="pre">check-asan</span></tt> command.</p>
+</div>
+<div class="section" id="more-information">
+<h2><a class="toc-backref" href="#id16">More Information</a><a class="headerlink" href="#more-information" title="Permalink to this headline">¶</a></h2>
+<p><a class="reference external" href="http://code.google.com/p/address-sanitizer/">http://code.google.com/p/address-sanitizer</a></p>
+</div>
+</div>
+
+
+      </div>
+      <div class="bottomnav">
+      
+        <p>
+        «  <a href="ThreadSafetyAnalysis.html">Thread Safety Analysis</a>
+          ::  
+        <a class="uplink" href="index.html">Contents</a>
+          ::  
+        <a href="ThreadSanitizer.html">ThreadSanitizer</a>  Â»
+        </p>
+
+      </div>
+
+    <div class="footer">
+        © Copyright 2007-2015, The Clang Team.
+      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.3.
+    </div>
+  </body>
+</html>
\ No newline at end of file

Added: www-releases/trunk/3.7.1/tools/docs/AttributeReference.html
URL: http://llvm.org/viewvc/llvm-project/www-releases/trunk/3.7.1/tools/docs/AttributeReference.html?rev=257950&view=auto
==============================================================================
--- www-releases/trunk/3.7.1/tools/docs/AttributeReference.html (added)
+++ www-releases/trunk/3.7.1/tools/docs/AttributeReference.html Fri Jan 15 17:13:16 2016
@@ -0,0 +1,3094 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+
+
+<html xmlns="http://www.w3.org/1999/xhtml">
+  <head>
+    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+    
+    <title>Attributes in Clang — Clang 3.7 documentation</title>
+    
+    <link rel="stylesheet" href="_static/haiku.css" type="text/css" />
+    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
+    
+    <script type="text/javascript">
+      var DOCUMENTATION_OPTIONS = {
+        URL_ROOT:    './',
+        VERSION:     '3.7',
+        COLLAPSE_INDEX: false,
+        FILE_SUFFIX: '.html',
+        HAS_SOURCE:  true
+      };
+    </script>
+    <script type="text/javascript" src="_static/jquery.js"></script>
+    <script type="text/javascript" src="_static/underscore.js"></script>
+    <script type="text/javascript" src="_static/doctools.js"></script>
+    <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
+    <link rel="top" title="Clang 3.7 documentation" href="index.html" />
+    <link rel="next" title="Cross-compilation using Clang" href="CrossCompilation.html" />
+    <link rel="prev" title="Objective-C Automatic Reference Counting (ARC)" href="AutomaticReferenceCounting.html" /> 
+  </head>
+  <body>
+      <div class="header"><h1 class="heading"><a href="index.html">
+          <span>Clang 3.7 documentation</span></a></h1>
+        <h2 class="heading"><span>Attributes in Clang</span></h2>
+      </div>
+      <div class="topnav">
+      
+        <p>
+        «  <a href="AutomaticReferenceCounting.html">Objective-C Automatic Reference Counting (ARC)</a>
+          ::  
+        <a class="uplink" href="index.html">Contents</a>
+          ::  
+        <a href="CrossCompilation.html">Cross-compilation using Clang</a>  Â»
+        </p>
+
+      </div>
+      <div class="content">
+        
+        
+  <div class="section" id="attributes-in-clang">
+<h1>Attributes in Clang<a class="headerlink" href="#attributes-in-clang" title="Permalink to this headline">¶</a></h1>
+<div class="contents local topic" id="contents">
+<ul class="simple">
+<li><a class="reference internal" href="#introduction" id="id5">Introduction</a></li>
+<li><a class="reference internal" href="#amd-gpu-register-attributes" id="id6">AMD GPU Register Attributes</a><ul>
+<li><a class="reference internal" href="#amdgpu-num-sgpr" id="id7">amdgpu_num_sgpr</a></li>
+<li><a class="reference internal" href="#amdgpu-num-vgpr" id="id8">amdgpu_num_vgpr</a></li>
+</ul>
+</li>
+<li><a class="reference internal" href="#function-attributes" id="id9">Function Attributes</a><ul>
+<li><a class="reference internal" href="#interrupt" id="id10">interrupt</a></li>
+<li><a class="reference internal" href="#acquire-capability-acquire-shared-capability-clang-acquire-capability-clang-acquire-shared-capability" id="id11">acquire_capability (acquire_shared_capability, clang::acquire_capability, clang::acquire_shared_capability)</a></li>
+<li><a class="reference internal" href="#assert-capability-assert-shared-capability-clang-assert-capability-clang-assert-shared-capability" id="id12">assert_capability (assert_shared_capability, clang::assert_capability, clang::assert_shared_capability)</a></li>
+<li><a class="reference internal" href="#assume-aligned-gnu-assume-aligned" id="id13">assume_aligned (gnu::assume_aligned)</a></li>
+<li><a class="reference internal" href="#availability" id="id14">availability</a></li>
+<li><a class="reference internal" href="#noreturn" id="id15">_Noreturn</a></li>
+<li><a class="reference internal" href="#id1" id="id16">noreturn</a></li>
+<li><a class="reference internal" href="#carries-dependency" id="id17">carries_dependency</a></li>
+<li><a class="reference internal" href="#enable-if" id="id18">enable_if</a></li>
+<li><a class="reference internal" href="#flatten-gnu-flatten" id="id19">flatten (gnu::flatten)</a></li>
+<li><a class="reference internal" href="#format-gnu-format" id="id20">format (gnu::format)</a></li>
+<li><a class="reference internal" href="#noduplicate-clang-noduplicate" id="id21">noduplicate (clang::noduplicate)</a></li>
+<li><a class="reference internal" href="#no-sanitize-clang-no-sanitize" id="id22">no_sanitize (clang::no_sanitize)</a></li>
+<li><a class="reference internal" href="#no-sanitize-address-no-address-safety-analysis-gnu-no-address-safety-analysis-gnu-no-sanitize-address" id="id23">no_sanitize_address (no_address_safety_analysis, gnu::no_address_safety_analysis, gnu::no_sanitize_address)</a></li>
+<li><a class="reference internal" href="#no-sanitize-thread" id="id24">no_sanitize_thread</a></li>
+<li><a class="reference internal" href="#no-sanitize-memory" id="id25">no_sanitize_memory</a></li>
+<li><a class="reference internal" href="#no-split-stack-gnu-no-split-stack" id="id26">no_split_stack (gnu::no_split_stack)</a></li>
+<li><a class="reference internal" href="#objc-boxable" id="id27">objc_boxable</a></li>
+<li><a class="reference internal" href="#objc-method-family" id="id28">objc_method_family</a></li>
+<li><a class="reference internal" href="#objc-requires-super" id="id29">objc_requires_super</a></li>
+<li><a class="reference internal" href="#objc-runtime-name" id="id30">objc_runtime_name</a></li>
+<li><a class="reference internal" href="#optnone-clang-optnone" id="id31">optnone (clang::optnone)</a></li>
+<li><a class="reference internal" href="#overloadable" id="id32">overloadable</a></li>
+<li><a class="reference internal" href="#release-capability-release-shared-capability-clang-release-capability-clang-release-shared-capability" id="id33">release_capability (release_shared_capability, clang::release_capability, clang::release_shared_capability)</a></li>
+<li><a class="reference internal" href="#target-gnu-target" id="id34">target (gnu::target)</a></li>
+<li><a class="reference internal" href="#try-acquire-capability-try-acquire-shared-capability-clang-try-acquire-capability-clang-try-acquire-shared-capability" id="id35">try_acquire_capability (try_acquire_shared_capability, clang::try_acquire_capability, clang::try_acquire_shared_capability)</a></li>
+</ul>
+</li>
+<li><a class="reference internal" href="#variable-attributes" id="id36">Variable Attributes</a><ul>
+<li><a class="reference internal" href="#init-seg" id="id37">init_seg</a></li>
+<li><a class="reference internal" href="#section-gnu-section-declspec-allocate" id="id38">section (gnu::section, __declspec(allocate))</a></li>
+<li><a class="reference internal" href="#tls-model-gnu-tls-model" id="id39">tls_model (gnu::tls_model)</a></li>
+<li><a class="reference internal" href="#thread" id="id40">thread</a></li>
+</ul>
+</li>
+<li><a class="reference internal" href="#type-attributes" id="id41">Type Attributes</a><ul>
+<li><a class="reference internal" href="#align-value" id="id42">align_value</a></li>
+<li><a class="reference internal" href="#flag-enum" id="id43">flag_enum</a></li>
+<li><a class="reference internal" href="#single-inhertiance-multiple-inheritance-virtual-inheritance" id="id44">__single_inhertiance, __multiple_inheritance, __virtual_inheritance</a></li>
+<li><a class="reference internal" href="#novtable" id="id45">novtable</a></li>
+</ul>
+</li>
+<li><a class="reference internal" href="#statement-attributes" id="id46">Statement Attributes</a><ul>
+<li><a class="reference internal" href="#fallthrough-clang-fallthrough" id="id47">fallthrough (clang::fallthrough)</a></li>
+<li><a class="reference internal" href="#pragma-clang-loop" id="id48">#pragma clang loop</a></li>
+<li><a class="reference internal" href="#pragma-unroll-pragma-nounroll" id="id49">#pragma unroll, #pragma nounroll</a></li>
+</ul>
+</li>
+<li><a class="reference internal" href="#calling-conventions" id="id50">Calling Conventions</a><ul>
+<li><a class="reference internal" href="#fastcall-gnu-fastcall-fastcall-fastcall" id="id51">fastcall (gnu::fastcall, __fastcall, _fastcall)</a></li>
+<li><a class="reference internal" href="#ms-abi-gnu-ms-abi" id="id52">ms_abi (gnu::ms_abi)</a></li>
+<li><a class="reference internal" href="#pcs-gnu-pcs" id="id53">pcs (gnu::pcs)</a></li>
+<li><a class="reference internal" href="#regparm-gnu-regparm" id="id54">regparm (gnu::regparm)</a></li>
+<li><a class="reference internal" href="#stdcall-gnu-stdcall-stdcall-stdcall" id="id55">stdcall (gnu::stdcall, __stdcall, _stdcall)</a></li>
+<li><a class="reference internal" href="#thiscall-gnu-thiscall-thiscall-thiscall" id="id56">thiscall (gnu::thiscall, __thiscall, _thiscall)</a></li>
+<li><a class="reference internal" href="#vectorcall-vectorcall-vectorcall" id="id57">vectorcall (__vectorcall, _vectorcall)</a></li>
+</ul>
+</li>
+<li><a class="reference internal" href="#consumed-annotation-checking" id="id58">Consumed Annotation Checking</a><ul>
+<li><a class="reference internal" href="#callable-when" id="id59">callable_when</a></li>
+<li><a class="reference internal" href="#consumable" id="id60">consumable</a></li>
+<li><a class="reference internal" href="#param-typestate" id="id61">param_typestate</a></li>
+<li><a class="reference internal" href="#return-typestate" id="id62">return_typestate</a></li>
+<li><a class="reference internal" href="#set-typestate" id="id63">set_typestate</a></li>
+<li><a class="reference internal" href="#test-typestate" id="id64">test_typestate</a></li>
+</ul>
+</li>
+<li><a class="reference internal" href="#type-safety-checking" id="id65">Type Safety Checking</a><ul>
+<li><a class="reference internal" href="#argument-with-type-tag" id="id66">argument_with_type_tag</a></li>
+<li><a class="reference internal" href="#pointer-with-type-tag" id="id67">pointer_with_type_tag</a></li>
+<li><a class="reference internal" href="#type-tag-for-datatype" id="id68">type_tag_for_datatype</a></li>
+</ul>
+</li>
+<li><a class="reference internal" href="#opencl-address-spaces" id="id69">OpenCL Address Spaces</a><ul>
+<li><a class="reference internal" href="#constant-constant" id="id70">__constant(constant)</a></li>
+<li><a class="reference internal" href="#generic-generic" id="id71">__generic(generic)</a></li>
+<li><a class="reference internal" href="#global-global" id="id72">__global(global)</a></li>
+<li><a class="reference internal" href="#local-local" id="id73">__local(local)</a></li>
+<li><a class="reference internal" href="#private-private" id="id74">__private(private)</a></li>
+</ul>
+</li>
+<li><a class="reference internal" href="#nullability-attributes" id="id75">Nullability Attributes</a><ul>
+<li><a class="reference internal" href="#nonnull" id="id76">nonnull</a></li>
+<li><a class="reference internal" href="#returns-nonnull" id="id77">returns_nonnull</a></li>
+<li><a class="reference internal" href="#id4" id="id78">_Nonnull</a></li>
+<li><a class="reference internal" href="#null-unspecified" id="id79">_Null_unspecified</a></li>
+<li><a class="reference internal" href="#nullable" id="id80">_Nullable</a></li>
+</ul>
+</li>
+</ul>
+</div>
+<div class="section" id="introduction">
+<h2><a class="toc-backref" href="#id5">Introduction</a><a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2>
+<p>This page lists the attributes currently supported by Clang.</p>
+</div>
+<div class="section" id="amd-gpu-register-attributes">
+<h2><a class="toc-backref" href="#id6">AMD GPU Register Attributes</a><a class="headerlink" href="#amd-gpu-register-attributes" title="Permalink to this headline">¶</a></h2>
+<p>Clang supports attributes for controlling register usage on AMD GPU
+targets. These attributes may be attached to a kernel function
+definition and is an optimization hint to the backend for the maximum
+number of registers to use. This is useful in cases where register
+limited occupancy is known to be an important factor for the
+performance for the kernel.</p>
+<p>The semantics are as follows:</p>
+<ul class="simple">
+<li>The backend will attempt to limit the number of used registers to
+the specified value, but the exact number used is not
+guaranteed. The number used may be rounded up to satisfy the
+allocation requirements or ABI constraints of the subtarget. For
+example, on Southern Islands VGPRs may only be allocated in
+increments of 4, so requesting a limit of 39 VGPRs will really
+attempt to use up to 40. Requesting more registers than the
+subtarget supports will truncate to the maximum allowed. The backend
+may also use fewer registers than requested whenever possible.</li>
+<li>0 implies the default no limit on register usage.</li>
+<li>Ignored on older VLIW subtargets which did not have separate scalar
+and vector registers, R600 through Northern Islands.</li>
+</ul>
+<div class="section" id="amdgpu-num-sgpr">
+<h3><a class="toc-backref" href="#id7">amdgpu_num_sgpr</a><a class="headerlink" href="#amdgpu-num-sgpr" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>Clang supports the
+<tt class="docutils literal"><span class="pre">__attribute__((amdgpu_num_sgpr(<num_registers>)))</span></tt> attribute on AMD
+Southern Islands GPUs and later for controlling the number of scalar
+registers. A typical value would be between 8 and 104 in increments of
+8.</p>
+<p>Due to common instruction constraints, an additional 2-4 SGPRs are
+typically required for internal use depending on features used. This
+value is a hint for the total number of SGPRs to use, and not the
+number of user SGPRs, so no special consideration needs to be given
+for these.</p>
+</div>
+<div class="section" id="amdgpu-num-vgpr">
+<h3><a class="toc-backref" href="#id8">amdgpu_num_vgpr</a><a class="headerlink" href="#amdgpu-num-vgpr" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>Clang supports the
+<tt class="docutils literal"><span class="pre">__attribute__((amdgpu_num_vgpr(<num_registers>)))</span></tt> attribute on AMD
+Southern Islands GPUs and later for controlling the number of vector
+registers. A typical value would be between 4 and 256 in increments
+of 4.</p>
+</div>
+</div>
+<div class="section" id="function-attributes">
+<h2><a class="toc-backref" href="#id9">Function Attributes</a><a class="headerlink" href="#function-attributes" title="Permalink to this headline">¶</a></h2>
+<div class="section" id="interrupt">
+<h3><a class="toc-backref" href="#id10">interrupt</a><a class="headerlink" href="#interrupt" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>Clang supports the GNU style <tt class="docutils literal"><span class="pre">__attribute__((interrupt("TYPE")))</span></tt> attribute on
+ARM targets. This attribute may be attached to a function definition and
+instructs the backend to generate appropriate function entry/exit code so that
+it can be used directly as an interrupt service routine.</p>
+<p>The parameter passed to the interrupt attribute is optional, but if
+provided it must be a string literal with one of the following values: “IRQ”,
+“FIQ”, “SWI”, “ABORT”, “UNDEF”.</p>
+<p>The semantics are as follows:</p>
+<ul>
+<li><p class="first">If the function is AAPCS, Clang instructs the backend to realign the stack to
+8 bytes on entry. This is a general requirement of the AAPCS at public
+interfaces, but may not hold when an exception is taken. Doing this allows
+other AAPCS functions to be called.</p>
+</li>
+<li><p class="first">If the CPU is M-class this is all that needs to be done since the architecture
+itself is designed in such a way that functions obeying the normal AAPCS ABI
+constraints are valid exception handlers.</p>
+</li>
+<li><p class="first">If the CPU is not M-class, the prologue and epilogue are modified to save all
+non-banked registers that are used, so that upon return the user-mode state
+will not be corrupted. Note that to avoid unnecessary overhead, only
+general-purpose (integer) registers are saved in this way. If VFP operations
+are needed, that state must be saved manually.</p>
+<p>Specifically, interrupt kinds other than “FIQ” will save all core registers
+except “lr” and “sp”. “FIQ” interrupts will save r0-r7.</p>
+</li>
+<li><p class="first">If the CPU is not M-class, the return instruction is changed to one of the
+canonical sequences permitted by the architecture for exception return. Where
+possible the function itself will make the necessary “lr” adjustments so that
+the “preferred return address” is selected.</p>
+<p>Unfortunately the compiler is unable to make this guarantee for an “UNDEF”
+handler, where the offset from “lr” to the preferred return address depends on
+the execution state of the code which generated the exception. In this case
+a sequence equivalent to “movs pc, lr” will be used.</p>
+</li>
+</ul>
+</div>
+<div class="section" id="acquire-capability-acquire-shared-capability-clang-acquire-capability-clang-acquire-shared-capability">
+<h3><a class="toc-backref" href="#id11">acquire_capability (acquire_shared_capability, clang::acquire_capability, clang::acquire_shared_capability)</a><a class="headerlink" href="#acquire-capability-acquire-shared-capability-clang-acquire-capability-clang-acquire-shared-capability" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>Marks a function as acquiring a capability.</p>
+</div>
+<div class="section" id="assert-capability-assert-shared-capability-clang-assert-capability-clang-assert-shared-capability">
+<h3><a class="toc-backref" href="#id12">assert_capability (assert_shared_capability, clang::assert_capability, clang::assert_shared_capability)</a><a class="headerlink" href="#assert-capability-assert-shared-capability-clang-assert-capability-clang-assert-shared-capability" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>Marks a function that dynamically tests whether a capability is held, and halts
+the program if it is not held.</p>
+</div>
+<div class="section" id="assume-aligned-gnu-assume-aligned">
+<h3><a class="toc-backref" href="#id13">assume_aligned (gnu::assume_aligned)</a><a class="headerlink" href="#assume-aligned-gnu-assume-aligned" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>Use <tt class="docutils literal"><span class="pre">__attribute__((assume_aligned(<alignment>[,<offset>]))</span></tt> on a function
+declaration to specify that the return value of the function (which must be a
+pointer type) has the specified offset, in bytes, from an address with the
+specified alignment. The offset is taken to be zero if omitted.</p>
+<div class="highlight-c++"><div class="highlight"><pre><span class="c1">// The returned pointer value has 32-byte alignment.</span>
+<span class="kt">void</span> <span class="o">*</span><span class="nf">a</span><span class="p">()</span> <span class="n">__attribute__</span><span class="p">((</span><span class="n">assume_aligned</span> <span class="p">(</span><span class="mi">32</span><span class="p">)));</span>
+
+<span class="c1">// The returned pointer value is 4 bytes greater than an address having</span>
+<span class="c1">// 32-byte alignment.</span>
+<span class="kt">void</span> <span class="o">*</span><span class="nf">b</span><span class="p">()</span> <span class="n">__attribute__</span><span class="p">((</span><span class="n">assume_aligned</span> <span class="p">(</span><span class="mi">32</span><span class="p">,</span> <span class="mi">4</span><span class="p">)));</span>
+</pre></div>
+</div>
+<p>Note that this attribute provides information to the compiler regarding a
+condition that the code already ensures is true. It does not cause the compiler
+to enforce the provided alignment assumption.</p>
+</div>
+<div class="section" id="availability">
+<h3><a class="toc-backref" href="#id14">availability</a><a class="headerlink" href="#availability" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>The <tt class="docutils literal"><span class="pre">availability</span></tt> attribute can be placed on declarations to describe the
+lifecycle of that declaration relative to operating system versions.  Consider
+the function declaration for a hypothetical function <tt class="docutils literal"><span class="pre">f</span></tt>:</p>
+<div class="highlight-c++"><div class="highlight"><pre><span class="kt">void</span> <span class="nf">f</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="n">__attribute__</span><span class="p">((</span><span class="n">availability</span><span class="p">(</span><span class="n">macosx</span><span class="p">,</span><span class="n">introduced</span><span class="o">=</span><span class="mf">10.4</span><span class="p">,</span><span class="n">deprecated</span><span class="o">=</span><span class="mf">10.6</span><span class="p">,</span><span class="n">obsoleted</span><span class="o">=</span><span class="mf">10.7</span><span class="p">)));</span>
+</pre></div>
+</div>
+<p>The availability attribute states that <tt class="docutils literal"><span class="pre">f</span></tt> was introduced in Mac OS X 10.4,
+deprecated in Mac OS X 10.6, and obsoleted in Mac OS X 10.7.  This information
+is used by Clang to determine when it is safe to use <tt class="docutils literal"><span class="pre">f</span></tt>: for example, if
+Clang is instructed to compile code for Mac OS X 10.5, a call to <tt class="docutils literal"><span class="pre">f()</span></tt>
+succeeds.  If Clang is instructed to compile code for Mac OS X 10.6, the call
+succeeds but Clang emits a warning specifying that the function is deprecated.
+Finally, if Clang is instructed to compile code for Mac OS X 10.7, the call
+fails because <tt class="docutils literal"><span class="pre">f()</span></tt> is no longer available.</p>
+<p>The availability attribute is a comma-separated list starting with the
+platform name and then including clauses specifying important milestones in the
+declaration’s lifetime (in any order) along with additional information.  Those
+clauses can be:</p>
+<dl class="docutils">
+<dt>introduced=<em>version</em></dt>
+<dd>The first version in which this declaration was introduced.</dd>
+<dt>deprecated=<em>version</em></dt>
+<dd>The first version in which this declaration was deprecated, meaning that
+users should migrate away from this API.</dd>
+<dt>obsoleted=<em>version</em></dt>
+<dd>The first version in which this declaration was obsoleted, meaning that it
+was removed completely and can no longer be used.</dd>
+<dt>unavailable</dt>
+<dd>This declaration is never available on this platform.</dd>
+<dt>message=<em>string-literal</em></dt>
+<dd>Additional message text that Clang will provide when emitting a warning or
+error about use of a deprecated or obsoleted declaration.  Useful to direct
+users to replacement APIs.</dd>
+</dl>
+<p>Multiple availability attributes can be placed on a declaration, which may
+correspond to different platforms.  Only the availability attribute with the
+platform corresponding to the target platform will be used; any others will be
+ignored.  If no availability attribute specifies availability for the current
+target platform, the availability attributes are ignored.  Supported platforms
+are:</p>
+<dl class="docutils">
+<dt><tt class="docutils literal"><span class="pre">ios</span></tt></dt>
+<dd>Apple’s iOS operating system.  The minimum deployment target is specified by
+the <tt class="docutils literal"><span class="pre">-mios-version-min=*version*</span></tt> or <tt class="docutils literal"><span class="pre">-miphoneos-version-min=*version*</span></tt>
+command-line arguments.</dd>
+<dt><tt class="docutils literal"><span class="pre">macosx</span></tt></dt>
+<dd>Apple’s Mac OS X operating system.  The minimum deployment target is
+specified by the <tt class="docutils literal"><span class="pre">-mmacosx-version-min=*version*</span></tt> command-line argument.</dd>
+</dl>
+<p>A declaration can be used even when deploying back to a platform version prior
+to when the declaration was introduced.  When this happens, the declaration is
+<a class="reference external" href="https://developer.apple.com/library/mac/#documentation/MacOSX/Conceptual/BPFrameworks/Concepts/WeakLinking.html">weakly linked</a>,
+as if the <tt class="docutils literal"><span class="pre">weak_import</span></tt> attribute were added to the declaration.  A
+weakly-linked declaration may or may not be present a run-time, and a program
+can determine whether the declaration is present by checking whether the
+address of that declaration is non-NULL.</p>
+<p>If there are multiple declarations of the same entity, the availability
+attributes must either match on a per-platform basis or later
+declarations must not have availability attributes for that
+platform. For example:</p>
+<div class="highlight-c"><div class="highlight"><pre><span class="kt">void</span> <span class="nf">g</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="n">__attribute__</span><span class="p">((</span><span class="n">availability</span><span class="p">(</span><span class="n">macosx</span><span class="p">,</span><span class="n">introduced</span><span class="o">=</span><span class="mf">10.4</span><span class="p">)));</span>
+<span class="kt">void</span> <span class="nf">g</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="n">__attribute__</span><span class="p">((</span><span class="n">availability</span><span class="p">(</span><span class="n">macosx</span><span class="p">,</span><span class="n">introduced</span><span class="o">=</span><span class="mf">10.4</span><span class="p">)));</span> <span class="c1">// okay, matches</span>
+<span class="kt">void</span> <span class="nf">g</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="n">__attribute__</span><span class="p">((</span><span class="n">availability</span><span class="p">(</span><span class="n">ios</span><span class="p">,</span><span class="n">introduced</span><span class="o">=</span><span class="mf">4.0</span><span class="p">)));</span> <span class="c1">// okay, adds a new platform</span>
+<span class="kt">void</span> <span class="nf">g</span><span class="p">(</span><span class="kt">void</span><span class="p">);</span> <span class="c1">// okay, inherits both macosx and ios availability from above.</span>
+<span class="kt">void</span> <span class="nf">g</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="n">__attribute__</span><span class="p">((</span><span class="n">availability</span><span class="p">(</span><span class="n">macosx</span><span class="p">,</span><span class="n">introduced</span><span class="o">=</span><span class="mf">10.5</span><span class="p">)));</span> <span class="c1">// error: mismatch</span>
+</pre></div>
+</div>
+<p>When one method overrides another, the overriding method can be more widely available than the overridden method, e.g.,:</p>
+<div class="highlight-objc"><div class="highlight"><pre><span class="k">@interface</span> <span class="nc">A</span>
+<span class="k">-</span> <span class="p">(</span><span class="kt">id</span><span class="p">)</span><span class="nf">method</span> <span class="n">__attribute__</span><span class="p">((</span><span class="n">availability</span><span class="p">(</span><span class="n">macosx</span><span class="p">,</span><span class="n">introduced</span><span class="o">=</span><span class="mf">10.4</span><span class="p">)));</span>
+<span class="k">-</span> <span class="p">(</span><span class="kt">id</span><span class="p">)</span><span class="nf">method2</span> <span class="n">__attribute__</span><span class="p">((</span><span class="n">availability</span><span class="p">(</span><span class="n">macosx</span><span class="p">,</span><span class="n">introduced</span><span class="o">=</span><span class="mf">10.4</span><span class="p">)));</span>
+<span class="k">@end</span>
+
+<span class="k">@interface</span> <span class="nc">B</span> : <span class="nc">A</span>
+<span class="k">-</span> <span class="p">(</span><span class="kt">id</span><span class="p">)</span><span class="nf">method</span> <span class="n">__attribute__</span><span class="p">((</span><span class="n">availability</span><span class="p">(</span><span class="n">macosx</span><span class="p">,</span><span class="n">introduced</span><span class="o">=</span><span class="mf">10.3</span><span class="p">)));</span> <span class="c1">// okay: method moved into base class later</span>
+<span class="k">-</span> <span class="p">(</span><span class="kt">id</span><span class="p">)</span><span class="nf">method</span> <span class="n">__attribute__</span><span class="p">((</span><span class="n">availability</span><span class="p">(</span><span class="n">macosx</span><span class="p">,</span><span class="n">introduced</span><span class="o">=</span><span class="mf">10.5</span><span class="p">)));</span> <span class="c1">// error: this method was available via the base class in 10.4</span>
+<span class="k">@end</span>
+</pre></div>
+</div>
+</div>
+<div class="section" id="noreturn">
+<h3><a class="toc-backref" href="#id15">_Noreturn</a><a class="headerlink" href="#noreturn" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td> </td>
+<td> </td>
+<td> </td>
+<td>X</td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>A function declared as <tt class="docutils literal"><span class="pre">_Noreturn</span></tt> shall not return to its caller. The
+compiler will generate a diagnostic for a function declared as <tt class="docutils literal"><span class="pre">_Noreturn</span></tt>
+that appears to be capable of returning to its caller.</p>
+</div>
+<div class="section" id="id1">
+<h3><a class="toc-backref" href="#id16">noreturn</a><a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td> </td>
+<td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>A function declared as <tt class="docutils literal"><span class="pre">[[noreturn]]</span></tt> shall not return to its caller. The
+compiler will generate a diagnostic for a function declared as <tt class="docutils literal"><span class="pre">[[noreturn]]</span></tt>
+that appears to be capable of returning to its caller.</p>
+</div>
+<div class="section" id="carries-dependency">
+<h3><a class="toc-backref" href="#id17">carries_dependency</a><a class="headerlink" href="#carries-dependency" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>The <tt class="docutils literal"><span class="pre">carries_dependency</span></tt> attribute specifies dependency propagation into and
+out of functions.</p>
+<p>When specified on a function or Objective-C method, the <tt class="docutils literal"><span class="pre">carries_dependency</span></tt>
+attribute means that the return value carries a dependency out of the function,
+so that the implementation need not constrain ordering upon return from that
+function. Implementations of the function and its caller may choose to preserve
+dependencies instead of emitting memory ordering instructions such as fences.</p>
+<p>Note, this attribute does not change the meaning of the program, but may result
+in generation of more efficient code.</p>
+</div>
+<div class="section" id="enable-if">
+<h3><a class="toc-backref" href="#id18">enable_if</a><a class="headerlink" href="#enable-if" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<div class="admonition note">
+<p class="first admonition-title">Note</p>
+<p class="last">Some features of this attribute are experimental. The meaning of
+multiple enable_if attributes on a single declaration is subject to change in
+a future version of clang. Also, the ABI is not standardized and the name
+mangling may change in future versions. To avoid that, use asm labels.</p>
+</div>
+<p>The <tt class="docutils literal"><span class="pre">enable_if</span></tt> attribute can be placed on function declarations to control
+which overload is selected based on the values of the function’s arguments.
+When combined with the <tt class="docutils literal"><span class="pre">overloadable</span></tt> attribute, this feature is also
+available in C.</p>
+<div class="highlight-c++"><div class="highlight"><pre><span class="kt">int</span> <span class="nf">isdigit</span><span class="p">(</span><span class="kt">int</span> <span class="n">c</span><span class="p">);</span>
+<span class="kt">int</span> <span class="nf">isdigit</span><span class="p">(</span><span class="kt">int</span> <span class="n">c</span><span class="p">)</span> <span class="n">__attribute__</span><span class="p">((</span><span class="n">enable_if</span><span class="p">(</span><span class="n">c</span> <span class="o"><=</span> <span class="o">-</span><span class="mi">1</span> <span class="o">||</span> <span class="n">c</span> <span class="o">></span> <span class="mi">255</span><span class="p">,</span> <span class="s">"chosen when 'c' is out of range"</span><span class="p">)))</span> <span class="n">__attribute__</span><span class="p">((</span><span class="n">unavailable</span><span class="p">(</span><span class="s">"'c' must have the value of an unsigned char or EOF"</span><span class="p">)));</span>
+
+<span class="kt">void</span> <span class="nf">foo</span><span class="p">(</span><span class="kt">char</span> <span class="n">c</span><span class="p">)</span> <span class="p">{</span>
+  <span class="n">isdigit</span><span class="p">(</span><span class="n">c</span><span class="p">);</span>
+  <span class="n">isdigit</span><span class="p">(</span><span class="mi">10</span><span class="p">);</span>
+  <span class="n">isdigit</span><span class="p">(</span><span class="o">-</span><span class="mi">10</span><span class="p">);</span>  <span class="c1">// results in a compile-time error.</span>
+<span class="p">}</span>
+</pre></div>
+</div>
+<p>The enable_if attribute takes two arguments, the first is an expression written
+in terms of the function parameters, the second is a string explaining why this
+overload candidate could not be selected to be displayed in diagnostics. The
+expression is part of the function signature for the purposes of determining
+whether it is a redeclaration (following the rules used when determining
+whether a C++ template specialization is ODR-equivalent), but is not part of
+the type.</p>
+<p>The enable_if expression is evaluated as if it were the body of a
+bool-returning constexpr function declared with the arguments of the function
+it is being applied to, then called with the parameters at the call site. If the
+result is false or could not be determined through constant expression
+evaluation, then this overload will not be chosen and the provided string may
+be used in a diagnostic if the compile fails as a result.</p>
+<p>Because the enable_if expression is an unevaluated context, there are no global
+state changes, nor the ability to pass information from the enable_if
+expression to the function body. For example, suppose we want calls to
+strnlen(strbuf, maxlen) to resolve to strnlen_chk(strbuf, maxlen, size of
+strbuf) only if the size of strbuf can be determined:</p>
+<div class="highlight-c++"><div class="highlight"><pre><span class="n">__attribute__</span><span class="p">((</span><span class="n">always_inline</span><span class="p">))</span>
+<span class="k">static</span> <span class="kr">inline</span> <span class="kt">size_t</span> <span class="n">strnlen</span><span class="p">(</span><span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">s</span><span class="p">,</span> <span class="kt">size_t</span> <span class="n">maxlen</span><span class="p">)</span>
+  <span class="n">__attribute__</span><span class="p">((</span><span class="n">overloadable</span><span class="p">))</span>
+  <span class="n">__attribute__</span><span class="p">((</span><span class="n">enable_if</span><span class="p">(</span><span class="n">__builtin_object_size</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="p">))),</span>
+                           <span class="s">"chosen when the buffer size is known but 'maxlen' is not"</span><span class="p">)))</span>
+<span class="p">{</span>
+  <span class="k">return</span> <span class="n">strnlen_chk</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">maxlen</span><span class="p">,</span> <span class="n">__builtin_object_size</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="mi">0</span><span class="p">));</span>
+<span class="p">}</span>
+</pre></div>
+</div>
+<p>Multiple enable_if attributes may be applied to a single declaration. In this
+case, the enable_if expressions are evaluated from left to right in the
+following manner. First, the candidates whose enable_if expressions evaluate to
+false or cannot be evaluated are discarded. If the remaining candidates do not
+share ODR-equivalent enable_if expressions, the overload resolution is
+ambiguous. Otherwise, enable_if overload resolution continues with the next
+enable_if attribute on the candidates that have not been discarded and have
+remaining enable_if attributes. In this way, we pick the most specific
+overload out of a number of viable overloads using enable_if.</p>
+<div class="highlight-c++"><div class="highlight"><pre><span class="kt">void</span> <span class="nf">f</span><span class="p">()</span> <span class="n">__attribute__</span><span class="p">((</span><span class="n">enable_if</span><span class="p">(</span><span class="nb">true</span><span class="p">,</span> <span class="s">""</span><span class="p">)));</span>  <span class="c1">// #1</span>
+<span class="kt">void</span> <span class="nf">f</span><span class="p">()</span> <span class="n">__attribute__</span><span class="p">((</span><span class="n">enable_if</span><span class="p">(</span><span class="nb">true</span><span class="p">,</span> <span class="s">""</span><span class="p">)))</span> <span class="n">__attribute__</span><span class="p">((</span><span class="n">enable_if</span><span class="p">(</span><span class="nb">true</span><span class="p">,</span> <span class="s">""</span><span class="p">)));</span>  <span class="c1">// #2</span>
+
+<span class="kt">void</span> <span class="nf">g</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="p">,</span> <span class="kt">int</span> <span class="n">j</span><span class="p">)</span> <span class="n">__attribute__</span><span class="p">((</span><span class="n">enable_if</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="s">""</span><span class="p">)));</span>  <span class="c1">// #1</span>
+<span class="kt">void</span> <span class="nf">g</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="p">,</span> <span class="kt">int</span> <span class="n">j</span><span class="p">)</span> <span class="n">__attribute__</span><span class="p">((</span><span class="n">enable_if</span><span class="p">(</span><span class="n">j</span><span class="p">,</span> <span class="s">""</span><span class="p">)))</span> <span class="n">__attribute__</span><span class="p">((</span><span class="n">enable_if</span><span class="p">(</span><span class="nb">true</span><span class="p">)));</span>  <span class="c1">// #2</span>
+</pre></div>
+</div>
+<p>In this example, a call to f() is always resolved to #2, as the first enable_if
+expression is ODR-equivalent for both declarations, but #1 does not have another
+enable_if expression to continue evaluating, so the next round of evaluation has
+only a single candidate. In a call to g(1, 1), the call is ambiguous even though
+#2 has more enable_if attributes, because the first enable_if expressions are
+not ODR-equivalent.</p>
+<p>Query for this feature with <tt class="docutils literal"><span class="pre">__has_attribute(enable_if)</span></tt>.</p>
+</div>
+<div class="section" id="flatten-gnu-flatten">
+<h3><a class="toc-backref" href="#id19">flatten (gnu::flatten)</a><a class="headerlink" href="#flatten-gnu-flatten" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>The <tt class="docutils literal"><span class="pre">flatten</span></tt> attribute causes calls within the attributed function to
+be inlined unless it is impossible to do so, for example if the body of the
+callee is unavailable or if the callee has the <tt class="docutils literal"><span class="pre">noinline</span></tt> attribute.</p>
+</div>
+<div class="section" id="format-gnu-format">
+<h3><a class="toc-backref" href="#id20">format (gnu::format)</a><a class="headerlink" href="#format-gnu-format" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>Clang supports the <tt class="docutils literal"><span class="pre">format</span></tt> attribute, which indicates that the function
+accepts a <tt class="docutils literal"><span class="pre">printf</span></tt> or <tt class="docutils literal"><span class="pre">scanf</span></tt>-like format string and corresponding
+arguments or a <tt class="docutils literal"><span class="pre">va_list</span></tt> that contains these arguments.</p>
+<p>Please see <a class="reference external" href="http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html">GCC documentation about format attribute</a> to find details
+about attribute syntax.</p>
+<p>Clang implements two kinds of checks with this attribute.</p>
+<ol class="arabic">
+<li><p class="first">Clang checks that the function with the <tt class="docutils literal"><span class="pre">format</span></tt> attribute is called with
+a format string that uses format specifiers that are allowed, and that
+arguments match the format string.  This is the <tt class="docutils literal"><span class="pre">-Wformat</span></tt> warning, it is
+on by default.</p>
+</li>
+<li><p class="first">Clang checks that the format string argument is a literal string.  This is
+the <tt class="docutils literal"><span class="pre">-Wformat-nonliteral</span></tt> warning, it is off by default.</p>
+<p>Clang implements this mostly the same way as GCC, but there is a difference
+for functions that accept a <tt class="docutils literal"><span class="pre">va_list</span></tt> argument (for example, <tt class="docutils literal"><span class="pre">vprintf</span></tt>).
+GCC does not emit <tt class="docutils literal"><span class="pre">-Wformat-nonliteral</span></tt> warning for calls to such
+functions.  Clang does not warn if the format string comes from a function
+parameter, where the function is annotated with a compatible attribute,
+otherwise it warns.  For example:</p>
+<div class="highlight-c"><div class="highlight"><pre><span class="n">__attribute__</span><span class="p">((</span><span class="n">__format__</span> <span class="p">(</span><span class="n">__scanf__</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">)))</span>
+<span class="kt">void</span> <span class="n">foo</span><span class="p">(</span><span class="k">const</span> <span class="kt">char</span><span class="o">*</span> <span class="n">s</span><span class="p">,</span> <span class="kt">char</span> <span class="o">*</span><span class="n">buf</span><span class="p">,</span> <span class="p">...)</span> <span class="p">{</span>
+  <span class="kt">va_list</span> <span class="n">ap</span><span class="p">;</span>
+  <span class="n">va_start</span><span class="p">(</span><span class="n">ap</span><span class="p">,</span> <span class="n">buf</span><span class="p">);</span>
+
+  <span class="n">vprintf</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">ap</span><span class="p">);</span> <span class="c1">// warning: format string is not a string literal</span>
+<span class="p">}</span>
+</pre></div>
+</div>
+<p>In this case we warn because <tt class="docutils literal"><span class="pre">s</span></tt> contains a format string for a
+<tt class="docutils literal"><span class="pre">scanf</span></tt>-like function, but it is passed to a <tt class="docutils literal"><span class="pre">printf</span></tt>-like function.</p>
+<p>If the attribute is removed, clang still warns, because the format string is
+not a string literal.</p>
+<p>Another example:</p>
+<div class="highlight-c"><div class="highlight"><pre><span class="n">__attribute__</span><span class="p">((</span><span class="n">__format__</span> <span class="p">(</span><span class="n">__printf__</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">)))</span>
+<span class="kt">void</span> <span class="n">foo</span><span class="p">(</span><span class="k">const</span> <span class="kt">char</span><span class="o">*</span> <span class="n">s</span><span class="p">,</span> <span class="kt">char</span> <span class="o">*</span><span class="n">buf</span><span class="p">,</span> <span class="p">...)</span> <span class="p">{</span>
+  <span class="kt">va_list</span> <span class="n">ap</span><span class="p">;</span>
+  <span class="n">va_start</span><span class="p">(</span><span class="n">ap</span><span class="p">,</span> <span class="n">buf</span><span class="p">);</span>
+
+  <span class="n">vprintf</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">ap</span><span class="p">);</span> <span class="c1">// warning</span>
+<span class="p">}</span>
+</pre></div>
+</div>
+<p>In this case Clang does not warn because the format string <tt class="docutils literal"><span class="pre">s</span></tt> and
+the corresponding arguments are annotated.  If the arguments are
+incorrect, the caller of <tt class="docutils literal"><span class="pre">foo</span></tt> will receive a warning.</p>
+</li>
+</ol>
+</div>
+<div class="section" id="noduplicate-clang-noduplicate">
+<h3><a class="toc-backref" href="#id21">noduplicate (clang::noduplicate)</a><a class="headerlink" href="#noduplicate-clang-noduplicate" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>The <tt class="docutils literal"><span class="pre">noduplicate</span></tt> attribute can be placed on function declarations to control
+whether function calls to this function can be duplicated or not as a result of
+optimizations. This is required for the implementation of functions with
+certain special requirements, like the OpenCL “barrier” function, that might
+need to be run concurrently by all the threads that are executing in lockstep
+on the hardware. For example this attribute applied on the function
+“nodupfunc” in the code below avoids that:</p>
+<div class="highlight-c"><div class="highlight"><pre><span class="kt">void</span> <span class="nf">nodupfunc</span><span class="p">()</span> <span class="n">__attribute__</span><span class="p">((</span><span class="n">noduplicate</span><span class="p">));</span>
+<span class="c1">// Setting it as a C++11 attribute is also valid</span>
+<span class="c1">// void nodupfunc() [[clang::noduplicate]];</span>
+<span class="kt">void</span> <span class="nf">foo</span><span class="p">();</span>
+<span class="kt">void</span> <span class="nf">bar</span><span class="p">();</span>
+
+<span class="n">nodupfunc</span><span class="p">();</span>
+<span class="k">if</span> <span class="p">(</span><span class="n">a</span> <span class="o">></span> <span class="n">n</span><span class="p">)</span> <span class="p">{</span>
+  <span class="n">foo</span><span class="p">();</span>
+<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
+  <span class="n">bar</span><span class="p">();</span>
+<span class="p">}</span>
+</pre></div>
+</div>
+<p>gets possibly modified by some optimizations into code similar to this:</p>
+<div class="highlight-c"><div class="highlight"><pre><span class="k">if</span> <span class="p">(</span><span class="n">a</span> <span class="o">></span> <span class="n">n</span><span class="p">)</span> <span class="p">{</span>
+  <span class="n">nodupfunc</span><span class="p">();</span>
+  <span class="n">foo</span><span class="p">();</span>
+<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
+  <span class="n">nodupfunc</span><span class="p">();</span>
+  <span class="n">bar</span><span class="p">();</span>
+<span class="p">}</span>
+</pre></div>
+</div>
+<p>where the call to “nodupfunc” is duplicated and sunk into the two branches
+of the condition.</p>
+</div>
+<div class="section" id="no-sanitize-clang-no-sanitize">
+<h3><a class="toc-backref" href="#id22">no_sanitize (clang::no_sanitize)</a><a class="headerlink" href="#no-sanitize-clang-no-sanitize" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>Use the <tt class="docutils literal"><span class="pre">no_sanitize</span></tt> attribute on a function declaration to specify
+that a particular instrumentation or set of instrumentations should not be
+applied to that function. The attribute takes a list of string literals,
+which have the same meaning as values accepted by the <tt class="docutils literal"><span class="pre">-fno-sanitize=</span></tt>
+flag. For example, <tt class="docutils literal"><span class="pre">__attribute__((no_sanitize("address",</span> <span class="pre">"thread")))</span></tt>
+specifies that AddressSanitizer and ThreadSanitizer should not be applied
+to the function.</p>
+<p>See <a class="reference internal" href="UsersManual.html#controlling-code-generation"><em>Controlling Code Generation</em></a> for a
+full list of supported sanitizer flags.</p>
+</div>
+<div class="section" id="no-sanitize-address-no-address-safety-analysis-gnu-no-address-safety-analysis-gnu-no-sanitize-address">
+<h3><a class="toc-backref" href="#id23">no_sanitize_address (no_address_safety_analysis, gnu::no_address_safety_analysis, gnu::no_sanitize_address)</a><a class="headerlink" href="#no-sanitize-address-no-address-safety-analysis-gnu-no-address-safety-analysis-gnu-no-sanitize-address" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p id="langext-address-sanitizer">Use <tt class="docutils literal"><span class="pre">__attribute__((no_sanitize_address))</span></tt> on a function declaration to
+specify that address safety instrumentation (e.g. AddressSanitizer) should
+not be applied to that function.</p>
+</div>
+<div class="section" id="no-sanitize-thread">
+<h3><a class="toc-backref" href="#id24">no_sanitize_thread</a><a class="headerlink" href="#no-sanitize-thread" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p id="langext-thread-sanitizer">Use <tt class="docutils literal"><span class="pre">__attribute__((no_sanitize_thread))</span></tt> on a function declaration to
+specify that checks for data races on plain (non-atomic) memory accesses should
+not be inserted by ThreadSanitizer. The function is still instrumented by the
+tool to avoid false positives and provide meaningful stack traces.</p>
+</div>
+<div class="section" id="no-sanitize-memory">
+<h3><a class="toc-backref" href="#id25">no_sanitize_memory</a><a class="headerlink" href="#no-sanitize-memory" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p id="langext-memory-sanitizer">Use <tt class="docutils literal"><span class="pre">__attribute__((no_sanitize_memory))</span></tt> on a function declaration to
+specify that checks for uninitialized memory should not be inserted
+(e.g. by MemorySanitizer). The function may still be instrumented by the tool
+to avoid false positives in other places.</p>
+</div>
+<div class="section" id="no-split-stack-gnu-no-split-stack">
+<h3><a class="toc-backref" href="#id26">no_split_stack (gnu::no_split_stack)</a><a class="headerlink" href="#no-split-stack-gnu-no-split-stack" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>The <tt class="docutils literal"><span class="pre">no_split_stack</span></tt> attribute disables the emission of the split stack
+preamble for a particular function. It has no effect if <tt class="docutils literal"><span class="pre">-fsplit-stack</span></tt>
+is not specified.</p>
+</div>
+<div class="section" id="objc-boxable">
+<h3><a class="toc-backref" href="#id27">objc_boxable</a><a class="headerlink" href="#objc-boxable" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>Structs and unions marked with the <tt class="docutils literal"><span class="pre">objc_boxable</span></tt> attribute can be used
+with the Objective-C boxed expression syntax, <tt class="docutils literal"><span class="pre">@(...)</span></tt>.</p>
+<p><strong>Usage</strong>: <tt class="docutils literal"><span class="pre">__attribute__((objc_boxable))</span></tt>. This attribute
+can only be placed on a declaration of a trivially-copyable struct or union:</p>
+<div class="highlight-objc"><div class="highlight"><pre>struct __attribute__((objc_boxable)) some_struct {
+  int i;
+};
+union __attribute__((objc_boxable)) some_union {
+  int i;
+  float f;
+};
+typedef struct __attribute__((objc_boxable)) _some_struct some_struct;
+
+// ...
+
+some_struct ss;
+NSValue *boxed = @(ss);
+</pre></div>
+</div>
+</div>
+<div class="section" id="objc-method-family">
+<h3><a class="toc-backref" href="#id28">objc_method_family</a><a class="headerlink" href="#objc-method-family" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>Many methods in Objective-C have conventional meanings determined by their
+selectors. It is sometimes useful to be able to mark a method as having a
+particular conventional meaning despite not having the right selector, or as
+not having the conventional meaning that its selector would suggest. For these
+use cases, we provide an attribute to specifically describe the “method family”
+that a method belongs to.</p>
+<p><strong>Usage</strong>: <tt class="docutils literal"><span class="pre">__attribute__((objc_method_family(X)))</span></tt>, where <tt class="docutils literal"><span class="pre">X</span></tt> is one of
+<tt class="docutils literal"><span class="pre">none</span></tt>, <tt class="docutils literal"><span class="pre">alloc</span></tt>, <tt class="docutils literal"><span class="pre">copy</span></tt>, <tt class="docutils literal"><span class="pre">init</span></tt>, <tt class="docutils literal"><span class="pre">mutableCopy</span></tt>, or <tt class="docutils literal"><span class="pre">new</span></tt>.  This
+attribute can only be placed at the end of a method declaration:</p>
+<div class="highlight-objc"><div class="highlight"><pre><span class="k">-</span> <span class="p">(</span><span class="n">NSString</span> <span class="o">*</span><span class="p">)</span><span class="nf">initMyStringValue</span> <span class="n">__attribute__</span><span class="p">((</span><span class="n">objc_method_family</span><span class="p">(</span><span class="n">none</span><span class="p">)));</span>
+</pre></div>
+</div>
+<p>Users who do not wish to change the conventional meaning of a method, and who
+merely want to document its non-standard retain and release semantics, should
+use the retaining behavior attributes (<tt class="docutils literal"><span class="pre">ns_returns_retained</span></tt>,
+<tt class="docutils literal"><span class="pre">ns_returns_not_retained</span></tt>, etc).</p>
+<p>Query for this feature with <tt class="docutils literal"><span class="pre">__has_attribute(objc_method_family)</span></tt>.</p>
+</div>
+<div class="section" id="objc-requires-super">
+<h3><a class="toc-backref" href="#id29">objc_requires_super</a><a class="headerlink" href="#objc-requires-super" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>Some Objective-C classes allow a subclass to override a particular method in a
+parent class but expect that the overriding method also calls the overridden
+method in the parent class. For these cases, we provide an attribute to
+designate that a method requires a “call to <tt class="docutils literal"><span class="pre">super</span></tt>” in the overriding
+method in the subclass.</p>
+<p><strong>Usage</strong>: <tt class="docutils literal"><span class="pre">__attribute__((objc_requires_super))</span></tt>.  This attribute can only
+be placed at the end of a method declaration:</p>
+<div class="highlight-objc"><div class="highlight"><pre><span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">foo</span> <span class="n">__attribute__</span><span class="p">((</span><span class="n">objc_requires_super</span><span class="p">));</span>
+</pre></div>
+</div>
+<p>This attribute can only be applied the method declarations within a class, and
+not a protocol.  Currently this attribute does not enforce any placement of
+where the call occurs in the overriding method (such as in the case of
+<tt class="docutils literal"><span class="pre">-dealloc</span></tt> where the call must appear at the end).  It checks only that it
+exists.</p>
+<p>Note that on both OS X and iOS that the Foundation framework provides a
+convenience macro <tt class="docutils literal"><span class="pre">NS_REQUIRES_SUPER</span></tt> that provides syntactic sugar for this
+attribute:</p>
+<div class="highlight-objc"><div class="highlight"><pre><span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">foo</span> <span class="n">NS_REQUIRES_SUPER</span><span class="p">;</span>
+</pre></div>
+</div>
+<p>This macro is conditionally defined depending on the compiler’s support for
+this attribute.  If the compiler does not support the attribute the macro
+expands to nothing.</p>
+<p>Operationally, when a method has this annotation the compiler will warn if the
+implementation of an override in a subclass does not call super.  For example:</p>
+<div class="highlight-objc"><div class="highlight"><pre><span class="nl">warning:</span> <span class="n">method</span> <span class="n">possibly</span> <span class="n">missing</span> <span class="n">a</span> <span class="p">[</span><span class="n">super</span> <span class="n">AnnotMeth</span><span class="p">]</span> <span class="n">call</span>
+<span class="o">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="n">AnnotMeth</span><span class="p">{};</span>
+                   <span class="o">^</span>
+</pre></div>
+</div>
+</div>
+<div class="section" id="objc-runtime-name">
+<h3><a class="toc-backref" href="#id30">objc_runtime_name</a><a class="headerlink" href="#objc-runtime-name" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>By default, the Objective-C interface or protocol identifier is used
+in the metadata name for that object. The <cite>objc_runtime_name</cite>
+attribute allows annotated interfaces or protocols to use the
+specified string argument in the object’s metadata name instead of the
+default name.</p>
+<p><strong>Usage</strong>: <tt class="docutils literal"><span class="pre">__attribute__((objc_runtime_name("MyLocalName")))</span></tt>.  This attribute
+can only be placed before an @protocol or @interface declaration:</p>
+<div class="highlight-objc"><div class="highlight"><pre><span class="n">__attribute__</span><span class="p">((</span><span class="n">objc_runtime_name</span><span class="p">(</span><span class="s">"MyLocalName"</span><span class="p">)))</span>
+<span class="k">@interface</span> <span class="nc">Message</span>
+<span class="k">@end</span>
+</pre></div>
+</div>
+</div>
+<div class="section" id="optnone-clang-optnone">
+<h3><a class="toc-backref" href="#id31">optnone (clang::optnone)</a><a class="headerlink" href="#optnone-clang-optnone" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>The <tt class="docutils literal"><span class="pre">optnone</span></tt> attribute suppresses essentially all optimizations
+on a function or method, regardless of the optimization level applied to
+the compilation unit as a whole.  This is particularly useful when you
+need to debug a particular function, but it is infeasible to build the
+entire application without optimization.  Avoiding optimization on the
+specified function can improve the quality of the debugging information
+for that function.</p>
+<p>This attribute is incompatible with the <tt class="docutils literal"><span class="pre">always_inline</span></tt> and <tt class="docutils literal"><span class="pre">minsize</span></tt>
+attributes.</p>
+</div>
+<div class="section" id="overloadable">
+<h3><a class="toc-backref" href="#id32">overloadable</a><a class="headerlink" href="#overloadable" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>Clang provides support for C++ function overloading in C.  Function overloading
+in C is introduced using the <tt class="docutils literal"><span class="pre">overloadable</span></tt> attribute.  For example, one
+might provide several overloaded versions of a <tt class="docutils literal"><span class="pre">tgsin</span></tt> function that invokes
+the appropriate standard function computing the sine of a value with <tt class="docutils literal"><span class="pre">float</span></tt>,
+<tt class="docutils literal"><span class="pre">double</span></tt>, or <tt class="docutils literal"><span class="pre">long</span> <span class="pre">double</span></tt> precision:</p>
+<div class="highlight-c"><div class="highlight"><pre><span class="cp">#include <math.h></span>
+<span class="kt">float</span> <span class="nf">__attribute__</span><span class="p">((</span><span class="n">overloadable</span><span class="p">))</span> <span class="n">tgsin</span><span class="p">(</span><span class="kt">float</span> <span class="n">x</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">sinf</span><span class="p">(</span><span class="n">x</span><span class="p">);</span> <span class="p">}</span>
+<span class="kt">double</span> <span class="nf">__attribute__</span><span class="p">((</span><span class="n">overloadable</span><span class="p">))</span> <span class="n">tgsin</span><span class="p">(</span><span class="kt">double</span> <span class="n">x</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">);</span> <span class="p">}</span>
+<span class="kt">long</span> <span class="kt">double</span> <span class="nf">__attribute__</span><span class="p">((</span><span class="n">overloadable</span><span class="p">))</span> <span class="n">tgsin</span><span class="p">(</span><span class="kt">long</span> <span class="kt">double</span> <span class="n">x</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">sinl</span><span class="p">(</span><span class="n">x</span><span class="p">);</span> <span class="p">}</span>
+</pre></div>
+</div>
+<p>Given these declarations, one can call <tt class="docutils literal"><span class="pre">tgsin</span></tt> with a <tt class="docutils literal"><span class="pre">float</span></tt> value to
+receive a <tt class="docutils literal"><span class="pre">float</span></tt> result, with a <tt class="docutils literal"><span class="pre">double</span></tt> to receive a <tt class="docutils literal"><span class="pre">double</span></tt> result,
+etc.  Function overloading in C follows the rules of C++ function overloading
+to pick the best overload given the call arguments, with a few C-specific
+semantics:</p>
+<ul class="simple">
+<li>Conversion from <tt class="docutils literal"><span class="pre">float</span></tt> or <tt class="docutils literal"><span class="pre">double</span></tt> to <tt class="docutils literal"><span class="pre">long</span> <span class="pre">double</span></tt> is ranked as a
+floating-point promotion (per C99) rather than as a floating-point conversion
+(as in C++).</li>
+<li>A conversion from a pointer of type <tt class="docutils literal"><span class="pre">T*</span></tt> to a pointer of type <tt class="docutils literal"><span class="pre">U*</span></tt> is
+considered a pointer conversion (with conversion rank) if <tt class="docutils literal"><span class="pre">T</span></tt> and <tt class="docutils literal"><span class="pre">U</span></tt> are
+compatible types.</li>
+<li>A conversion from type <tt class="docutils literal"><span class="pre">T</span></tt> to a value of type <tt class="docutils literal"><span class="pre">U</span></tt> is permitted if <tt class="docutils literal"><span class="pre">T</span></tt>
+and <tt class="docutils literal"><span class="pre">U</span></tt> are compatible types.  This conversion is given “conversion” rank.</li>
+</ul>
+<p>The declaration of <tt class="docutils literal"><span class="pre">overloadable</span></tt> functions is restricted to function
+declarations and definitions.  Most importantly, if any function with a given
+name is given the <tt class="docutils literal"><span class="pre">overloadable</span></tt> attribute, then all function declarations
+and definitions with that name (and in that scope) must have the
+<tt class="docutils literal"><span class="pre">overloadable</span></tt> attribute.  This rule even applies to redeclarations of
+functions whose original declaration had the <tt class="docutils literal"><span class="pre">overloadable</span></tt> attribute, e.g.,</p>
+<div class="highlight-c"><div class="highlight"><pre><span class="kt">int</span> <span class="nf">f</span><span class="p">(</span><span class="kt">int</span><span class="p">)</span> <span class="n">__attribute__</span><span class="p">((</span><span class="n">overloadable</span><span class="p">));</span>
+<span class="kt">float</span> <span class="nf">f</span><span class="p">(</span><span class="kt">float</span><span class="p">);</span> <span class="c1">// error: declaration of "f" must have the "overloadable" attribute</span>
+
+<span class="kt">int</span> <span class="nf">g</span><span class="p">(</span><span class="kt">int</span><span class="p">)</span> <span class="n">__attribute__</span><span class="p">((</span><span class="n">overloadable</span><span class="p">));</span>
+<span class="kt">int</span> <span class="nf">g</span><span class="p">(</span><span class="kt">int</span><span class="p">)</span> <span class="p">{</span> <span class="p">}</span> <span class="c1">// error: redeclaration of "g" must also have the "overloadable" attribute</span>
+</pre></div>
+</div>
+<p>Functions marked <tt class="docutils literal"><span class="pre">overloadable</span></tt> must have prototypes.  Therefore, the
+following code is ill-formed:</p>
+<div class="highlight-c"><div class="highlight"><pre><span class="kt">int</span> <span class="nf">h</span><span class="p">()</span> <span class="n">__attribute__</span><span class="p">((</span><span class="n">overloadable</span><span class="p">));</span> <span class="c1">// error: h does not have a prototype</span>
+</pre></div>
+</div>
+<p>However, <tt class="docutils literal"><span class="pre">overloadable</span></tt> functions are allowed to use a ellipsis even if there
+are no named parameters (as is permitted in C++).  This feature is particularly
+useful when combined with the <tt class="docutils literal"><span class="pre">unavailable</span></tt> attribute:</p>
+<div class="highlight-c++"><div class="highlight"><pre><span class="kt">void</span> <span class="nf">honeypot</span><span class="p">(...)</span> <span class="n">__attribute__</span><span class="p">((</span><span class="n">overloadable</span><span class="p">,</span> <span class="n">unavailable</span><span class="p">));</span> <span class="c1">// calling me is an error</span>
+</pre></div>
+</div>
+<p>Functions declared with the <tt class="docutils literal"><span class="pre">overloadable</span></tt> attribute have their names mangled
+according to the same rules as C++ function names.  For example, the three
+<tt class="docutils literal"><span class="pre">tgsin</span></tt> functions in our motivating example get the mangled names
+<tt class="docutils literal"><span class="pre">_Z5tgsinf</span></tt>, <tt class="docutils literal"><span class="pre">_Z5tgsind</span></tt>, and <tt class="docutils literal"><span class="pre">_Z5tgsine</span></tt>, respectively.  There are two
+caveats to this use of name mangling:</p>
+<ul class="simple">
+<li>Future versions of Clang may change the name mangling of functions overloaded
+in C, so you should not depend on an specific mangling.  To be completely
+safe, we strongly urge the use of <tt class="docutils literal"><span class="pre">static</span> <span class="pre">inline</span></tt> with <tt class="docutils literal"><span class="pre">overloadable</span></tt>
+functions.</li>
+<li>The <tt class="docutils literal"><span class="pre">overloadable</span></tt> attribute has almost no meaning when used in C++,
+because names will already be mangled and functions are already overloadable.
+However, when an <tt class="docutils literal"><span class="pre">overloadable</span></tt> function occurs within an <tt class="docutils literal"><span class="pre">extern</span> <span class="pre">"C"</span></tt>
+linkage specification, it’s name <em>will</em> be mangled in the same way as it
+would in C.</li>
+</ul>
+<p>Query for this feature with <tt class="docutils literal"><span class="pre">__has_extension(attribute_overloadable)</span></tt>.</p>
+</div>
+<div class="section" id="release-capability-release-shared-capability-clang-release-capability-clang-release-shared-capability">
+<h3><a class="toc-backref" href="#id33">release_capability (release_shared_capability, clang::release_capability, clang::release_shared_capability)</a><a class="headerlink" href="#release-capability-release-shared-capability-clang-release-capability-clang-release-shared-capability" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>Marks a function as releasing a capability.</p>
+</div>
+<div class="section" id="target-gnu-target">
+<h3><a class="toc-backref" href="#id34">target (gnu::target)</a><a class="headerlink" href="#target-gnu-target" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>Clang supports the GNU style <tt class="docutils literal"><span class="pre">__attribute__((target("OPTIONS")))</span></tt> attribute.
+This attribute may be attached to a function definition and instructs
+the backend to use different code generation options than were passed on the
+command line.</p>
+<p>The current set of options correspond to the existing “subtarget features” for
+the target with or without a “-mno-” in front corresponding to the absence
+of the feature, as well as <tt class="docutils literal"><span class="pre">arch="CPU"</span></tt> which will change the default “CPU”
+for the function.</p>
+<p>Example “subtarget features” from the x86 backend include: “mmx”, “sse”, “sse4.2”,
+“avx”, “xop” and largely correspond to the machine specific options handled by
+the front end.</p>
+</div>
+<div class="section" id="try-acquire-capability-try-acquire-shared-capability-clang-try-acquire-capability-clang-try-acquire-shared-capability">
+<h3><a class="toc-backref" href="#id35">try_acquire_capability (try_acquire_shared_capability, clang::try_acquire_capability, clang::try_acquire_shared_capability)</a><a class="headerlink" href="#try-acquire-capability-try-acquire-shared-capability-clang-try-acquire-capability-clang-try-acquire-shared-capability" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>Marks a function that attempts to acquire a capability. This function may fail to
+actually acquire the capability; they accept a Boolean value determining
+whether acquiring the capability means success (true), or failing to acquire
+the capability means success (false).</p>
+</div>
+</div>
+<div class="section" id="variable-attributes">
+<h2><a class="toc-backref" href="#id36">Variable Attributes</a><a class="headerlink" href="#variable-attributes" title="Permalink to this headline">¶</a></h2>
+<div class="section" id="init-seg">
+<h3><a class="toc-backref" href="#id37">init_seg</a><a class="headerlink" href="#init-seg" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td> </td>
+<td> </td>
+<td> </td>
+<td> </td>
+<td>X</td>
+</tr>
+</tbody>
+</table>
+<p>The attribute applied by <tt class="docutils literal"><span class="pre">pragma</span> <span class="pre">init_seg()</span></tt> controls the section into
+which global initialization function pointers are emitted.  It is only
+available with <tt class="docutils literal"><span class="pre">-fms-extensions</span></tt>.  Typically, this function pointer is
+emitted into <tt class="docutils literal"><span class="pre">.CRT$XCU</span></tt> on Windows.  The user can change the order of
+initialization by using a different section name with the same
+<tt class="docutils literal"><span class="pre">.CRT$XC</span></tt> prefix and a suffix that sorts lexicographically before or
+after the standard <tt class="docutils literal"><span class="pre">.CRT$XCU</span></tt> sections.  See the <a class="reference external" href="http://msdn.microsoft.com/en-us/library/7977wcck(v=vs.110).aspx">init_seg</a>
+documentation on MSDN for more information.</p>
+</div>
+<div class="section" id="section-gnu-section-declspec-allocate">
+<h3><a class="toc-backref" href="#id38">section (gnu::section, __declspec(allocate))</a><a class="headerlink" href="#section-gnu-section-declspec-allocate" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td>X</td>
+<td>X</td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>The <tt class="docutils literal"><span class="pre">section</span></tt> attribute allows you to specify a specific section a
+global variable or function should be in after translation.</p>
+</div>
+<div class="section" id="tls-model-gnu-tls-model">
+<h3><a class="toc-backref" href="#id39">tls_model (gnu::tls_model)</a><a class="headerlink" href="#tls-model-gnu-tls-model" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>The <tt class="docutils literal"><span class="pre">tls_model</span></tt> attribute allows you to specify which thread-local storage
+model to use. It accepts the following strings:</p>
+<ul class="simple">
+<li>global-dynamic</li>
+<li>local-dynamic</li>
+<li>initial-exec</li>
+<li>local-exec</li>
+</ul>
+<p>TLS models are mutually exclusive.</p>
+</div>
+<div class="section" id="thread">
+<h3><a class="toc-backref" href="#id40">thread</a><a class="headerlink" href="#thread" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td> </td>
+<td> </td>
+<td>X</td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>The <tt class="docutils literal"><span class="pre">__declspec(thread)</span></tt> attribute declares a variable with thread local
+storage.  It is available under the <tt class="docutils literal"><span class="pre">-fms-extensions</span></tt> flag for MSVC
+compatibility.  See the documentation for <a class="reference external" href="http://msdn.microsoft.com/en-us/library/9w1sdazb.aspx">__declspec(thread)</a> on MSDN.</p>
+<p>In Clang, <tt class="docutils literal"><span class="pre">__declspec(thread)</span></tt> is generally equivalent in functionality to the
+GNU <tt class="docutils literal"><span class="pre">__thread</span></tt> keyword.  The variable must not have a destructor and must have
+a constant initializer, if any.  The attribute only applies to variables
+declared with static storage duration, such as globals, class static data
+members, and static locals.</p>
+</div>
+</div>
+<div class="section" id="type-attributes">
+<h2><a class="toc-backref" href="#id41">Type Attributes</a><a class="headerlink" href="#type-attributes" title="Permalink to this headline">¶</a></h2>
+<div class="section" id="align-value">
+<h3><a class="toc-backref" href="#id42">align_value</a><a class="headerlink" href="#align-value" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>The align_value attribute can be added to the typedef of a pointer type or the
+declaration of a variable of pointer or reference type. It specifies that the
+pointer will point to, or the reference will bind to, only objects with at
+least the provided alignment. This alignment value must be some positive power
+of 2.</p>
+<blockquote>
+<div><div class="highlight-c"><div class="highlight"><pre><span class="k">typedef</span> <span class="kt">double</span> <span class="o">*</span> <span class="n">aligned_double_ptr</span> <span class="nf">__attribute__</span><span class="p">((</span><span class="n">align_value</span><span class="p">(</span><span class="mi">64</span><span class="p">)));</span>
+<span class="kt">void</span> <span class="nf">foo</span><span class="p">(</span><span class="kt">double</span> <span class="o">&</span> <span class="n">x</span>  <span class="n">__attribute__</span><span class="p">((</span><span class="n">align_value</span><span class="p">(</span><span class="mi">128</span><span class="p">)),</span>
+         <span class="n">aligned_double_ptr</span> <span class="n">y</span><span class="p">)</span> <span class="p">{</span> <span class="p">...</span> <span class="p">}</span>
+</pre></div>
+</div>
+</div></blockquote>
+<p>If the pointer value does not have the specified alignment at runtime, the
+behavior of the program is undefined.</p>
+</div>
+<div class="section" id="flag-enum">
+<h3><a class="toc-backref" href="#id43">flag_enum</a><a class="headerlink" href="#flag-enum" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>This attribute can be added to an enumerator to signal to the compiler that it
+is intended to be used as a flag type. This will cause the compiler to assume
+that the range of the type includes all of the values that you can get by
+manipulating bits of the enumerator when issuing warnings.</p>
+</div>
+<div class="section" id="single-inhertiance-multiple-inheritance-virtual-inheritance">
+<h3><a class="toc-backref" href="#id44">__single_inhertiance, __multiple_inheritance, __virtual_inheritance</a><a class="headerlink" href="#single-inhertiance-multiple-inheritance-virtual-inheritance" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td> </td>
+<td> </td>
+<td> </td>
+<td>X</td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>This collection of keywords is enabled under <tt class="docutils literal"><span class="pre">-fms-extensions</span></tt> and controls
+the pointer-to-member representation used on <tt class="docutils literal"><span class="pre">*-*-win32</span></tt> targets.</p>
+<p>The <tt class="docutils literal"><span class="pre">*-*-win32</span></tt> targets utilize a pointer-to-member representation which
+varies in size and alignment depending on the definition of the underlying
+class.</p>
+<p>However, this is problematic when a forward declaration is only available and
+no definition has been made yet.  In such cases, Clang is forced to utilize the
+most general representation that is available to it.</p>
+<p>These keywords make it possible to use a pointer-to-member representation other
+than the most general one regardless of whether or not the definition will ever
+be present in the current translation unit.</p>
+<p>This family of keywords belong between the <tt class="docutils literal"><span class="pre">class-key</span></tt> and <tt class="docutils literal"><span class="pre">class-name</span></tt>:</p>
+<div class="highlight-c++"><div class="highlight"><pre><span class="k">struct</span> <span class="n">__single_inheritance</span> <span class="n">S</span><span class="p">;</span>
+<span class="kt">int</span> <span class="n">S</span><span class="o">::*</span><span class="n">i</span><span class="p">;</span>
+<span class="k">struct</span> <span class="n">S</span> <span class="p">{};</span>
+</pre></div>
+</div>
+<p>This keyword can be applied to class templates but only has an effect when used
+on full specializations:</p>
+<div class="highlight-c++"><div class="highlight"><pre><span class="k">template</span> <span class="o"><</span><span class="k">typename</span> <span class="n">T</span><span class="p">,</span> <span class="k">typename</span> <span class="n">U</span><span class="o">></span> <span class="k">struct</span> <span class="n">__single_inheritance</span> <span class="n">A</span><span class="p">;</span> <span class="c1">// warning: inheritance model ignored on primary template</span>
+<span class="k">template</span> <span class="o"><</span><span class="k">typename</span> <span class="n">T</span><span class="o">></span> <span class="k">struct</span> <span class="n">__multiple_inheritance</span> <span class="n">A</span><span class="o"><</span><span class="n">T</span><span class="p">,</span> <span class="n">T</span><span class="o">></span><span class="p">;</span> <span class="c1">// warning: inheritance model ignored on partial specialization</span>
+<span class="k">template</span> <span class="o"><></span> <span class="k">struct</span> <span class="n">__single_inheritance</span> <span class="n">A</span><span class="o"><</span><span class="kt">int</span><span class="p">,</span> <span class="kt">float</span><span class="o">></span><span class="p">;</span>
+</pre></div>
+</div>
+<p>Note that choosing an inheritance model less general than strictly necessary is
+an error:</p>
+<div class="highlight-c++"><div class="highlight"><pre><span class="k">struct</span> <span class="n">__multiple_inheritance</span> <span class="n">S</span><span class="p">;</span> <span class="c1">// error: inheritance model does not match definition</span>
+<span class="kt">int</span> <span class="n">S</span><span class="o">::*</span><span class="n">i</span><span class="p">;</span>
+<span class="k">struct</span> <span class="n">S</span> <span class="p">{};</span>
+</pre></div>
+</div>
+</div>
+<div class="section" id="novtable">
+<h3><a class="toc-backref" href="#id45">novtable</a><a class="headerlink" href="#novtable" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td> </td>
+<td> </td>
+<td>X</td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>This attribute can be added to a class declaration or definition to signal to
+the compiler that constructors and destructors will not reference the virtual
+function table.</p>
+</div>
+</div>
+<div class="section" id="statement-attributes">
+<h2><a class="toc-backref" href="#id46">Statement Attributes</a><a class="headerlink" href="#statement-attributes" title="Permalink to this headline">¶</a></h2>
+<div class="section" id="fallthrough-clang-fallthrough">
+<h3><a class="toc-backref" href="#id47">fallthrough (clang::fallthrough)</a><a class="headerlink" href="#fallthrough-clang-fallthrough" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td> </td>
+<td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>The <tt class="docutils literal"><span class="pre">clang::fallthrough</span></tt> attribute is used along with the
+<tt class="docutils literal"><span class="pre">-Wimplicit-fallthrough</span></tt> argument to annotate intentional fall-through
+between switch labels.  It can only be applied to a null statement placed at a
+point of execution between any statement and the next switch label.  It is
+common to mark these places with a specific comment, but this attribute is
+meant to replace comments with a more strict annotation, which can be checked
+by the compiler.  This attribute doesn’t change semantics of the code and can
+be used wherever an intended fall-through occurs.  It is designed to mimic
+control-flow statements like <tt class="docutils literal"><span class="pre">break;</span></tt>, so it can be placed in most places
+where <tt class="docutils literal"><span class="pre">break;</span></tt> can, but only if there are no statements on the execution path
+between it and the next switch label.</p>
+<p>Here is an example:</p>
+<div class="highlight-c++"><div class="highlight"><pre><span class="c1">// compile with -Wimplicit-fallthrough</span>
+<span class="k">switch</span> <span class="p">(</span><span class="n">n</span><span class="p">)</span> <span class="p">{</span>
+<span class="k">case</span> <span class="mi">22</span>:
+<span class="k">case</span> <span class="mi">33</span>:  <span class="c1">// no warning: no statements between case labels</span>
+  <span class="n">f</span><span class="p">();</span>
+<span class="k">case</span> <span class="mi">44</span>:  <span class="c1">// warning: unannotated fall-through</span>
+  <span class="n">g</span><span class="p">();</span>
+  <span class="p">[[</span><span class="n">clang</span><span class="o">::</span><span class="n">fallthrough</span><span class="p">]];</span>
+<span class="k">case</span> <span class="mi">55</span>:  <span class="c1">// no warning</span>
+  <span class="k">if</span> <span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="p">{</span>
+    <span class="n">h</span><span class="p">();</span>
+    <span class="k">break</span><span class="p">;</span>
+  <span class="p">}</span>
+  <span class="k">else</span> <span class="p">{</span>
+    <span class="n">i</span><span class="p">();</span>
+    <span class="p">[[</span><span class="n">clang</span><span class="o">::</span><span class="n">fallthrough</span><span class="p">]];</span>
+  <span class="p">}</span>
+<span class="k">case</span> <span class="mi">66</span>:  <span class="c1">// no warning</span>
+  <span class="n">p</span><span class="p">();</span>
+  <span class="p">[[</span><span class="n">clang</span><span class="o">::</span><span class="n">fallthrough</span><span class="p">]];</span> <span class="c1">// warning: fallthrough annotation does not</span>
+                          <span class="c1">//          directly precede case label</span>
+  <span class="n">q</span><span class="p">();</span>
+<span class="k">case</span> <span class="mi">77</span>:  <span class="c1">// warning: unannotated fall-through</span>
+  <span class="n">r</span><span class="p">();</span>
+<span class="p">}</span>
+</pre></div>
+</div>
+</div>
+<div class="section" id="pragma-clang-loop">
+<h3><a class="toc-backref" href="#id48">#pragma clang loop</a><a class="headerlink" href="#pragma-clang-loop" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td> </td>
+<td> </td>
+<td> </td>
+<td> </td>
+<td>X</td>
+</tr>
+</tbody>
+</table>
+<p>The <tt class="docutils literal"><span class="pre">#pragma</span> <span class="pre">clang</span> <span class="pre">loop</span></tt> directive allows loop optimization hints to be
+specified for the subsequent loop. The directive allows vectorization,
+interleaving, and unrolling to be enabled or disabled. Vector width as well
+as interleave and unrolling count can be manually specified. See
+<a class="reference external" href="http://clang.llvm.org/docs/LanguageExtensions.html#extensions-for-loop-hint-optimizations">language extensions</a>
+for details.</p>
+</div>
+<div class="section" id="pragma-unroll-pragma-nounroll">
+<h3><a class="toc-backref" href="#id49">#pragma unroll, #pragma nounroll</a><a class="headerlink" href="#pragma-unroll-pragma-nounroll" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td> </td>
+<td> </td>
+<td> </td>
+<td> </td>
+<td>X</td>
+</tr>
+</tbody>
+</table>
+<p>Loop unrolling optimization hints can be specified with <tt class="docutils literal"><span class="pre">#pragma</span> <span class="pre">unroll</span></tt> and
+<tt class="docutils literal"><span class="pre">#pragma</span> <span class="pre">nounroll</span></tt>. The pragma is placed immediately before a for, while,
+do-while, or c++11 range-based for loop.</p>
+<p>Specifying <tt class="docutils literal"><span class="pre">#pragma</span> <span class="pre">unroll</span></tt> without a parameter directs the loop unroller to
+attempt to fully unroll the loop if the trip count is known at compile time:</p>
+<div class="highlight-c++"><div class="highlight"><pre><span class="cp">#pragma unroll</span>
+<span class="k">for</span> <span class="p">(...)</span> <span class="p">{</span>
+  <span class="p">...</span>
+<span class="p">}</span>
+</pre></div>
+</div>
+<p>Specifying the optional parameter, <tt class="docutils literal"><span class="pre">#pragma</span> <span class="pre">unroll</span> <span class="pre">_value_</span></tt>, directs the
+unroller to unroll the loop <tt class="docutils literal"><span class="pre">_value_</span></tt> times.  The parameter may optionally be
+enclosed in parentheses:</p>
+<div class="highlight-c++"><div class="highlight"><pre><span class="cp">#pragma unroll 16</span>
+<span class="k">for</span> <span class="p">(...)</span> <span class="p">{</span>
+  <span class="p">...</span>
+<span class="p">}</span>
+
+<span class="cp">#pragma unroll(16)</span>
+<span class="k">for</span> <span class="p">(...)</span> <span class="p">{</span>
+  <span class="p">...</span>
+<span class="p">}</span>
+</pre></div>
+</div>
+<p>Specifying <tt class="docutils literal"><span class="pre">#pragma</span> <span class="pre">nounroll</span></tt> indicates that the loop should not be unrolled:</p>
+<div class="highlight-c++"><div class="highlight"><pre><span class="cp">#pragma nounroll</span>
+<span class="k">for</span> <span class="p">(...)</span> <span class="p">{</span>
+  <span class="p">...</span>
+<span class="p">}</span>
+</pre></div>
+</div>
+<p><tt class="docutils literal"><span class="pre">#pragma</span> <span class="pre">unroll</span></tt> and <tt class="docutils literal"><span class="pre">#pragma</span> <span class="pre">unroll</span> <span class="pre">_value_</span></tt> have identical semantics to
+<tt class="docutils literal"><span class="pre">#pragma</span> <span class="pre">clang</span> <span class="pre">loop</span> <span class="pre">unroll(full)</span></tt> and
+<tt class="docutils literal"><span class="pre">#pragma</span> <span class="pre">clang</span> <span class="pre">loop</span> <span class="pre">unroll_count(_value_)</span></tt> respectively. <tt class="docutils literal"><span class="pre">#pragma</span> <span class="pre">nounroll</span></tt>
+is equivalent to <tt class="docutils literal"><span class="pre">#pragma</span> <span class="pre">clang</span> <span class="pre">loop</span> <span class="pre">unroll(disable)</span></tt>.  See
+<a class="reference external" href="http://clang.llvm.org/docs/LanguageExtensions.html#extensions-for-loop-hint-optimizations">language extensions</a>
+for further details including limitations of the unroll hints.</p>
+</div>
+</div>
+<div class="section" id="calling-conventions">
+<h2><a class="toc-backref" href="#id50">Calling Conventions</a><a class="headerlink" href="#calling-conventions" title="Permalink to this headline">¶</a></h2>
+<p>Clang supports several different calling conventions, depending on the target
+platform and architecture. The calling convention used for a function determines
+how parameters are passed, how results are returned to the caller, and other
+low-level details of calling a function.</p>
+<div class="section" id="fastcall-gnu-fastcall-fastcall-fastcall">
+<h3><a class="toc-backref" href="#id51">fastcall (gnu::fastcall, __fastcall, _fastcall)</a><a class="headerlink" href="#fastcall-gnu-fastcall-fastcall-fastcall" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td>X</td>
+<td> </td>
+<td>X</td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>On 32-bit x86 targets, this attribute changes the calling convention of a
+function to use ECX and EDX as register parameters and clear parameters off of
+the stack on return. This convention does not support variadic calls or
+unprototyped functions in C, and has no effect on x86_64 targets. This calling
+convention is supported primarily for compatibility with existing code. Users
+seeking register parameters should use the <tt class="docutils literal"><span class="pre">regparm</span></tt> attribute, which does
+not require callee-cleanup.  See the documentation for <a class="reference external" href="http://msdn.microsoft.com/en-us/library/6xa169sk.aspx">__fastcall</a> on MSDN.</p>
+</div>
+<div class="section" id="ms-abi-gnu-ms-abi">
+<h3><a class="toc-backref" href="#id52">ms_abi (gnu::ms_abi)</a><a class="headerlink" href="#ms-abi-gnu-ms-abi" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>On non-Windows x86_64 targets, this attribute changes the calling convention of
+a function to match the default convention used on Windows x86_64. This
+attribute has no effect on Windows targets or non-x86_64 targets.</p>
+</div>
+<div class="section" id="pcs-gnu-pcs">
+<h3><a class="toc-backref" href="#id53">pcs (gnu::pcs)</a><a class="headerlink" href="#pcs-gnu-pcs" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>On ARM targets, this attribute can be used to select calling conventions
+similar to <tt class="docutils literal"><span class="pre">stdcall</span></tt> on x86. Valid parameter values are “aapcs” and
+“aapcs-vfp”.</p>
+</div>
+<div class="section" id="regparm-gnu-regparm">
+<h3><a class="toc-backref" href="#id54">regparm (gnu::regparm)</a><a class="headerlink" href="#regparm-gnu-regparm" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>On 32-bit x86 targets, the regparm attribute causes the compiler to pass
+the first three integer parameters in EAX, EDX, and ECX instead of on the
+stack. This attribute has no effect on variadic functions, and all parameters
+are passed via the stack as normal.</p>
+</div>
+<div class="section" id="stdcall-gnu-stdcall-stdcall-stdcall">
+<h3><a class="toc-backref" href="#id55">stdcall (gnu::stdcall, __stdcall, _stdcall)</a><a class="headerlink" href="#stdcall-gnu-stdcall-stdcall-stdcall" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td>X</td>
+<td> </td>
+<td>X</td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>On 32-bit x86 targets, this attribute changes the calling convention of a
+function to clear parameters off of the stack on return. This convention does
+not support variadic calls or unprototyped functions in C, and has no effect on
+x86_64 targets. This calling convention is used widely by the Windows API and
+COM applications.  See the documentation for <a class="reference external" href="http://msdn.microsoft.com/en-us/library/zxk0tw93.aspx">__stdcall</a> on MSDN.</p>
+</div>
+<div class="section" id="thiscall-gnu-thiscall-thiscall-thiscall">
+<h3><a class="toc-backref" href="#id56">thiscall (gnu::thiscall, __thiscall, _thiscall)</a><a class="headerlink" href="#thiscall-gnu-thiscall-thiscall-thiscall" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td>X</td>
+<td> </td>
+<td>X</td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>On 32-bit x86 targets, this attribute changes the calling convention of a
+function to use ECX for the first parameter (typically the implicit <tt class="docutils literal"><span class="pre">this</span></tt>
+parameter of C++ methods) and clear parameters off of the stack on return. This
+convention does not support variadic calls or unprototyped functions in C, and
+has no effect on x86_64 targets. See the documentation for <a class="reference external" href="http://msdn.microsoft.com/en-us/library/ek8tkfbw.aspx">__thiscall</a> on
+MSDN.</p>
+</div>
+<div class="section" id="vectorcall-vectorcall-vectorcall">
+<h3><a class="toc-backref" href="#id57">vectorcall (__vectorcall, _vectorcall)</a><a class="headerlink" href="#vectorcall-vectorcall-vectorcall" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td> </td>
+<td> </td>
+<td>X</td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>On 32-bit x86 <em>and</em> x86_64 targets, this attribute changes the calling
+convention of a function to pass vector parameters in SSE registers.</p>
+<p>On 32-bit x86 targets, this calling convention is similar to <tt class="docutils literal"><span class="pre">__fastcall</span></tt>.
+The first two integer parameters are passed in ECX and EDX. Subsequent integer
+parameters are passed in memory, and callee clears the stack.  On x86_64
+targets, the callee does <em>not</em> clear the stack, and integer parameters are
+passed in RCX, RDX, R8, and R9 as is done for the default Windows x64 calling
+convention.</p>
+<p>On both 32-bit x86 and x86_64 targets, vector and floating point arguments are
+passed in XMM0-XMM5. Homogenous vector aggregates of up to four elements are
+passed in sequential SSE registers if enough are available. If AVX is enabled,
+256 bit vectors are passed in YMM0-YMM5. Any vector or aggregate type that
+cannot be passed in registers for any reason is passed by reference, which
+allows the caller to align the parameter memory.</p>
+<p>See the documentation for <a class="reference external" href="http://msdn.microsoft.com/en-us/library/dn375768.aspx">__vectorcall</a> on MSDN for more details.</p>
+</div>
+</div>
+<div class="section" id="consumed-annotation-checking">
+<h2><a class="toc-backref" href="#id58">Consumed Annotation Checking</a><a class="headerlink" href="#consumed-annotation-checking" title="Permalink to this headline">¶</a></h2>
+<p>Clang supports additional attributes for checking basic resource management
+properties, specifically for unique objects that have a single owning reference.
+The following attributes are currently supported, although <strong>the implementation
+for these annotations is currently in development and are subject to change.</strong></p>
+<div class="section" id="callable-when">
+<h3><a class="toc-backref" href="#id59">callable_when</a><a class="headerlink" href="#callable-when" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>Use <tt class="docutils literal"><span class="pre">__attribute__((callable_when(...)))</span></tt> to indicate what states a method
+may be called in.  Valid states are unconsumed, consumed, or unknown.  Each
+argument to this attribute must be a quoted string.  E.g.:</p>
+<p><tt class="docutils literal"><span class="pre">__attribute__((callable_when("unconsumed",</span> <span class="pre">"unknown")))</span></tt></p>
+</div>
+<div class="section" id="consumable">
+<h3><a class="toc-backref" href="#id60">consumable</a><a class="headerlink" href="#consumable" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>Each <tt class="docutils literal"><span class="pre">class</span></tt> that uses any of the typestate annotations must first be marked
+using the <tt class="docutils literal"><span class="pre">consumable</span></tt> attribute.  Failure to do so will result in a warning.</p>
+<p>This attribute accepts a single parameter that must be one of the following:
+<tt class="docutils literal"><span class="pre">unknown</span></tt>, <tt class="docutils literal"><span class="pre">consumed</span></tt>, or <tt class="docutils literal"><span class="pre">unconsumed</span></tt>.</p>
+</div>
+<div class="section" id="param-typestate">
+<h3><a class="toc-backref" href="#id61">param_typestate</a><a class="headerlink" href="#param-typestate" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>This attribute specifies expectations about function parameters.  Calls to an
+function with annotated parameters will issue a warning if the corresponding
+argument isn’t in the expected state.  The attribute is also used to set the
+initial state of the parameter when analyzing the function’s body.</p>
+</div>
+<div class="section" id="return-typestate">
+<h3><a class="toc-backref" href="#id62">return_typestate</a><a class="headerlink" href="#return-typestate" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>The <tt class="docutils literal"><span class="pre">return_typestate</span></tt> attribute can be applied to functions or parameters.
+When applied to a function the attribute specifies the state of the returned
+value.  The function’s body is checked to ensure that it always returns a value
+in the specified state.  On the caller side, values returned by the annotated
+function are initialized to the given state.</p>
+<p>When applied to a function parameter it modifies the state of an argument after
+a call to the function returns.  The function’s body is checked to ensure that
+the parameter is in the expected state before returning.</p>
+</div>
+<div class="section" id="set-typestate">
+<h3><a class="toc-backref" href="#id63">set_typestate</a><a class="headerlink" href="#set-typestate" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>Annotate methods that transition an object into a new state with
+<tt class="docutils literal"><span class="pre">__attribute__((set_typestate(new_state)))</span></tt>.  The new state must be
+unconsumed, consumed, or unknown.</p>
+</div>
+<div class="section" id="test-typestate">
+<h3><a class="toc-backref" href="#id64">test_typestate</a><a class="headerlink" href="#test-typestate" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>Use <tt class="docutils literal"><span class="pre">__attribute__((test_typestate(tested_state)))</span></tt> to indicate that a method
+returns true if the object is in the specified state..</p>
+</div>
+</div>
+<div class="section" id="type-safety-checking">
+<h2><a class="toc-backref" href="#id65">Type Safety Checking</a><a class="headerlink" href="#type-safety-checking" title="Permalink to this headline">¶</a></h2>
+<p>Clang supports additional attributes to enable checking type safety properties
+that can’t be enforced by the C type system.  Use cases include:</p>
+<ul class="simple">
+<li>MPI library implementations, where these attributes enable checking that
+the buffer type matches the passed <tt class="docutils literal"><span class="pre">MPI_Datatype</span></tt>;</li>
+<li>for HDF5 library there is a similar use case to MPI;</li>
+<li>checking types of variadic functions’ arguments for functions like
+<tt class="docutils literal"><span class="pre">fcntl()</span></tt> and <tt class="docutils literal"><span class="pre">ioctl()</span></tt>.</li>
+</ul>
+<p>You can detect support for these attributes with <tt class="docutils literal"><span class="pre">__has_attribute()</span></tt>.  For
+example:</p>
+<div class="highlight-c++"><div class="highlight"><pre><span class="cp">#if defined(__has_attribute)</span>
+<span class="cp">#  if __has_attribute(argument_with_type_tag) && \</span>
+<span class="cp">      __has_attribute(pointer_with_type_tag) && \</span>
+<span class="cp">      __has_attribute(type_tag_for_datatype)</span>
+<span class="cp">#    define ATTR_MPI_PWT(buffer_idx, type_idx) __attribute__((pointer_with_type_tag(mpi,buffer_idx,type_idx)))</span>
+<span class="cm">/* ... other macros ...  */</span>
+<span class="cp">#  endif</span>
+<span class="cp">#endif</span>
+
+<span class="cp">#if !defined(ATTR_MPI_PWT)</span>
+<span class="cp"># define ATTR_MPI_PWT(buffer_idx, type_idx)</span>
+<span class="cp">#endif</span>
+
+<span class="kt">int</span> <span class="nf">MPI_Send</span><span class="p">(</span><span class="kt">void</span> <span class="o">*</span><span class="n">buf</span><span class="p">,</span> <span class="kt">int</span> <span class="n">count</span><span class="p">,</span> <span class="n">MPI_Datatype</span> <span class="n">datatype</span> <span class="cm">/*, other args omitted */</span><span class="p">)</span>
+    <span class="n">ATTR_MPI_PWT</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">3</span><span class="p">);</span>
+</pre></div>
+</div>
+<div class="section" id="argument-with-type-tag">
+<h3><a class="toc-backref" href="#id66">argument_with_type_tag</a><a class="headerlink" href="#argument-with-type-tag" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>Use <tt class="docutils literal"><span class="pre">__attribute__((argument_with_type_tag(arg_kind,</span> <span class="pre">arg_idx,</span>
+<span class="pre">type_tag_idx)))</span></tt> on a function declaration to specify that the function
+accepts a type tag that determines the type of some other argument.
+<tt class="docutils literal"><span class="pre">arg_kind</span></tt> is an identifier that should be used when annotating all
+applicable type tags.</p>
+<p>This attribute is primarily useful for checking arguments of variadic functions
+(<tt class="docutils literal"><span class="pre">pointer_with_type_tag</span></tt> can be used in most non-variadic cases).</p>
+<p>For example:</p>
+<div class="highlight-c++"><div class="highlight"><pre><span class="kt">int</span> <span class="nf">fcntl</span><span class="p">(</span><span class="kt">int</span> <span class="n">fd</span><span class="p">,</span> <span class="kt">int</span> <span class="n">cmd</span><span class="p">,</span> <span class="p">...)</span>
+    <span class="n">__attribute__</span><span class="p">((</span> <span class="n">argument_with_type_tag</span><span class="p">(</span><span class="n">fcntl</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span> <span class="p">));</span>
+</pre></div>
+</div>
+</div>
+<div class="section" id="pointer-with-type-tag">
+<h3><a class="toc-backref" href="#id67">pointer_with_type_tag</a><a class="headerlink" href="#pointer-with-type-tag" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>Use <tt class="docutils literal"><span class="pre">__attribute__((pointer_with_type_tag(ptr_kind,</span> <span class="pre">ptr_idx,</span> <span class="pre">type_tag_idx)))</span></tt>
+on a function declaration to specify that the function accepts a type tag that
+determines the pointee type of some other pointer argument.</p>
+<p>For example:</p>
+<div class="highlight-c++"><div class="highlight"><pre><span class="kt">int</span> <span class="nf">MPI_Send</span><span class="p">(</span><span class="kt">void</span> <span class="o">*</span><span class="n">buf</span><span class="p">,</span> <span class="kt">int</span> <span class="n">count</span><span class="p">,</span> <span class="n">MPI_Datatype</span> <span class="n">datatype</span> <span class="cm">/*, other args omitted */</span><span class="p">)</span>
+    <span class="n">__attribute__</span><span class="p">((</span> <span class="n">pointer_with_type_tag</span><span class="p">(</span><span class="n">mpi</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span> <span class="p">));</span>
+</pre></div>
+</div>
+</div>
+<div class="section" id="type-tag-for-datatype">
+<h3><a class="toc-backref" href="#id68">type_tag_for_datatype</a><a class="headerlink" href="#type-tag-for-datatype" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>Clang supports annotating type tags of two forms.</p>
+<ul>
+<li><p class="first"><strong>Type tag that is an expression containing a reference to some declared
+identifier.</strong> Use <tt class="docutils literal"><span class="pre">__attribute__((type_tag_for_datatype(kind,</span> <span class="pre">type)))</span></tt> on a
+declaration with that identifier:</p>
+<div class="highlight-c++"><div class="highlight"><pre><span class="k">extern</span> <span class="k">struct</span> <span class="n">mpi_datatype</span> <span class="n">mpi_datatype_int</span>
+    <span class="nf">__attribute__</span><span class="p">((</span> <span class="n">type_tag_for_datatype</span><span class="p">(</span><span class="n">mpi</span><span class="p">,</span><span class="kt">int</span><span class="p">)</span> <span class="p">));</span>
+<span class="cp">#define MPI_INT ((MPI_Datatype) &mpi_datatype_int)</span>
+</pre></div>
+</div>
+</li>
+<li><p class="first"><strong>Type tag that is an integral literal.</strong> Introduce a <tt class="docutils literal"><span class="pre">static</span> <span class="pre">const</span></tt>
+variable with a corresponding initializer value and attach
+<tt class="docutils literal"><span class="pre">__attribute__((type_tag_for_datatype(kind,</span> <span class="pre">type)))</span></tt> on that declaration,
+for example:</p>
+<div class="highlight-c++"><div class="highlight"><pre><span class="cp">#define MPI_INT ((MPI_Datatype) 42)</span>
+<span class="k">static</span> <span class="k">const</span> <span class="n">MPI_Datatype</span> <span class="n">mpi_datatype_int</span>
+    <span class="n">__attribute__</span><span class="p">((</span> <span class="n">type_tag_for_datatype</span><span class="p">(</span><span class="n">mpi</span><span class="p">,</span><span class="kt">int</span><span class="p">)</span> <span class="p">))</span> <span class="o">=</span> <span class="mi">42</span>
+</pre></div>
+</div>
+</li>
+</ul>
+<p>The attribute also accepts an optional third argument that determines how the
+expression is compared to the type tag.  There are two supported flags:</p>
+<ul>
+<li><p class="first"><tt class="docutils literal"><span class="pre">layout_compatible</span></tt> will cause types to be compared according to
+layout-compatibility rules (C++11 [class.mem] p 17, 18).  This is
+implemented to support annotating types like <tt class="docutils literal"><span class="pre">MPI_DOUBLE_INT</span></tt>.</p>
+<p>For example:</p>
+<div class="highlight-c++"><div class="highlight"><pre><span class="cm">/* In mpi.h */</span>
+<span class="k">struct</span> <span class="n">internal_mpi_double_int</span> <span class="p">{</span> <span class="kt">double</span> <span class="n">d</span><span class="p">;</span> <span class="kt">int</span> <span class="n">i</span><span class="p">;</span> <span class="p">};</span>
+<span class="k">extern</span> <span class="k">struct</span> <span class="n">mpi_datatype</span> <span class="n">mpi_datatype_double_int</span>
+    <span class="nf">__attribute__</span><span class="p">((</span> <span class="n">type_tag_for_datatype</span><span class="p">(</span><span class="n">mpi</span><span class="p">,</span> <span class="k">struct</span> <span class="n">internal_mpi_double_int</span><span class="p">,</span> <span class="n">layout_compatible</span><span class="p">)</span> <span class="p">));</span>
+
+<span class="cp">#define MPI_DOUBLE_INT ((MPI_Datatype) &mpi_datatype_double_int)</span>
+
+<span class="cm">/* In user code */</span>
+<span class="k">struct</span> <span class="n">my_pair</span> <span class="p">{</span> <span class="kt">double</span> <span class="n">a</span><span class="p">;</span> <span class="kt">int</span> <span class="n">b</span><span class="p">;</span> <span class="p">};</span>
+<span class="k">struct</span> <span class="n">my_pair</span> <span class="o">*</span><span class="n">buffer</span><span class="p">;</span>
+<span class="n">MPI_Send</span><span class="p">(</span><span class="n">buffer</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">MPI_DOUBLE_INT</span> <span class="cm">/*, ...  */</span><span class="p">);</span> <span class="c1">// no warning</span>
+
+<span class="k">struct</span> <span class="n">my_int_pair</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">a</span><span class="p">;</span> <span class="kt">int</span> <span class="n">b</span><span class="p">;</span> <span class="p">}</span>
+<span class="k">struct</span> <span class="n">my_int_pair</span> <span class="o">*</span><span class="n">buffer2</span><span class="p">;</span>
+<span class="n">MPI_Send</span><span class="p">(</span><span class="n">buffer2</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">MPI_DOUBLE_INT</span> <span class="cm">/*, ...  */</span><span class="p">);</span> <span class="c1">// warning: actual buffer element</span>
+                                                  <span class="c1">// type 'struct my_int_pair'</span>
+                                                  <span class="c1">// doesn't match specified MPI_Datatype</span>
+</pre></div>
+</div>
+</li>
+<li><p class="first"><tt class="docutils literal"><span class="pre">must_be_null</span></tt> specifies that the expression should be a null pointer
+constant, for example:</p>
+<div class="highlight-c++"><div class="highlight"><pre><span class="cm">/* In mpi.h */</span>
+<span class="k">extern</span> <span class="k">struct</span> <span class="n">mpi_datatype</span> <span class="n">mpi_datatype_null</span>
+    <span class="nf">__attribute__</span><span class="p">((</span> <span class="n">type_tag_for_datatype</span><span class="p">(</span><span class="n">mpi</span><span class="p">,</span> <span class="kt">void</span><span class="p">,</span> <span class="n">must_be_null</span><span class="p">)</span> <span class="p">));</span>
+
+<span class="cp">#define MPI_DATATYPE_NULL ((MPI_Datatype) &mpi_datatype_null)</span>
+
+<span class="cm">/* In user code */</span>
+<span class="n">MPI_Send</span><span class="p">(</span><span class="n">buffer</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">MPI_DATATYPE_NULL</span> <span class="cm">/*, ...  */</span><span class="p">);</span> <span class="c1">// warning: MPI_DATATYPE_NULL</span>
+                                                    <span class="c1">// was specified but buffer</span>
+                                                    <span class="c1">// is not a null pointer</span>
+</pre></div>
+</div>
+</li>
+</ul>
+</div>
+</div>
+<div class="section" id="opencl-address-spaces">
+<h2><a class="toc-backref" href="#id69">OpenCL Address Spaces</a><a class="headerlink" href="#opencl-address-spaces" title="Permalink to this headline">¶</a></h2>
+<p>The address space qualifier may be used to specify the region of memory that is
+used to allocate the object. OpenCL supports the following address spaces:
+__generic(generic), __global(global), __local(local), __private(private),
+__constant(constant).</p>
+<blockquote>
+<div><div class="highlight-c"><div class="highlight"><pre><span class="n">__constant</span> <span class="kt">int</span> <span class="n">c</span> <span class="o">=</span> <span class="p">...;</span>
+
+<span class="n">__generic</span> <span class="kt">int</span><span class="o">*</span> <span class="nf">foo</span><span class="p">(</span><span class="n">global</span> <span class="kt">int</span><span class="o">*</span> <span class="n">g</span><span class="p">)</span> <span class="p">{</span>
+  <span class="n">__local</span> <span class="kt">int</span><span class="o">*</span> <span class="n">l</span><span class="p">;</span>
+  <span class="n">private</span> <span class="kt">int</span> <span class="n">p</span><span class="p">;</span>
+  <span class="p">...</span>
+  <span class="k">return</span> <span class="n">l</span><span class="p">;</span>
+<span class="p">}</span>
+</pre></div>
+</div>
+</div></blockquote>
+<p>More details can be found in the OpenCL C language Spec v2.0, Section 6.5.</p>
+<div class="section" id="constant-constant">
+<h3><a class="toc-backref" href="#id70">__constant(constant)</a><a class="headerlink" href="#constant-constant" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td> </td>
+<td> </td>
+<td> </td>
+<td>X</td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>The constant address space attribute signals that an object is located in
+a constant (non-modifiable) memory region. It is available to all work items.
+Any type can be annotated with the constant address space attribute. Objects
+with the constant address space qualifier can be declared in any scope and must
+have an initializer.</p>
+</div>
+<div class="section" id="generic-generic">
+<h3><a class="toc-backref" href="#id71">__generic(generic)</a><a class="headerlink" href="#generic-generic" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td> </td>
+<td> </td>
+<td> </td>
+<td>X</td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>The generic address space attribute is only available with OpenCL v2.0 and later.
+It can be used with pointer types. Variables in global and local scope and
+function parameters in non-kernel functions can have the generic address space
+type attribute. It is intended to be a placeholder for any other address space
+except for ‘__constant’ in OpenCL code which can be used with multiple address
+spaces.</p>
+</div>
+<div class="section" id="global-global">
+<h3><a class="toc-backref" href="#id72">__global(global)</a><a class="headerlink" href="#global-global" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td> </td>
+<td> </td>
+<td> </td>
+<td>X</td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>The global address space attribute specifies that an object is allocated in
+global memory, which is accessible by all work items. The content stored in this
+memory area persists between kernel executions. Pointer types to the global
+address space are allowed as function parameters or local variables. Starting
+with OpenCL v2.0, the global address space can be used with global (program
+scope) variables and static local variable as well.</p>
+</div>
+<div class="section" id="local-local">
+<h3><a class="toc-backref" href="#id73">__local(local)</a><a class="headerlink" href="#local-local" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td> </td>
+<td> </td>
+<td> </td>
+<td>X</td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>The local address space specifies that an object is allocated in the local (work
+group) memory area, which is accessible to all work items in the same work
+group. The content stored in this memory region is not accessible after
+the kernel execution ends. In a kernel function scope, any variable can be in
+the local address space. In other scopes, only pointer types to the local address
+space are allowed. Local address space variables cannot have an initializer.</p>
+</div>
+<div class="section" id="private-private">
+<h3><a class="toc-backref" href="#id74">__private(private)</a><a class="headerlink" href="#private-private" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td> </td>
+<td> </td>
+<td> </td>
+<td>X</td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>The private address space specifies that an object is allocated in the private
+(work item) memory. Other work items cannot access the same memory area and its
+content is destroyed after work item execution ends. Local variables can be
+declared in the private address space. Function arguments are always in the
+private address space. Kernel function arguments of a pointer or an array type
+cannot point to the private address space.</p>
+</div>
+</div>
+<div class="section" id="nullability-attributes">
+<h2><a class="toc-backref" href="#id75">Nullability Attributes</a><a class="headerlink" href="#nullability-attributes" title="Permalink to this headline">¶</a></h2>
+<p>Whether a particular pointer may be “null” is an important concern when working with pointers in the C family of languages. The various nullability attributes indicate whether a particular pointer can be null or not, which makes APIs more expressive and can help static analysis tools identify bugs involving null pointers. Clang supports several kinds of nullability attributes: the <tt class="docutils literal"><span class="pre">nonnull</span></tt> and <tt class="docutils literal"><span class="pre">returns_nonnull</span></tt> attributes indicate which function or method parameters and result types can never be null, while nullability type qualifiers indicate which pointer types can be null (<tt class="docutils literal"><span class="pre">_Nullable</span></tt>) or cannot be null (<tt class="docutils literal"><span class="pre">_Nonnull</span></tt>).</p>
+<p>The nullability (type) qualifiers express whether a value of a given pointer type can be null (the <tt class="docutils literal"><span class="pre">_Nullable</span></tt> qualifier), doesn’t have a defined meaning for null (the <tt class="docutils literal"><span class="pre">_Nonnull</span></tt> qualifier), or for which the purpose of null is unclear (the <tt class="docutils literal"><span class="pre">_Null_unspecified</span></tt> qualifier). Because nullability qualifiers are expressed within the type system, they are more general than the <tt class="docutils literal"><span class="pre">nonnull</span></tt> and <tt class="docutils literal"><span class="pre">returns_nonnull</span></tt> attributes, allowing one to express (for example) a nullable pointer to an array of nonnull pointers. Nullability qualifiers are written to the right of the pointer to which they apply. For example:</p>
+<blockquote>
+<div><div class="highlight-c"><div class="highlight"><pre><span class="c1">// No meaningful result when 'ptr' is null (here, it happens to be undefined behavior).</span>
+<span class="kt">int</span> <span class="nf">fetch</span><span class="p">(</span><span class="kt">int</span> <span class="o">*</span> <span class="n">_Nonnull</span> <span class="n">ptr</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="o">*</span><span class="n">ptr</span><span class="p">;</span> <span class="p">}</span>
+
+<span class="c1">// 'ptr' may be null.</span>
+<span class="kt">int</span> <span class="nf">fetch_or_zero</span><span class="p">(</span><span class="kt">int</span> <span class="o">*</span> <span class="n">_Nullable</span> <span class="n">ptr</span><span class="p">)</span> <span class="p">{</span>
+  <span class="k">return</span> <span class="n">ptr</span> <span class="o">?</span> <span class="o">*</span><span class="n">ptr</span> <span class="o">:</span> <span class="mi">0</span><span class="p">;</span>
+<span class="p">}</span>
+
+<span class="c1">// A nullable pointer to non-null pointers to const characters.</span>
+<span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="nf">join_strings</span><span class="p">(</span><span class="k">const</span> <span class="kt">char</span> <span class="o">*</span> <span class="n">_Nonnull</span> <span class="o">*</span> <span class="n">_Nullable</span> <span class="n">strings</span><span class="p">,</span> <span class="kt">unsigned</span> <span class="n">n</span><span class="p">);</span>
+</pre></div>
+</div>
+</div></blockquote>
+<p>In Objective-C, there is an alternate spelling for the nullability qualifiers that can be used in Objective-C methods and properties using context-sensitive, non-underscored keywords. For example:</p>
+<blockquote>
+<div><div class="highlight-objective-c"><div class="highlight"><pre><span class="k">@interface</span> <span class="nc">NSView</span> : <span class="nc">NSResponder</span>
+  <span class="o">-</span> <span class="p">(</span><span class="n">nullable</span> <span class="n">NSView</span> <span class="o">*</span><span class="p">)</span><span class="n">ancestorSharedWithView</span><span class="o">:</span><span class="p">(</span><span class="n">nonnull</span> <span class="n">NSView</span> <span class="o">*</span><span class="p">)</span><span class="n">aView</span><span class="p">;</span>
+  <span class="k">@property</span> <span class="p">(</span><span class="n">assign</span><span class="p">,</span> <span class="n">nullable</span><span class="p">)</span> <span class="n">NSView</span> <span class="o">*</span><span class="n">superview</span><span class="p">;</span>
+  <span class="k">@property</span> <span class="p">(</span><span class="n">readonly</span><span class="p">,</span> <span class="n">nonnull</span><span class="p">)</span> <span class="n">NSArray</span> <span class="o">*</span><span class="n">subviews</span><span class="p">;</span>
+<span class="k">@end</span>
+</pre></div>
+</div>
+</div></blockquote>
+<div class="section" id="nonnull">
+<h3><a class="toc-backref" href="#id76">nonnull</a><a class="headerlink" href="#nonnull" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>The <tt class="docutils literal"><span class="pre">nonnull</span></tt> attribute indicates that some function parameters must not be null, and can be used in several different ways. It’s original usage (<a class="reference external" href="https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes">from GCC</a>) is as a function (or Objective-C method) attribute that specifies which parameters of the function are nonnull in a comma-separated list. For example:</p>
+<blockquote>
+<div><div class="highlight-c"><div class="highlight"><pre><span class="k">extern</span> <span class="kt">void</span> <span class="o">*</span> <span class="nf">my_memcpy</span> <span class="p">(</span><span class="kt">void</span> <span class="o">*</span><span class="n">dest</span><span class="p">,</span> <span class="k">const</span> <span class="kt">void</span> <span class="o">*</span><span class="n">src</span><span class="p">,</span> <span class="kt">size_t</span> <span class="n">len</span><span class="p">)</span>
+                <span class="n">__attribute__</span><span class="p">((</span><span class="n">nonnull</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)));</span>
+</pre></div>
+</div>
+</div></blockquote>
+<p>Here, the <tt class="docutils literal"><span class="pre">nonnull</span></tt> attribute indicates that parameters 1 and 2
+cannot have a null value. Omitting the parenthesized list of parameter indices means that all parameters of pointer type cannot be null:</p>
+<blockquote>
+<div><div class="highlight-c"><div class="highlight"><pre><span class="k">extern</span> <span class="kt">void</span> <span class="o">*</span> <span class="nf">my_memcpy</span> <span class="p">(</span><span class="kt">void</span> <span class="o">*</span><span class="n">dest</span><span class="p">,</span> <span class="k">const</span> <span class="kt">void</span> <span class="o">*</span><span class="n">src</span><span class="p">,</span> <span class="kt">size_t</span> <span class="n">len</span><span class="p">)</span>
+                <span class="n">__attribute__</span><span class="p">((</span><span class="n">nonnull</span><span class="p">));</span>
+</pre></div>
+</div>
+</div></blockquote>
+<p>Clang also allows the <tt class="docutils literal"><span class="pre">nonnull</span></tt> attribute to be placed directly on a function (or Objective-C method) parameter, eliminating the need to specify the parameter index ahead of type. For example:</p>
+<blockquote>
+<div><div class="highlight-c"><div class="highlight"><pre><span class="k">extern</span> <span class="kt">void</span> <span class="o">*</span> <span class="nf">my_memcpy</span> <span class="p">(</span><span class="kt">void</span> <span class="o">*</span><span class="n">dest</span> <span class="n">__attribute__</span><span class="p">((</span><span class="n">nonnull</span><span class="p">)),</span>
+                         <span class="k">const</span> <span class="kt">void</span> <span class="o">*</span><span class="n">src</span> <span class="n">__attribute__</span><span class="p">((</span><span class="n">nonnull</span><span class="p">)),</span> <span class="kt">size_t</span> <span class="n">len</span><span class="p">);</span>
+</pre></div>
+</div>
+</div></blockquote>
+<p>Note that the <tt class="docutils literal"><span class="pre">nonnull</span></tt> attribute indicates that passing null to a non-null parameter is undefined behavior, which the optimizer may take advantage of to, e.g., remove null checks. The <tt class="docutils literal"><span class="pre">_Nonnull</span></tt> type qualifier indicates that a pointer cannot be null in a more general manner (because it is part of the type system) and does not imply undefined behavior, making it more widely applicable.</p>
+</div>
+<div class="section" id="returns-nonnull">
+<h3><a class="toc-backref" href="#id77">returns_nonnull</a><a class="headerlink" href="#returns-nonnull" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td>X</td>
+<td>X</td>
+<td> </td>
+<td> </td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>The <tt class="docutils literal"><span class="pre">returns_nonnull</span></tt> attribute indicates that a particular function (or Objective-C method) always returns a non-null pointer. For example, a particular system <tt class="docutils literal"><span class="pre">malloc</span></tt> might be defined to terminate a process when memory is not available rather than returning a null pointer:</p>
+<blockquote>
+<div><div class="highlight-c"><div class="highlight"><pre><span class="k">extern</span> <span class="kt">void</span> <span class="o">*</span> <span class="nf">malloc</span> <span class="p">(</span><span class="kt">size_t</span> <span class="n">size</span><span class="p">)</span> <span class="n">__attribute__</span><span class="p">((</span><span class="n">returns_nonnull</span><span class="p">));</span>
+</pre></div>
+</div>
+</div></blockquote>
+<p>The <tt class="docutils literal"><span class="pre">returns_nonnull</span></tt> attribute implies that returning a null pointer is undefined behavior, which the optimizer may take advantage of. The <tt class="docutils literal"><span class="pre">_Nonnull</span></tt> type qualifier indicates that a pointer cannot be null in a more general manner (because it is part of the type system) and does not imply undefined behavior, making it more widely applicable</p>
+</div>
+<div class="section" id="id4">
+<h3><a class="toc-backref" href="#id78">_Nonnull</a><a class="headerlink" href="#id4" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td> </td>
+<td> </td>
+<td> </td>
+<td>X</td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>The <tt class="docutils literal"><span class="pre">_Nonnull</span></tt> nullability qualifier indicates that null is not a meaningful value for a value of the <tt class="docutils literal"><span class="pre">_Nonnull</span></tt> pointer type. For example, given a declaration such as:</p>
+<blockquote>
+<div><div class="highlight-c"><div class="highlight"><pre><span class="kt">int</span> <span class="nf">fetch</span><span class="p">(</span><span class="kt">int</span> <span class="o">*</span> <span class="n">_Nonnull</span> <span class="n">ptr</span><span class="p">);</span>
+</pre></div>
+</div>
+</div></blockquote>
+<p>a caller of <tt class="docutils literal"><span class="pre">fetch</span></tt> should not provide a null value, and the compiler will produce a warning if it sees a literal null value passed to <tt class="docutils literal"><span class="pre">fetch</span></tt>. Note that, unlike the declaration attribute <tt class="docutils literal"><span class="pre">nonnull</span></tt>, the presence of <tt class="docutils literal"><span class="pre">_Nonnull</span></tt> does not imply that passing null is undefined behavior: <tt class="docutils literal"><span class="pre">fetch</span></tt> is free to consider null undefined behavior or (perhaps for backward-compatibility reasons) defensively handle null.</p>
+</div>
+<div class="section" id="null-unspecified">
+<h3><a class="toc-backref" href="#id79">_Null_unspecified</a><a class="headerlink" href="#null-unspecified" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td> </td>
+<td> </td>
+<td> </td>
+<td>X</td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>The <tt class="docutils literal"><span class="pre">_Null_unspecified</span></tt> nullability qualifier indicates that neither the <tt class="docutils literal"><span class="pre">_Nonnull</span></tt> nor <tt class="docutils literal"><span class="pre">_Nullable</span></tt> qualifiers make sense for a particular pointer type. It is used primarily to indicate that the role of null with specific pointers in a nullability-annotated header is unclear, e.g., due to overly-complex implementations or historical factors with a long-lived API.</p>
+</div>
+<div class="section" id="nullable">
+<h3><a class="toc-backref" href="#id80">_Nullable</a><a class="headerlink" href="#nullable" title="Permalink to this headline">¶</a></h3>
+<table border="1" class="docutils">
+<caption>Supported Syntaxes</caption>
+<colgroup>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+</colgroup>
+<thead valign="bottom">
+<tr class="row-odd"><th class="head">GNU</th>
+<th class="head">C++11</th>
+<th class="head">__declspec</th>
+<th class="head">Keyword</th>
+<th class="head">Pragma</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr class="row-even"><td> </td>
+<td> </td>
+<td> </td>
+<td>X</td>
+<td> </td>
+</tr>
+</tbody>
+</table>
+<p>The <tt class="docutils literal"><span class="pre">_Nullable</span></tt> nullability qualifier indicates that a value of the <tt class="docutils literal"><span class="pre">_Nullable</span></tt> pointer type can be null. For example, given:</p>
+<blockquote>
+<div><div class="highlight-c"><div class="highlight"><pre><span class="kt">int</span> <span class="nf">fetch_or_zero</span><span class="p">(</span><span class="kt">int</span> <span class="o">*</span> <span class="n">_Nullable</span> <span class="n">ptr</span><span class="p">);</span>
+</pre></div>
+</div>
+</div></blockquote>
+<p>a caller of <tt class="docutils literal"><span class="pre">fetch_or_zero</span></tt> can provide null.</p>
+</div>
+</div>
+</div>
+
+
+      </div>
+      <div class="bottomnav">
+      
+        <p>
+        «  <a href="AutomaticReferenceCounting.html">Objective-C Automatic Reference Counting (ARC)</a>
+          ::  
+        <a class="uplink" href="index.html">Contents</a>
+          ::  
+        <a href="CrossCompilation.html">Cross-compilation using Clang</a>  Â»
+        </p>
+
+      </div>
+
+    <div class="footer">
+        © Copyright 2007-2015, The Clang Team.
+      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.3.
+    </div>
+  </body>
+</html>
\ No newline at end of file

Added: www-releases/trunk/3.7.1/tools/docs/AutomaticReferenceCounting.html
URL: http://llvm.org/viewvc/llvm-project/www-releases/trunk/3.7.1/tools/docs/AutomaticReferenceCounting.html?rev=257950&view=auto
==============================================================================
--- www-releases/trunk/3.7.1/tools/docs/AutomaticReferenceCounting.html (added)
+++ www-releases/trunk/3.7.1/tools/docs/AutomaticReferenceCounting.html Fri Jan 15 17:13:16 2016
@@ -0,0 +1,2085 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+
+
+<html xmlns="http://www.w3.org/1999/xhtml">
+  <head>
+    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+    
+    <title>Objective-C Automatic Reference Counting (ARC) — Clang 3.7 documentation</title>
+    
+    <link rel="stylesheet" href="_static/haiku.css" type="text/css" />
+    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
+    
+    <script type="text/javascript">
+      var DOCUMENTATION_OPTIONS = {
+        URL_ROOT:    './',
+        VERSION:     '3.7',
+        COLLAPSE_INDEX: false,
+        FILE_SUFFIX: '.html',
+        HAS_SOURCE:  true
+      };
+    </script>
+    <script type="text/javascript" src="_static/jquery.js"></script>
+    <script type="text/javascript" src="_static/underscore.js"></script>
+    <script type="text/javascript" src="_static/doctools.js"></script>
+    <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
+    <link rel="top" title="Clang 3.7 documentation" href="index.html" />
+    <link rel="up" title="Clang Language Extensions" href="LanguageExtensions.html" />
+    <link rel="next" title="Attributes in Clang" href="AttributeReference.html" />
+    <link rel="prev" title="Block Implementation Specification" href="Block-ABI-Apple.html" /> 
+  </head>
+  <body>
+      <div class="header"><h1 class="heading"><a href="index.html">
+          <span>Clang 3.7 documentation</span></a></h1>
+        <h2 class="heading"><span>Objective-C Automatic Reference Counting (ARC)</span></h2>
+      </div>
+      <div class="topnav">
+      
+        <p>
+        «  <a href="Block-ABI-Apple.html">Block Implementation Specification</a>
+          ::  
+        <a class="uplink" href="index.html">Contents</a>
+          ::  
+        <a href="AttributeReference.html">Attributes in Clang</a>  Â»
+        </p>
+
+      </div>
+      <div class="content">
+        
+        
+  <style>
+  .arc-term { font-style: italic; font-weight: bold; }
+  .revision { font-style: italic; }
+  .when-revised { font-weight: bold; font-style: normal; }
+
+  /*
+   * Automatic numbering is described in this article:
+   * http://dev.opera.com/articles/view/automatic-numbering-with-css-counters/
+   */
+  /*
+   * Automatic numbering for the TOC.
+   * This is wrong from the semantics point of view, since it is an ordered
+   * list, but uses "ul" tag.
+   */
+  div#contents.contents.local ul {
+    counter-reset: toc-section;
+    list-style-type: none;
+  }
+  div#contents.contents.local ul li {
+    counter-increment: toc-section;
+    background: none; // Remove bullets
+  }
+  div#contents.contents.local ul li a.reference:before {
+    content: counters(toc-section, ".") " ";
+  }
+
+  /* Automatic numbering for the body. */
+  body {
+    counter-reset: section subsection subsubsection;
+  }
+  .section h2 {
+    counter-reset: subsection subsubsection;
+    counter-increment: section;
+  }
+  .section h2 a.toc-backref:before {
+    content: counter(section) " ";
+  }
+  .section h3 {
+    counter-reset: subsubsection;
+    counter-increment: subsection;
+  }
+  .section h3 a.toc-backref:before {
+    content: counter(section) "." counter(subsection) " ";
+  }
+  .section h4 {
+    counter-increment: subsubsection;
+  }
+  .section h4 a.toc-backref:before {
+    content: counter(section) "." counter(subsection) "." counter(subsubsection) " ";
+  }
+</style><div class="section" id="objective-c-automatic-reference-counting-arc">
+<h1>Objective-C Automatic Reference Counting (ARC)<a class="headerlink" href="#objective-c-automatic-reference-counting-arc" title="Permalink to this headline">¶</a></h1>
+<div class="contents local topic" id="contents">
+<ul class="simple">
+<li><a class="reference internal" href="#about-this-document" id="id4">About this document</a><ul>
+<li><a class="reference internal" href="#purpose" id="id5">Purpose</a></li>
+<li><a class="reference internal" href="#background" id="id6">Background</a></li>
+<li><a class="reference internal" href="#evolution" id="id7">Evolution</a></li>
+</ul>
+</li>
+<li><a class="reference internal" href="#general" id="id8">General</a></li>
+<li><a class="reference internal" href="#retainable-object-pointers" id="id9">Retainable object pointers</a><ul>
+<li><a class="reference internal" href="#retain-count-semantics" id="id10">Retain count semantics</a></li>
+<li><a class="reference internal" href="#retainable-object-pointers-as-operands-and-arguments" id="id11">Retainable object pointers as operands and arguments</a><ul>
+<li><a class="reference internal" href="#consumed-parameters" id="id12">Consumed parameters</a></li>
+<li><a class="reference internal" href="#retained-return-values" id="id13">Retained return values</a></li>
+<li><a class="reference internal" href="#unretained-return-values" id="id14">Unretained return values</a></li>
+<li><a class="reference internal" href="#bridged-casts" id="id15">Bridged casts</a></li>
+</ul>
+</li>
+<li><a class="reference internal" href="#restrictions" id="id16">Restrictions</a><ul>
+<li><a class="reference internal" href="#conversion-of-retainable-object-pointers" id="id17">Conversion of retainable object pointers</a></li>
+<li><a class="reference internal" href="#conversion-to-retainable-object-pointer-type-of-expressions-with-known-semantics" id="id18">Conversion to retainable object pointer type of expressions with known semantics</a></li>
+<li><a class="reference internal" href="#conversion-from-retainable-object-pointer-type-in-certain-contexts" id="id19">Conversion from retainable object pointer type in certain contexts</a></li>
+</ul>
+</li>
+</ul>
+</li>
+<li><a class="reference internal" href="#ownership-qualification" id="id20">Ownership qualification</a><ul>
+<li><a class="reference internal" href="#spelling" id="id21">Spelling</a><ul>
+<li><a class="reference internal" href="#property-declarations" id="id22">Property declarations</a></li>
+</ul>
+</li>
+<li><a class="reference internal" href="#semantics" id="id23">Semantics</a></li>
+<li><a class="reference internal" href="#arc-ownership-restrictions" id="id24">Restrictions</a><ul>
+<li><a class="reference internal" href="#weak-unavailable-types" id="id25">Weak-unavailable types</a></li>
+<li><a class="reference internal" href="#storage-duration-of-autoreleasing-objects" id="id26">Storage duration of <tt class="docutils literal"><span class="pre">__autoreleasing</span></tt> objects</a></li>
+<li><a class="reference internal" href="#conversion-of-pointers-to-ownership-qualified-types" id="id27">Conversion of pointers to ownership-qualified types</a></li>
+<li><a class="reference internal" href="#passing-to-an-out-parameter-by-writeback" id="id28">Passing to an out parameter by writeback</a></li>
+<li><a class="reference internal" href="#ownership-qualified-fields-of-structs-and-unions" id="id29">Ownership-qualified fields of structs and unions</a></li>
+</ul>
+</li>
+<li><a class="reference internal" href="#ownership-inference" id="id30">Ownership inference</a><ul>
+<li><a class="reference internal" href="#objects" id="id31">Objects</a></li>
+<li><a class="reference internal" href="#indirect-parameters" id="id32">Indirect parameters</a></li>
+<li><a class="reference internal" href="#template-arguments" id="id33">Template arguments</a></li>
+</ul>
+</li>
+</ul>
+</li>
+<li><a class="reference internal" href="#method-families" id="id34">Method families</a><ul>
+<li><a class="reference internal" href="#explicit-method-family-control" id="id35">Explicit method family control</a></li>
+<li><a class="reference internal" href="#semantics-of-method-families" id="id36">Semantics of method families</a><ul>
+<li><a class="reference internal" href="#semantics-of-init" id="id37">Semantics of <tt class="docutils literal"><span class="pre">init</span></tt></a></li>
+<li><a class="reference internal" href="#related-result-types" id="id38">Related result types</a></li>
+</ul>
+</li>
+</ul>
+</li>
+<li><a class="reference internal" href="#optimization" id="id39">Optimization</a><ul>
+<li><a class="reference internal" href="#object-liveness" id="id40">Object liveness</a></li>
+<li><a class="reference internal" href="#no-object-lifetime-extension" id="id41">No object lifetime extension</a></li>
+<li><a class="reference internal" href="#precise-lifetime-semantics" id="id42">Precise lifetime semantics</a></li>
+</ul>
+</li>
+<li><a class="reference internal" href="#miscellaneous" id="id43">Miscellaneous</a><ul>
+<li><a class="reference internal" href="#special-methods" id="id44">Special methods</a><ul>
+<li><a class="reference internal" href="#memory-management-methods" id="id45">Memory management methods</a></li>
+<li><a class="reference internal" href="#dealloc" id="id46"><tt class="docutils literal"><span class="pre">dealloc</span></tt></a></li>
+</ul>
+</li>
+<li><a class="reference internal" href="#autoreleasepool" id="id47"><tt class="docutils literal"><span class="pre">@autoreleasepool</span></tt></a></li>
+<li><a class="reference internal" href="#self" id="id48"><tt class="docutils literal"><span class="pre">self</span></tt></a></li>
+<li><a class="reference internal" href="#fast-enumeration-iteration-variables" id="id49">Fast enumeration iteration variables</a></li>
+<li><a class="reference internal" href="#blocks" id="id50">Blocks</a></li>
+<li><a class="reference internal" href="#exceptions" id="id51">Exceptions</a></li>
+<li><a class="reference internal" href="#interior-pointers" id="id52">Interior pointers</a></li>
+<li><a class="reference internal" href="#c-retainable-pointer-types" id="id53">C retainable pointer types</a><ul>
+<li><a class="reference internal" href="#auditing-of-c-retainable-pointer-interfaces" id="id54">Auditing of C retainable pointer interfaces</a></li>
+</ul>
+</li>
+</ul>
+</li>
+<li><a class="reference internal" href="#runtime-support" id="id55">Runtime support</a><ul>
+<li><a class="reference internal" href="#arc-runtime-objc-autorelease" id="id56"><tt class="docutils literal"><span class="pre">id</span> <span class="pre">objc_autorelease(id</span> <span class="pre">value);</span></tt></a></li>
+<li><a class="reference internal" href="#void-objc-autoreleasepoolpop-void-pool" id="id57"><tt class="docutils literal"><span class="pre">void</span> <span class="pre">objc_autoreleasePoolPop(void</span> <span class="pre">*pool);</span></tt></a></li>
+<li><a class="reference internal" href="#void-objc-autoreleasepoolpush-void" id="id58"><tt class="docutils literal"><span class="pre">void</span> <span class="pre">*objc_autoreleasePoolPush(void);</span></tt></a></li>
+<li><a class="reference internal" href="#arc-runtime-objc-autoreleasereturnvalue" id="id59"><tt class="docutils literal"><span class="pre">id</span> <span class="pre">objc_autoreleaseReturnValue(id</span> <span class="pre">value);</span></tt></a></li>
+<li><a class="reference internal" href="#void-objc-copyweak-id-dest-id-src" id="id60"><tt class="docutils literal"><span class="pre">void</span> <span class="pre">objc_copyWeak(id</span> <span class="pre">*dest,</span> <span class="pre">id</span> <span class="pre">*src);</span></tt></a></li>
+<li><a class="reference internal" href="#void-objc-destroyweak-id-object" id="id61"><tt class="docutils literal"><span class="pre">void</span> <span class="pre">objc_destroyWeak(id</span> <span class="pre">*object);</span></tt></a></li>
+<li><a class="reference internal" href="#arc-runtime-objc-initweak" id="id62"><tt class="docutils literal"><span class="pre">id</span> <span class="pre">objc_initWeak(id</span> <span class="pre">*object,</span> <span class="pre">id</span> <span class="pre">value);</span></tt></a></li>
+<li><a class="reference internal" href="#arc-runtime-objc-loadweak" id="id63"><tt class="docutils literal"><span class="pre">id</span> <span class="pre">objc_loadWeak(id</span> <span class="pre">*object);</span></tt></a></li>
+<li><a class="reference internal" href="#arc-runtime-objc-loadweakretained" id="id64"><tt class="docutils literal"><span class="pre">id</span> <span class="pre">objc_loadWeakRetained(id</span> <span class="pre">*object);</span></tt></a></li>
+<li><a class="reference internal" href="#void-objc-moveweak-id-dest-id-src" id="id65"><tt class="docutils literal"><span class="pre">void</span> <span class="pre">objc_moveWeak(id</span> <span class="pre">*dest,</span> <span class="pre">id</span> <span class="pre">*src);</span></tt></a></li>
+<li><a class="reference internal" href="#void-objc-release-id-value" id="id66"><tt class="docutils literal"><span class="pre">void</span> <span class="pre">objc_release(id</span> <span class="pre">value);</span></tt></a></li>
+<li><a class="reference internal" href="#arc-runtime-objc-retain" id="id67"><tt class="docutils literal"><span class="pre">id</span> <span class="pre">objc_retain(id</span> <span class="pre">value);</span></tt></a></li>
+<li><a class="reference internal" href="#arc-runtime-objc-retainautorelease" id="id68"><tt class="docutils literal"><span class="pre">id</span> <span class="pre">objc_retainAutorelease(id</span> <span class="pre">value);</span></tt></a></li>
+<li><a class="reference internal" href="#arc-runtime-objc-retainautoreleasereturnvalue" id="id69"><tt class="docutils literal"><span class="pre">id</span> <span class="pre">objc_retainAutoreleaseReturnValue(id</span> <span class="pre">value);</span></tt></a></li>
+<li><a class="reference internal" href="#arc-runtime-objc-retainautoreleasedreturnvalue" id="id70"><tt class="docutils literal"><span class="pre">id</span> <span class="pre">objc_retainAutoreleasedReturnValue(id</span> <span class="pre">value);</span></tt></a></li>
+<li><a class="reference internal" href="#arc-runtime-objc-retainblock" id="id71"><tt class="docutils literal"><span class="pre">id</span> <span class="pre">objc_retainBlock(id</span> <span class="pre">value);</span></tt></a></li>
+<li><a class="reference internal" href="#arc-runtime-objc-storestrong" id="id72"><tt class="docutils literal"><span class="pre">id</span> <span class="pre">objc_storeStrong(id</span> <span class="pre">*object,</span> <span class="pre">id</span> <span class="pre">value);</span></tt></a></li>
+<li><a class="reference internal" href="#arc-runtime-objc-storeweak" id="id73"><tt class="docutils literal"><span class="pre">id</span> <span class="pre">objc_storeWeak(id</span> <span class="pre">*object,</span> <span class="pre">id</span> <span class="pre">value);</span></tt></a></li>
+</ul>
+</li>
+</ul>
+</div>
+<div class="section" id="about-this-document">
+<span id="arc-meta"></span><h2><a class="toc-backref" href="#id4">About this document</a><a class="headerlink" href="#about-this-document" title="Permalink to this headline">¶</a></h2>
+<div class="section" id="purpose">
+<span id="arc-meta-purpose"></span><h3><a class="toc-backref" href="#id5">Purpose</a><a class="headerlink" href="#purpose" title="Permalink to this headline">¶</a></h3>
+<p>The first and primary purpose of this document is to serve as a complete
+technical specification of Automatic Reference Counting.  Given a core
+Objective-C compiler and runtime, it should be possible to write a compiler and
+runtime which implements these new semantics.</p>
+<p>The secondary purpose is to act as a rationale for why ARC was designed in this
+way.  This should remain tightly focused on the technical design and should not
+stray into marketing speculation.</p>
+</div>
+<div class="section" id="background">
+<span id="arc-meta-background"></span><h3><a class="toc-backref" href="#id6">Background</a><a class="headerlink" href="#background" title="Permalink to this headline">¶</a></h3>
+<p>This document assumes a basic familiarity with C.</p>
+<p><span class="arc-term">Blocks</span> are a C language extension for creating anonymous functions.
+Users interact with and transfer block objects using <span class="arc-term">block
+pointers</span>, which are represented like a normal pointer.  A block may capture
+values from local variables; when this occurs, memory must be dynamically
+allocated.  The initial allocation is done on the stack, but the runtime
+provides a <tt class="docutils literal"><span class="pre">Block_copy</span></tt> function which, given a block pointer, either copies
+the underlying block object to the heap, setting its reference count to 1 and
+returning the new block pointer, or (if the block object is already on the
+heap) increases its reference count by 1.  The paired function is
+<tt class="docutils literal"><span class="pre">Block_release</span></tt>, which decreases the reference count by 1 and destroys the
+object if the count reaches zero and is on the heap.</p>
+<p>Objective-C is a set of language extensions, significant enough to be
+considered a different language.  It is a strict superset of C.  The extensions
+can also be imposed on C++, producing a language called Objective-C++.  The
+primary feature is a single-inheritance object system; we briefly describe the
+modern dialect.</p>
+<p>Objective-C defines a new type kind, collectively called the <span class="arc-term">object
+pointer types</span>.  This kind has two notable builtin members, <tt class="docutils literal"><span class="pre">id</span></tt> and
+<tt class="docutils literal"><span class="pre">Class</span></tt>; <tt class="docutils literal"><span class="pre">id</span></tt> is the final supertype of all object pointers.  The validity
+of conversions between object pointer types is not checked at runtime.  Users
+may define <span class="arc-term">classes</span>; each class is a type, and the pointer to that
+type is an object pointer type.  A class may have a superclass; its pointer
+type is a subtype of its superclass’s pointer type.  A class has a set of
+<span class="arc-term">ivars</span>, fields which appear on all instances of that class.  For
+every class <em>T</em> there’s an associated metaclass; it has no fields, its
+superclass is the metaclass of <em>T</em>‘s superclass, and its metaclass is a global
+class.  Every class has a global object whose class is the class’s metaclass;
+metaclasses have no associated type, so pointers to this object have type
+<tt class="docutils literal"><span class="pre">Class</span></tt>.</p>
+<p>A class declaration (<tt class="docutils literal"><span class="pre">@interface</span></tt>) declares a set of <span class="arc-term">methods</span>.  A
+method has a return type, a list of argument types, and a <span class="arc-term">selector</span>:
+a name like <tt class="docutils literal"><span class="pre">foo:bar:baz:</span></tt>, where the number of colons corresponds to the
+number of formal arguments.  A method may be an instance method, in which case
+it can be invoked on objects of the class, or a class method, in which case it
+can be invoked on objects of the metaclass.  A method may be invoked by
+providing an object (called the <span class="arc-term">receiver</span>) and a list of formal
+arguments interspersed with the selector, like so:</p>
+<div class="highlight-objc"><div class="highlight"><pre><span class="p">[</span><span class="n">receiver</span> <span class="n">foo</span><span class="o">:</span> <span class="n">fooArg</span> <span class="n">bar</span><span class="o">:</span> <span class="n">barArg</span> <span class="n">baz</span><span class="o">:</span> <span class="n">bazArg</span><span class="p">]</span>
+</pre></div>
+</div>
+<p>This looks in the dynamic class of the receiver for a method with this name,
+then in that class’s superclass, etc., until it finds something it can execute.
+The receiver “expression” may also be the name of a class, in which case the
+actual receiver is the class object for that class, or (within method
+definitions) it may be <tt class="docutils literal"><span class="pre">super</span></tt>, in which case the lookup algorithm starts
+with the static superclass instead of the dynamic class.  The actual methods
+dynamically found in a class are not those declared in the <tt class="docutils literal"><span class="pre">@interface</span></tt>, but
+those defined in a separate <tt class="docutils literal"><span class="pre">@implementation</span></tt> declaration; however, when
+compiling a call, typechecking is done based on the methods declared in the
+<tt class="docutils literal"><span class="pre">@interface</span></tt>.</p>
+<p>Method declarations may also be grouped into <span class="arc-term">protocols</span>, which are not
+inherently associated with any class, but which classes may claim to follow.
+Object pointer types may be qualified with additional protocols that the object
+is known to support.</p>
+<p><span class="arc-term">Class extensions</span> are collections of ivars and methods, designed to
+allow a class’s <tt class="docutils literal"><span class="pre">@interface</span></tt> to be split across multiple files; however,
+there is still a primary implementation file which must see the
+<tt class="docutils literal"><span class="pre">@interface</span></tt>s of all class extensions.  <span class="arc-term">Categories</span> allow
+methods (but not ivars) to be declared <em>post hoc</em> on an arbitrary class; the
+methods in the category’s <tt class="docutils literal"><span class="pre">@implementation</span></tt> will be dynamically added to that
+class’s method tables which the category is loaded at runtime, replacing those
+methods in case of a collision.</p>
+<p>In the standard environment, objects are allocated on the heap, and their
+lifetime is manually managed using a reference count.  This is done using two
+instance methods which all classes are expected to implement: <tt class="docutils literal"><span class="pre">retain</span></tt>
+increases the object’s reference count by 1, whereas <tt class="docutils literal"><span class="pre">release</span></tt> decreases it
+by 1 and calls the instance method <tt class="docutils literal"><span class="pre">dealloc</span></tt> if the count reaches 0.  To
+simplify certain operations, there is also an <span class="arc-term">autorelease pool</span>, a
+thread-local list of objects to call <tt class="docutils literal"><span class="pre">release</span></tt> on later; an object can be
+added to this pool by calling <tt class="docutils literal"><span class="pre">autorelease</span></tt> on it.</p>
+<p>Block pointers may be converted to type <tt class="docutils literal"><span class="pre">id</span></tt>; block objects are laid out in a
+way that makes them compatible with Objective-C objects.  There is a builtin
+class that all block objects are considered to be objects of; this class
+implements <tt class="docutils literal"><span class="pre">retain</span></tt> by adjusting the reference count, not by calling
+<tt class="docutils literal"><span class="pre">Block_copy</span></tt>.</p>
+</div>
+<div class="section" id="evolution">
+<span id="arc-meta-evolution"></span><h3><a class="toc-backref" href="#id7">Evolution</a><a class="headerlink" href="#evolution" title="Permalink to this headline">¶</a></h3>
+<p>ARC is under continual evolution, and this document must be updated as the
+language progresses.</p>
+<p>If a change increases the expressiveness of the language, for example by
+lifting a restriction or by adding new syntax, the change will be annotated
+with a revision marker, like so:</p>
+<blockquote>
+<div>ARC applies to Objective-C pointer types, block pointer types, and
+<span class="when-revised">[beginning Apple 8.0, LLVM 3.8]</span> <span class="revision">BPTRs declared
+within</span> <tt class="docutils literal"><span class="pre">extern</span> <span class="pre">"BCPL"</span></tt> blocks.</div></blockquote>
+<p>For now, it is sensible to version this document by the releases of its sole
+implementation (and its host project), clang.  “LLVM X.Y” refers to an
+open-source release of clang from the LLVM project.  “Apple X.Y” refers to an
+Apple-provided release of the Apple LLVM Compiler.  Other organizations that
+prepare their own, separately-versioned clang releases and wish to maintain
+similar information in this document should send requests to cfe-dev.</p>
+<p>If a change decreases the expressiveness of the language, for example by
+imposing a new restriction, this should be taken as an oversight in the
+original specification and something to be avoided in all versions.  Such
+changes are generally to be avoided.</p>
+</div>
+</div>
+<div class="section" id="general">
+<span id="arc-general"></span><h2><a class="toc-backref" href="#id8">General</a><a class="headerlink" href="#general" title="Permalink to this headline">¶</a></h2>
+<p>Automatic Reference Counting implements automatic memory management for
+Objective-C objects and blocks, freeing the programmer from the need to
+explicitly insert retains and releases.  It does not provide a cycle collector;
+users must explicitly manage the lifetime of their objects, breaking cycles
+manually or with weak or unsafe references.</p>
+<p>ARC may be explicitly enabled with the compiler flag <tt class="docutils literal"><span class="pre">-fobjc-arc</span></tt>.  It may
+also be explicitly disabled with the compiler flag <tt class="docutils literal"><span class="pre">-fno-objc-arc</span></tt>.  The last
+of these two flags appearing on the compile line “wins”.</p>
+<p>If ARC is enabled, <tt class="docutils literal"><span class="pre">__has_feature(objc_arc)</span></tt> will expand to 1 in the
+preprocessor.  For more information about <tt class="docutils literal"><span class="pre">__has_feature</span></tt>, see the
+<a class="reference internal" href="LanguageExtensions.html#langext-has-feature-has-extension"><em>language extensions</em></a> document.</p>
+</div>
+<div class="section" id="retainable-object-pointers">
+<span id="arc-objects"></span><h2><a class="toc-backref" href="#id9">Retainable object pointers</a><a class="headerlink" href="#retainable-object-pointers" title="Permalink to this headline">¶</a></h2>
+<p>This section describes retainable object pointers, their basic operations, and
+the restrictions imposed on their use under ARC.  Note in particular that it
+covers the rules for pointer <em>values</em> (patterns of bits indicating the location
+of a pointed-to object), not pointer <em>objects</em> (locations in memory which store
+pointer values).  The rules for objects are covered in the next section.</p>
+<p>A <span class="arc-term">retainable object pointer</span> (or “retainable pointer”) is a value of
+a <span class="arc-term">retainable object pointer type</span> (“retainable type”).  There are
+three kinds of retainable object pointer types:</p>
+<ul class="simple">
+<li>block pointers (formed by applying the caret (<tt class="docutils literal"><span class="pre">^</span></tt>) declarator sigil to a
+function type)</li>
+<li>Objective-C object pointers (<tt class="docutils literal"><span class="pre">id</span></tt>, <tt class="docutils literal"><span class="pre">Class</span></tt>, <tt class="docutils literal"><span class="pre">NSFoo*</span></tt>, etc.)</li>
+<li>typedefs marked with <tt class="docutils literal"><span class="pre">__attribute__((NSObject))</span></tt></li>
+</ul>
+<p>Other pointer types, such as <tt class="docutils literal"><span class="pre">int*</span></tt> and <tt class="docutils literal"><span class="pre">CFStringRef</span></tt>, are not subject to
+ARC’s semantics and restrictions.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p>We are not at liberty to require all code to be recompiled with ARC;
+therefore, ARC must interoperate with Objective-C code which manages retains
+and releases manually.  In general, there are three requirements in order for
+a compiler-supported reference-count system to provide reliable
+interoperation:</p>
+<ul class="last simple">
+<li>The type system must reliably identify which objects are to be managed.  An
+<tt class="docutils literal"><span class="pre">int*</span></tt> might be a pointer to a <tt class="docutils literal"><span class="pre">malloc</span></tt>‘ed array, or it might be an
+interior pointer to such an array, or it might point to some field or local
+variable.  In contrast, values of the retainable object pointer types are
+never interior.</li>
+<li>The type system must reliably indicate how to manage objects of a type.
+This usually means that the type must imply a procedure for incrementing
+and decrementing retain counts.  Supporting single-ownership objects
+requires a lot more explicit mediation in the language.</li>
+<li>There must be reliable conventions for whether and when “ownership” is
+passed between caller and callee, for both arguments and return values.
+Objective-C methods follow such a convention very reliably, at least for
+system libraries on Mac OS X, and functions always pass objects at +0.  The
+C-based APIs for Core Foundation objects, on the other hand, have much more
+varied transfer semantics.</li>
+</ul>
+</div>
+<p>The use of <tt class="docutils literal"><span class="pre">__attribute__((NSObject))</span></tt> typedefs is not recommended.  If it’s
+absolutely necessary to use this attribute, be very explicit about using the
+typedef, and do not assume that it will be preserved by language features like
+<tt class="docutils literal"><span class="pre">__typeof</span></tt> and C++ template argument substitution.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">Any compiler operation which incidentally strips type “sugar” from a type
+will yield a type without the attribute, which may result in unexpected
+behavior.</p>
+</div>
+<div class="section" id="retain-count-semantics">
+<span id="arc-objects-retains"></span><h3><a class="toc-backref" href="#id10">Retain count semantics</a><a class="headerlink" href="#retain-count-semantics" title="Permalink to this headline">¶</a></h3>
+<p>A retainable object pointer is either a <span class="arc-term">null pointer</span> or a pointer
+to a valid object.  Furthermore, if it has block pointer type and is not
+<tt class="docutils literal"><span class="pre">null</span></tt> then it must actually be a pointer to a block object, and if it has
+<tt class="docutils literal"><span class="pre">Class</span></tt> type (possibly protocol-qualified) then it must actually be a pointer
+to a class object.  Otherwise ARC does not enforce the Objective-C type system
+as long as the implementing methods follow the signature of the static type.
+It is undefined behavior if ARC is exposed to an invalid pointer.</p>
+<p>For ARC’s purposes, a valid object is one with “well-behaved” retaining
+operations.  Specifically, the object must be laid out such that the
+Objective-C message send machinery can successfully send it the following
+messages:</p>
+<ul class="simple">
+<li><tt class="docutils literal"><span class="pre">retain</span></tt>, taking no arguments and returning a pointer to the object.</li>
+<li><tt class="docutils literal"><span class="pre">release</span></tt>, taking no arguments and returning <tt class="docutils literal"><span class="pre">void</span></tt>.</li>
+<li><tt class="docutils literal"><span class="pre">autorelease</span></tt>, taking no arguments and returning a pointer to the object.</li>
+</ul>
+<p>The behavior of these methods is constrained in the following ways.  The term
+<span class="arc-term">high-level semantics</span> is an intentionally vague term; the intent is
+that programmers must implement these methods in a way such that the compiler,
+modifying code in ways it deems safe according to these constraints, will not
+violate their requirements.  For example, if the user puts logging statements
+in <tt class="docutils literal"><span class="pre">retain</span></tt>, they should not be surprised if those statements are executed
+more or less often depending on optimization settings.  These constraints are
+not exhaustive of the optimization opportunities: values held in local
+variables are subject to additional restrictions, described later in this
+document.</p>
+<p>It is undefined behavior if a computation history featuring a send of
+<tt class="docutils literal"><span class="pre">retain</span></tt> followed by a send of <tt class="docutils literal"><span class="pre">release</span></tt> to the same object, with no
+intervening <tt class="docutils literal"><span class="pre">release</span></tt> on that object, is not equivalent under the high-level
+semantics to a computation history in which these sends are removed.  Note that
+this implies that these methods may not raise exceptions.</p>
+<p>It is undefined behavior if a computation history features any use whatsoever
+of an object following the completion of a send of <tt class="docutils literal"><span class="pre">release</span></tt> that is not
+preceded by a send of <tt class="docutils literal"><span class="pre">retain</span></tt> to the same object.</p>
+<p>The behavior of <tt class="docutils literal"><span class="pre">autorelease</span></tt> must be equivalent to sending <tt class="docutils literal"><span class="pre">release</span></tt> when
+one of the autorelease pools currently in scope is popped.  It may not throw an
+exception.</p>
+<p>When the semantics call for performing one of these operations on a retainable
+object pointer, if that pointer is <tt class="docutils literal"><span class="pre">null</span></tt> then the effect is a no-op.</p>
+<p>All of the semantics described in this document are subject to additional
+<a class="reference internal" href="#arc-optimization"><em>optimization rules</em></a> which permit the removal or
+optimization of operations based on local knowledge of data flow.  The
+semantics describe the high-level behaviors that the compiler implements, not
+an exact sequence of operations that a program will be compiled into.</p>
+</div>
+<div class="section" id="retainable-object-pointers-as-operands-and-arguments">
+<span id="arc-objects-operands"></span><h3><a class="toc-backref" href="#id11">Retainable object pointers as operands and arguments</a><a class="headerlink" href="#retainable-object-pointers-as-operands-and-arguments" title="Permalink to this headline">¶</a></h3>
+<p>In general, ARC does not perform retain or release operations when simply using
+a retainable object pointer as an operand within an expression.  This includes:</p>
+<ul class="simple">
+<li>loading a retainable pointer from an object with non-weak <a class="reference internal" href="#arc-ownership"><em>ownership</em></a>,</li>
+<li>passing a retainable pointer as an argument to a function or method, and</li>
+<li>receiving a retainable pointer as the result of a function or method call.</li>
+</ul>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">While this might seem uncontroversial, it is actually unsafe when multiple
+expressions are evaluated in “parallel”, as with binary operators and calls,
+because (for example) one expression might load from an object while another
+writes to it.  However, C and C++ already call this undefined behavior
+because the evaluations are unsequenced, and ARC simply exploits that here to
+avoid needing to retain arguments across a large number of calls.</p>
+</div>
+<p>The remainder of this section describes exceptions to these rules, how those
+exceptions are detected, and what those exceptions imply semantically.</p>
+<div class="section" id="consumed-parameters">
+<span id="arc-objects-operands-consumed"></span><h4><a class="toc-backref" href="#id12">Consumed parameters</a><a class="headerlink" href="#consumed-parameters" title="Permalink to this headline">¶</a></h4>
+<p>A function or method parameter of retainable object pointer type may be marked
+as <span class="arc-term">consumed</span>, signifying that the callee expects to take ownership
+of a +1 retain count.  This is done by adding the <tt class="docutils literal"><span class="pre">ns_consumed</span></tt> attribute to
+the parameter declaration, like so:</p>
+<div class="highlight-objc"><div class="highlight"><pre><span class="kt">void</span> <span class="nf">foo</span><span class="p">(</span><span class="n">__attribute</span><span class="p">((</span><span class="n">ns_consumed</span><span class="p">))</span> <span class="kt">id</span> <span class="n">x</span><span class="p">);</span>
+<span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="nf">foo:</span> <span class="p">(</span><span class="kt">id</span><span class="p">)</span> <span class="n">__attribute</span><span class="p">((</span><span class="n">ns_consumed</span><span class="p">))</span> <span class="n">x</span><span class="p">;</span>
+</pre></div>
+</div>
+<p>This attribute is part of the type of the function or method, not the type of
+the parameter.  It controls only how the argument is passed and received.</p>
+<p>When passing such an argument, ARC retains the argument prior to making the
+call.</p>
+<p>When receiving such an argument, ARC releases the argument at the end of the
+function, subject to the usual optimizations for local values.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">This formalizes direct transfers of ownership from a caller to a callee.  The
+most common scenario here is passing the <tt class="docutils literal"><span class="pre">self</span></tt> parameter to <tt class="docutils literal"><span class="pre">init</span></tt>, but
+it is useful to generalize.  Typically, local optimization will remove any
+extra retains and releases: on the caller side the retain will be merged with
+a +1 source, and on the callee side the release will be rolled into the
+initialization of the parameter.</p>
+</div>
+<p>The implicit <tt class="docutils literal"><span class="pre">self</span></tt> parameter of a method may be marked as consumed by adding
+<tt class="docutils literal"><span class="pre">__attribute__((ns_consumes_self))</span></tt> to the method declaration.  Methods in
+the <tt class="docutils literal"><span class="pre">init</span></tt> <a class="reference internal" href="#arc-method-families"><em>family</em></a> are treated as if they were
+implicitly marked with this attribute.</p>
+<p>It is undefined behavior if an Objective-C message send to a method with
+<tt class="docutils literal"><span class="pre">ns_consumed</span></tt> parameters (other than self) is made with a null receiver.  It
+is undefined behavior if the method to which an Objective-C message send
+statically resolves to has a different set of <tt class="docutils literal"><span class="pre">ns_consumed</span></tt> parameters than
+the method it dynamically resolves to.  It is undefined behavior if a block or
+function call is made through a static type with a different set of
+<tt class="docutils literal"><span class="pre">ns_consumed</span></tt> parameters than the implementation of the called block or
+function.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">Consumed parameters with null receiver are a guaranteed leak.  Mismatches
+with consumed parameters will cause over-retains or over-releases, depending
+on the direction.  The rule about function calls is really just an
+application of the existing C/C++ rule about calling functions through an
+incompatible function type, but it’s useful to state it explicitly.</p>
+</div>
+</div>
+<div class="section" id="retained-return-values">
+<span id="arc-object-operands-retained-return-values"></span><h4><a class="toc-backref" href="#id13">Retained return values</a><a class="headerlink" href="#retained-return-values" title="Permalink to this headline">¶</a></h4>
+<p>A function or method which returns a retainable object pointer type may be
+marked as returning a retained value, signifying that the caller expects to take
+ownership of a +1 retain count.  This is done by adding the
+<tt class="docutils literal"><span class="pre">ns_returns_retained</span></tt> attribute to the function or method declaration, like
+so:</p>
+<div class="highlight-objc"><div class="highlight"><pre><span class="kt">id</span> <span class="nf">foo</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="n">__attribute</span><span class="p">((</span><span class="n">ns_returns_retained</span><span class="p">));</span>
+<span class="k">-</span> <span class="p">(</span><span class="kt">id</span><span class="p">)</span> <span class="nf">foo</span> <span class="n">__attribute</span><span class="p">((</span><span class="n">ns_returns_retained</span><span class="p">));</span>
+</pre></div>
+</div>
+<p>This attribute is part of the type of the function or method.</p>
+<p>When returning from such a function or method, ARC retains the value at the
+point of evaluation of the return statement, before leaving all local scopes.</p>
+<p>When receiving a return result from such a function or method, ARC releases the
+value at the end of the full-expression it is contained within, subject to the
+usual optimizations for local values.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">This formalizes direct transfers of ownership from a callee to a caller.  The
+most common scenario this models is the retained return from <tt class="docutils literal"><span class="pre">init</span></tt>,
+<tt class="docutils literal"><span class="pre">alloc</span></tt>, <tt class="docutils literal"><span class="pre">new</span></tt>, and <tt class="docutils literal"><span class="pre">copy</span></tt> methods, but there are other cases in the
+frameworks.  After optimization there are typically no extra retains and
+releases required.</p>
+</div>
+<p>Methods in the <tt class="docutils literal"><span class="pre">alloc</span></tt>, <tt class="docutils literal"><span class="pre">copy</span></tt>, <tt class="docutils literal"><span class="pre">init</span></tt>, <tt class="docutils literal"><span class="pre">mutableCopy</span></tt>, and <tt class="docutils literal"><span class="pre">new</span></tt>
+<a class="reference internal" href="#arc-method-families"><em>families</em></a> are implicitly marked
+<tt class="docutils literal"><span class="pre">__attribute__((ns_returns_retained))</span></tt>.  This may be suppressed by explicitly
+marking the method <tt class="docutils literal"><span class="pre">__attribute__((ns_returns_not_retained))</span></tt>.</p>
+<p>It is undefined behavior if the method to which an Objective-C message send
+statically resolves has different retain semantics on its result from the
+method it dynamically resolves to.  It is undefined behavior if a block or
+function call is made through a static type with different retain semantics on
+its result from the implementation of the called block or function.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">Mismatches with returned results will cause over-retains or over-releases,
+depending on the direction.  Again, the rule about function calls is really
+just an application of the existing C/C++ rule about calling functions
+through an incompatible function type.</p>
+</div>
+</div>
+<div class="section" id="unretained-return-values">
+<span id="arc-objects-operands-unretained-returns"></span><h4><a class="toc-backref" href="#id14">Unretained return values</a><a class="headerlink" href="#unretained-return-values" title="Permalink to this headline">¶</a></h4>
+<p>A method or function which returns a retainable object type but does not return
+a retained value must ensure that the object is still valid across the return
+boundary.</p>
+<p>When returning from such a function or method, ARC retains the value at the
+point of evaluation of the return statement, then leaves all local scopes, and
+then balances out the retain while ensuring that the value lives across the
+call boundary.  In the worst case, this may involve an <tt class="docutils literal"><span class="pre">autorelease</span></tt>, but
+callers must not assume that the value is actually in the autorelease pool.</p>
+<p>ARC performs no extra mandatory work on the caller side, although it may elect
+to do something to shorten the lifetime of the returned value.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">It is common in non-ARC code to not return an autoreleased value; therefore
+the convention does not force either path.  It is convenient to not be
+required to do unnecessary retains and autoreleases; this permits
+optimizations such as eliding retain/autoreleases when it can be shown that
+the original pointer will still be valid at the point of return.</p>
+</div>
+<p>A method or function may be marked with
+<tt class="docutils literal"><span class="pre">__attribute__((ns_returns_autoreleased))</span></tt> to indicate that it returns a
+pointer which is guaranteed to be valid at least as long as the innermost
+autorelease pool.  There are no additional semantics enforced in the definition
+of such a method; it merely enables optimizations in callers.</p>
+</div>
+<div class="section" id="bridged-casts">
+<span id="arc-objects-operands-casts"></span><h4><a class="toc-backref" href="#id15">Bridged casts</a><a class="headerlink" href="#bridged-casts" title="Permalink to this headline">¶</a></h4>
+<p>A <span class="arc-term">bridged cast</span> is a C-style cast annotated with one of three
+keywords:</p>
+<ul class="simple">
+<li><tt class="docutils literal"><span class="pre">(__bridge</span> <span class="pre">T)</span> <span class="pre">op</span></tt> casts the operand to the destination type <tt class="docutils literal"><span class="pre">T</span></tt>.  If
+<tt class="docutils literal"><span class="pre">T</span></tt> is a retainable object pointer type, then <tt class="docutils literal"><span class="pre">op</span></tt> must have a
+non-retainable pointer type.  If <tt class="docutils literal"><span class="pre">T</span></tt> is a non-retainable pointer type,
+then <tt class="docutils literal"><span class="pre">op</span></tt> must have a retainable object pointer type.  Otherwise the cast
+is ill-formed.  There is no transfer of ownership, and ARC inserts no retain
+operations.</li>
+<li><tt class="docutils literal"><span class="pre">(__bridge_retained</span> <span class="pre">T)</span> <span class="pre">op</span></tt> casts the operand, which must have retainable
+object pointer type, to the destination type, which must be a non-retainable
+pointer type.  ARC retains the value, subject to the usual optimizations on
+local values, and the recipient is responsible for balancing that +1.</li>
+<li><tt class="docutils literal"><span class="pre">(__bridge_transfer</span> <span class="pre">T)</span> <span class="pre">op</span></tt> casts the operand, which must have
+non-retainable pointer type, to the destination type, which must be a
+retainable object pointer type.  ARC will release the value at the end of
+the enclosing full-expression, subject to the usual optimizations on local
+values.</li>
+</ul>
+<p>These casts are required in order to transfer objects in and out of ARC
+control; see the rationale in the section on <a class="reference internal" href="#arc-objects-restrictions-conversion"><em>conversion of retainable
+object pointers</em></a>.</p>
+<p>Using a <tt class="docutils literal"><span class="pre">__bridge_retained</span></tt> or <tt class="docutils literal"><span class="pre">__bridge_transfer</span></tt> cast purely to convince
+ARC to emit an unbalanced retain or release, respectively, is poor form.</p>
+</div>
+</div>
+<div class="section" id="restrictions">
+<span id="arc-objects-restrictions"></span><h3><a class="toc-backref" href="#id16">Restrictions</a><a class="headerlink" href="#restrictions" title="Permalink to this headline">¶</a></h3>
+<div class="section" id="conversion-of-retainable-object-pointers">
+<span id="arc-objects-restrictions-conversion"></span><h4><a class="toc-backref" href="#id17">Conversion of retainable object pointers</a><a class="headerlink" href="#conversion-of-retainable-object-pointers" title="Permalink to this headline">¶</a></h4>
+<p>In general, a program which attempts to implicitly or explicitly convert a
+value of retainable object pointer type to any non-retainable type, or
+vice-versa, is ill-formed.  For example, an Objective-C object pointer shall
+not be converted to <tt class="docutils literal"><span class="pre">void*</span></tt>.  As an exception, cast to <tt class="docutils literal"><span class="pre">intptr_t</span></tt> is
+allowed because such casts are not transferring ownership.  The <a class="reference internal" href="#arc-objects-operands-casts"><em>bridged
+casts</em></a> may be used to perform these conversions
+where necessary.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">We cannot ensure the correct management of the lifetime of objects if they
+may be freely passed around as unmanaged types.  The bridged casts are
+provided so that the programmer may explicitly describe whether the cast
+transfers control into or out of ARC.</p>
+</div>
+<p>However, the following exceptions apply.</p>
+</div>
+<div class="section" id="conversion-to-retainable-object-pointer-type-of-expressions-with-known-semantics">
+<span id="arc-objects-restrictions-conversion-with-known-semantics"></span><h4><a class="toc-backref" href="#id18">Conversion to retainable object pointer type of expressions with known semantics</a><a class="headerlink" href="#conversion-to-retainable-object-pointer-type-of-expressions-with-known-semantics" title="Permalink to this headline">¶</a></h4>
+<p><span class="when-revised">[beginning Apple 4.0, LLVM 3.1]</span>
+<span class="revision">These exceptions have been greatly expanded; they previously applied
+only to a much-reduced subset which is difficult to categorize but which
+included null pointers, message sends (under the given rules), and the various
+global constants.</span></p>
+<p>An unbridged conversion to a retainable object pointer type from a type other
+than a retainable object pointer type is ill-formed, as discussed above, unless
+the operand of the cast has a syntactic form which is known retained, known
+unretained, or known retain-agnostic.</p>
+<p>An expression is <span class="arc-term">known retain-agnostic</span> if it is:</p>
+<ul class="simple">
+<li>an Objective-C string literal,</li>
+<li>a load from a <tt class="docutils literal"><span class="pre">const</span></tt> system global variable of <a class="reference internal" href="#arc-misc-c-retainable"><em>C retainable pointer
+type</em></a>, or</li>
+<li>a null pointer constant.</li>
+</ul>
+<p>An expression is <span class="arc-term">known unretained</span> if it is an rvalue of <a class="reference internal" href="#arc-misc-c-retainable"><em>C
+retainable pointer type</em></a> and it is:</p>
+<ul class="simple">
+<li>a direct call to a function, and either that function has the
+<tt class="docutils literal"><span class="pre">cf_returns_not_retained</span></tt> attribute or it is an <a class="reference internal" href="#arc-misc-c-retainable-audit"><em>audited</em></a> function that does not have the
+<tt class="docutils literal"><span class="pre">cf_returns_retained</span></tt> attribute and does not follow the create/copy naming
+convention,</li>
+<li>a message send, and the declared method either has the
+<tt class="docutils literal"><span class="pre">cf_returns_not_retained</span></tt> attribute or it has neither the
+<tt class="docutils literal"><span class="pre">cf_returns_retained</span></tt> attribute nor a <a class="reference internal" href="#arc-method-families"><em>selector family</em></a> that implies a retained result, or</li>
+<li><span class="when-revised">[beginning LLVM 3.6]</span> <span class="revision">a load from a</span> <tt class="docutils literal"><span class="pre">const</span></tt>
+<span class="revision">non-system global variable.</span></li>
+</ul>
+<p>An expression is <span class="arc-term">known retained</span> if it is an rvalue of <a class="reference internal" href="#arc-misc-c-retainable"><em>C
+retainable pointer type</em></a> and it is:</p>
+<ul class="simple">
+<li>a message send, and the declared method either has the
+<tt class="docutils literal"><span class="pre">cf_returns_retained</span></tt> attribute, or it does not have the
+<tt class="docutils literal"><span class="pre">cf_returns_not_retained</span></tt> attribute but it does have a <a class="reference internal" href="#arc-method-families"><em>selector
+family</em></a> that implies a retained result.</li>
+</ul>
+<p>Furthermore:</p>
+<ul class="simple">
+<li>a comma expression is classified according to its right-hand side,</li>
+<li>a statement expression is classified according to its result expression, if
+it has one,</li>
+<li>an lvalue-to-rvalue conversion applied to an Objective-C property lvalue is
+classified according to the underlying message send, and</li>
+<li>a conditional operator is classified according to its second and third
+operands, if they agree in classification, or else the other if one is known
+retain-agnostic.</li>
+</ul>
+<p>If the cast operand is known retained, the conversion is treated as a
+<tt class="docutils literal"><span class="pre">__bridge_transfer</span></tt> cast.  If the cast operand is known unretained or known
+retain-agnostic, the conversion is treated as a <tt class="docutils literal"><span class="pre">__bridge</span></tt> cast.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p>Bridging casts are annoying.  Absent the ability to completely automate the
+management of CF objects, however, we are left with relatively poor attempts
+to reduce the need for a glut of explicit bridges.  Hence these rules.</p>
+<p>We’ve so far consciously refrained from implicitly turning retained CF
+results from function calls into <tt class="docutils literal"><span class="pre">__bridge_transfer</span></tt> casts.  The worry is
+that some code patterns  —  for example, creating a CF value, assigning it
+to an ObjC-typed local, and then calling <tt class="docutils literal"><span class="pre">CFRelease</span></tt> when done  —  are a
+bit too likely to be accidentally accepted, leading to mysterious behavior.</p>
+<p class="last">For loads from <tt class="docutils literal"><span class="pre">const</span></tt> global variables of <a class="reference internal" href="#arc-misc-c-retainable"><em>C retainable pointer type</em></a>, it is reasonable to assume that global system
+constants were initialitzed with true constants (e.g. string literals), but
+user constants might have been initialized with something dynamically
+allocated, using a global initializer.</p>
+</div>
+</div>
+<div class="section" id="conversion-from-retainable-object-pointer-type-in-certain-contexts">
+<span id="arc-objects-restrictions-conversion-exception-contextual"></span><h4><a class="toc-backref" href="#id19">Conversion from retainable object pointer type in certain contexts</a><a class="headerlink" href="#conversion-from-retainable-object-pointer-type-in-certain-contexts" title="Permalink to this headline">¶</a></h4>
+<p><span class="when-revised">[beginning Apple 4.0, LLVM 3.1]</span></p>
+<p>If an expression of retainable object pointer type is explicitly cast to a
+<a class="reference internal" href="#arc-misc-c-retainable"><em>C retainable pointer type</em></a>, the program is
+ill-formed as discussed above unless the result is immediately used:</p>
+<ul class="simple">
+<li>to initialize a parameter in an Objective-C message send where the parameter
+is not marked with the <tt class="docutils literal"><span class="pre">cf_consumed</span></tt> attribute, or</li>
+<li>to initialize a parameter in a direct call to an
+<a class="reference internal" href="#arc-misc-c-retainable-audit"><em>audited</em></a> function where the parameter is
+not marked with the <tt class="docutils literal"><span class="pre">cf_consumed</span></tt> attribute.</li>
+</ul>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">Consumed parameters are left out because ARC would naturally balance them
+with a retain, which was judged too treacherous.  This is in part because
+several of the most common consuming functions are in the <tt class="docutils literal"><span class="pre">Release</span></tt> family,
+and it would be quite unfortunate for explicit releases to be silently
+balanced out in this way.</p>
+</div>
+</div>
+</div>
+</div>
+<div class="section" id="ownership-qualification">
+<span id="arc-ownership"></span><h2><a class="toc-backref" href="#id20">Ownership qualification</a><a class="headerlink" href="#ownership-qualification" title="Permalink to this headline">¶</a></h2>
+<p>This section describes the behavior of <em>objects</em> of retainable object pointer
+type; that is, locations in memory which store retainable object pointers.</p>
+<p>A type is a <span class="arc-term">retainable object owner type</span> if it is a retainable
+object pointer type or an array type whose element type is a retainable object
+owner type.</p>
+<p>An <span class="arc-term">ownership qualifier</span> is a type qualifier which applies only to
+retainable object owner types.  An array type is ownership-qualified according
+to its element type, and adding an ownership qualifier to an array type so
+qualifies its element type.</p>
+<p>A program is ill-formed if it attempts to apply an ownership qualifier to a
+type which is already ownership-qualified, even if it is the same qualifier.
+There is a single exception to this rule: an ownership qualifier may be applied
+to a substituted template type parameter, which overrides the ownership
+qualifier provided by the template argument.</p>
+<p>When forming a function type, the result type is adjusted so that any
+top-level ownership qualifier is deleted.</p>
+<p>Except as described under the <a class="reference internal" href="#arc-ownership-inference"><em>inference rules</em></a>,
+a program is ill-formed if it attempts to form a pointer or reference type to a
+retainable object owner type which lacks an ownership qualifier.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">These rules, together with the inference rules, ensure that all objects and
+lvalues of retainable object pointer type have an ownership qualifier.  The
+ability to override an ownership qualifier during template substitution is
+required to counteract the <a class="reference internal" href="#arc-ownership-inference-template-arguments"><em>inference of __strong for template type
+arguments</em></a>.  Ownership qualifiers
+on return types are dropped because they serve no purpose there except to
+cause spurious problems with overloading and templates.</p>
+</div>
+<p>There are four ownership qualifiers:</p>
+<ul class="simple">
+<li><tt class="docutils literal"><span class="pre">__autoreleasing</span></tt></li>
+<li><tt class="docutils literal"><span class="pre">__strong</span></tt></li>
+<li><tt class="docutils literal"><span class="pre">__unsafe_unretained</span></tt></li>
+<li><tt class="docutils literal"><span class="pre">__weak</span></tt></li>
+</ul>
+<p>A type is <span class="arc-term">nontrivially ownership-qualified</span> if it is qualified with
+<tt class="docutils literal"><span class="pre">__autoreleasing</span></tt>, <tt class="docutils literal"><span class="pre">__strong</span></tt>, or <tt class="docutils literal"><span class="pre">__weak</span></tt>.</p>
+<div class="section" id="spelling">
+<span id="arc-ownership-spelling"></span><h3><a class="toc-backref" href="#id21">Spelling</a><a class="headerlink" href="#spelling" title="Permalink to this headline">¶</a></h3>
+<p>The names of the ownership qualifiers are reserved for the implementation.  A
+program may not assume that they are or are not implemented with macros, or
+what those macros expand to.</p>
+<p>An ownership qualifier may be written anywhere that any other type qualifier
+may be written.</p>
+<p>If an ownership qualifier appears in the <em>declaration-specifiers</em>, the
+following rules apply:</p>
+<ul class="simple">
+<li>if the type specifier is a retainable object owner type, the qualifier
+initially applies to that type;</li>
+<li>otherwise, if the outermost non-array declarator is a pointer
+or block pointer declarator, the qualifier initially applies to
+that type;</li>
+<li>otherwise the program is ill-formed.</li>
+<li>If the qualifier is so applied at a position in the declaration
+where the next-innermost declarator is a function declarator, and
+there is an block declarator within that function declarator, then
+the qualifier applies instead to that block declarator and this rule
+is considered afresh beginning from the new position.</li>
+</ul>
+<p>If an ownership qualifier appears on the declarator name, or on the declared
+object, it is applied to the innermost pointer or block-pointer type.</p>
+<p>If an ownership qualifier appears anywhere else in a declarator, it applies to
+the type there.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">Ownership qualifiers are like <tt class="docutils literal"><span class="pre">const</span></tt> and <tt class="docutils literal"><span class="pre">volatile</span></tt> in the sense
+that they may sensibly apply at multiple distinct positions within a
+declarator.  However, unlike those qualifiers, there are many
+situations where they are not meaningful, and so we make an effort
+to “move” the qualifier to a place where it will be meaningful.  The
+general goal is to allow the programmer to write, say, <tt class="docutils literal"><span class="pre">__strong</span></tt>
+before the entire declaration and have it apply in the leftmost
+sensible place.</p>
+</div>
+<div class="section" id="property-declarations">
+<span id="arc-ownership-spelling-property"></span><h4><a class="toc-backref" href="#id22">Property declarations</a><a class="headerlink" href="#property-declarations" title="Permalink to this headline">¶</a></h4>
+<p>A property of retainable object pointer type may have ownership.  If the
+property’s type is ownership-qualified, then the property has that ownership.
+If the property has one of the following modifiers, then the property has the
+corresponding ownership.  A property is ill-formed if it has conflicting
+sources of ownership, or if it has redundant ownership modifiers, or if it has
+<tt class="docutils literal"><span class="pre">__autoreleasing</span></tt> ownership.</p>
+<ul class="simple">
+<li><tt class="docutils literal"><span class="pre">assign</span></tt> implies <tt class="docutils literal"><span class="pre">__unsafe_unretained</span></tt> ownership.</li>
+<li><tt class="docutils literal"><span class="pre">copy</span></tt> implies <tt class="docutils literal"><span class="pre">__strong</span></tt> ownership, as well as the usual behavior of
+copy semantics on the setter.</li>
+<li><tt class="docutils literal"><span class="pre">retain</span></tt> implies <tt class="docutils literal"><span class="pre">__strong</span></tt> ownership.</li>
+<li><tt class="docutils literal"><span class="pre">strong</span></tt> implies <tt class="docutils literal"><span class="pre">__strong</span></tt> ownership.</li>
+<li><tt class="docutils literal"><span class="pre">unsafe_unretained</span></tt> implies <tt class="docutils literal"><span class="pre">__unsafe_unretained</span></tt> ownership.</li>
+<li><tt class="docutils literal"><span class="pre">weak</span></tt> implies <tt class="docutils literal"><span class="pre">__weak</span></tt> ownership.</li>
+</ul>
+<p>With the exception of <tt class="docutils literal"><span class="pre">weak</span></tt>, these modifiers are available in non-ARC
+modes.</p>
+<p>A property’s specified ownership is preserved in its metadata, but otherwise
+the meaning is purely conventional unless the property is synthesized.  If a
+property is synthesized, then the <span class="arc-term">associated instance variable</span> is
+the instance variable which is named, possibly implicitly, by the
+<tt class="docutils literal"><span class="pre">@synthesize</span></tt> declaration.  If the associated instance variable already
+exists, then its ownership qualification must equal the ownership of the
+property; otherwise, the instance variable is created with that ownership
+qualification.</p>
+<p>A property of retainable object pointer type which is synthesized without a
+source of ownership has the ownership of its associated instance variable, if it
+already exists; otherwise, <span class="when-revised">[beginning Apple 3.1, LLVM 3.1]</span>
+<span class="revision">its ownership is implicitly</span> <tt class="docutils literal"><span class="pre">strong</span></tt>.  Prior to this revision, it
+was ill-formed to synthesize such a property.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">Using <tt class="docutils literal"><span class="pre">strong</span></tt> by default is safe and consistent with the generic ARC rule
+about <a class="reference internal" href="#arc-ownership-inference-variables"><em>inferring ownership</em></a>.  It is,
+unfortunately, inconsistent with the non-ARC rule which states that such
+properties are implicitly <tt class="docutils literal"><span class="pre">assign</span></tt>.  However, that rule is clearly
+untenable in ARC, since it leads to default-unsafe code.  The main merit to
+banning the properties is to avoid confusion with non-ARC practice, which did
+not ultimately strike us as sufficient to justify requiring extra syntax and
+(more importantly) forcing novices to understand ownership rules just to
+declare a property when the default is so reasonable.  Changing the rule away
+from non-ARC practice was acceptable because we had conservatively banned the
+synthesis in order to give ourselves exactly this leeway.</p>
+</div>
+<p>Applying <tt class="docutils literal"><span class="pre">__attribute__((NSObject))</span></tt> to a property not of retainable object
+pointer type has the same behavior it does outside of ARC: it requires the
+property type to be some sort of pointer and permits the use of modifiers other
+than <tt class="docutils literal"><span class="pre">assign</span></tt>.  These modifiers only affect the synthesized getter and
+setter; direct accesses to the ivar (even if synthesized) still have primitive
+semantics, and the value in the ivar will not be automatically released during
+deallocation.</p>
+</div>
+</div>
+<div class="section" id="semantics">
+<span id="arc-ownership-semantics"></span><h3><a class="toc-backref" href="#id23">Semantics</a><a class="headerlink" href="#semantics" title="Permalink to this headline">¶</a></h3>
+<p>There are five <span class="arc-term">managed operations</span> which may be performed on an
+object of retainable object pointer type.  Each qualifier specifies different
+semantics for each of these operations.  It is still undefined behavior to
+access an object outside of its lifetime.</p>
+<p>A load or store with “primitive semantics” has the same semantics as the
+respective operation would have on an <tt class="docutils literal"><span class="pre">void*</span></tt> lvalue with the same alignment
+and non-ownership qualification.</p>
+<p><span class="arc-term">Reading</span> occurs when performing a lvalue-to-rvalue conversion on an
+object lvalue.</p>
+<ul class="simple">
+<li>For <tt class="docutils literal"><span class="pre">__weak</span></tt> objects, the current pointee is retained and then released at
+the end of the current full-expression.  This must execute atomically with
+respect to assignments and to the final release of the pointee.</li>
+<li>For all other objects, the lvalue is loaded with primitive semantics.</li>
+</ul>
+<p><span class="arc-term">Assignment</span> occurs when evaluating an assignment operator.  The
+semantics vary based on the qualification:</p>
+<ul class="simple">
+<li>For <tt class="docutils literal"><span class="pre">__strong</span></tt> objects, the new pointee is first retained; second, the
+lvalue is loaded with primitive semantics; third, the new pointee is stored
+into the lvalue with primitive semantics; and finally, the old pointee is
+released.  This is not performed atomically; external synchronization must be
+used to make this safe in the face of concurrent loads and stores.</li>
+<li>For <tt class="docutils literal"><span class="pre">__weak</span></tt> objects, the lvalue is updated to point to the new pointee,
+unless the new pointee is an object currently undergoing deallocation, in
+which case the lvalue is updated to a null pointer.  This must execute
+atomically with respect to other assignments to the object, to reads from the
+object, and to the final release of the new pointee.</li>
+<li>For <tt class="docutils literal"><span class="pre">__unsafe_unretained</span></tt> objects, the new pointee is stored into the
+lvalue using primitive semantics.</li>
+<li>For <tt class="docutils literal"><span class="pre">__autoreleasing</span></tt> objects, the new pointee is retained, autoreleased,
+and stored into the lvalue using primitive semantics.</li>
+</ul>
+<p><span class="arc-term">Initialization</span> occurs when an object’s lifetime begins, which
+depends on its storage duration.  Initialization proceeds in two stages:</p>
+<ol class="arabic simple">
+<li>First, a null pointer is stored into the lvalue using primitive semantics.
+This step is skipped if the object is <tt class="docutils literal"><span class="pre">__unsafe_unretained</span></tt>.</li>
+<li>Second, if the object has an initializer, that expression is evaluated and
+then assigned into the object using the usual assignment semantics.</li>
+</ol>
+<p><span class="arc-term">Destruction</span> occurs when an object’s lifetime ends.  In all cases it
+is semantically equivalent to assigning a null pointer to the object, with the
+proviso that of course the object cannot be legally read after the object’s
+lifetime ends.</p>
+<p><span class="arc-term">Moving</span> occurs in specific situations where an lvalue is “moved
+from”, meaning that its current pointee will be used but the object may be left
+in a different (but still valid) state.  This arises with <tt class="docutils literal"><span class="pre">__block</span></tt> variables
+and rvalue references in C++.  For <tt class="docutils literal"><span class="pre">__strong</span></tt> lvalues, moving is equivalent
+to loading the lvalue with primitive semantics, writing a null pointer to it
+with primitive semantics, and then releasing the result of the load at the end
+of the current full-expression.  For all other lvalues, moving is equivalent to
+reading the object.</p>
+</div>
+<div class="section" id="arc-ownership-restrictions">
+<span id="id1"></span><h3><a class="toc-backref" href="#id24">Restrictions</a><a class="headerlink" href="#arc-ownership-restrictions" title="Permalink to this headline">¶</a></h3>
+<div class="section" id="weak-unavailable-types">
+<span id="arc-ownership-restrictions-weak"></span><h4><a class="toc-backref" href="#id25">Weak-unavailable types</a><a class="headerlink" href="#weak-unavailable-types" title="Permalink to this headline">¶</a></h4>
+<p>It is explicitly permitted for Objective-C classes to not support <tt class="docutils literal"><span class="pre">__weak</span></tt>
+references.  It is undefined behavior to perform an operation with weak
+assignment semantics with a pointer to an Objective-C object whose class does
+not support <tt class="docutils literal"><span class="pre">__weak</span></tt> references.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">Historically, it has been possible for a class to provide its own
+reference-count implementation by overriding <tt class="docutils literal"><span class="pre">retain</span></tt>, <tt class="docutils literal"><span class="pre">release</span></tt>, etc.
+However, weak references to an object require coordination with its class’s
+reference-count implementation because, among other things, weak loads and
+stores must be atomic with respect to the final release.  Therefore, existing
+custom reference-count implementations will generally not support weak
+references without additional effort.  This is unavoidable without breaking
+binary compatibility.</p>
+</div>
+<p>A class may indicate that it does not support weak references by providing the
+<tt class="docutils literal"><span class="pre">objc_arc_weak_unavailable</span></tt> attribute on the class’s interface declaration.  A
+retainable object pointer type is <strong>weak-unavailable</strong> if
+is a pointer to an (optionally protocol-qualified) Objective-C class <tt class="docutils literal"><span class="pre">T</span></tt> where
+<tt class="docutils literal"><span class="pre">T</span></tt> or one of its superclasses has the <tt class="docutils literal"><span class="pre">objc_arc_weak_unavailable</span></tt>
+attribute.  A program is ill-formed if it applies the <tt class="docutils literal"><span class="pre">__weak</span></tt> ownership
+qualifier to a weak-unavailable type or if the value operand of a weak
+assignment operation has a weak-unavailable type.</p>
+</div>
+<div class="section" id="storage-duration-of-autoreleasing-objects">
+<span id="arc-ownership-restrictions-autoreleasing"></span><h4><a class="toc-backref" href="#id26">Storage duration of <tt class="docutils literal"><span class="pre">__autoreleasing</span></tt> objects</a><a class="headerlink" href="#storage-duration-of-autoreleasing-objects" title="Permalink to this headline">¶</a></h4>
+<p>A program is ill-formed if it declares an <tt class="docutils literal"><span class="pre">__autoreleasing</span></tt> object of
+non-automatic storage duration.  A program is ill-formed if it captures an
+<tt class="docutils literal"><span class="pre">__autoreleasing</span></tt> object in a block or, unless by reference, in a C++11
+lambda.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">Autorelease pools are tied to the current thread and scope by their nature.
+While it is possible to have temporary objects whose instance variables are
+filled with autoreleased objects, there is no way that ARC can provide any
+sort of safety guarantee there.</p>
+</div>
+<p>It is undefined behavior if a non-null pointer is assigned to an
+<tt class="docutils literal"><span class="pre">__autoreleasing</span></tt> object while an autorelease pool is in scope and then that
+object is read after the autorelease pool’s scope is left.</p>
+</div>
+<div class="section" id="conversion-of-pointers-to-ownership-qualified-types">
+<span id="arc-ownership-restrictions-conversion-indirect"></span><h4><a class="toc-backref" href="#id27">Conversion of pointers to ownership-qualified types</a><a class="headerlink" href="#conversion-of-pointers-to-ownership-qualified-types" title="Permalink to this headline">¶</a></h4>
+<p>A program is ill-formed if an expression of type <tt class="docutils literal"><span class="pre">T*</span></tt> is converted,
+explicitly or implicitly, to the type <tt class="docutils literal"><span class="pre">U*</span></tt>, where <tt class="docutils literal"><span class="pre">T</span></tt> and <tt class="docutils literal"><span class="pre">U</span></tt> have
+different ownership qualification, unless:</p>
+<ul class="simple">
+<li><tt class="docutils literal"><span class="pre">T</span></tt> is qualified with <tt class="docutils literal"><span class="pre">__strong</span></tt>, <tt class="docutils literal"><span class="pre">__autoreleasing</span></tt>, or
+<tt class="docutils literal"><span class="pre">__unsafe_unretained</span></tt>, and <tt class="docutils literal"><span class="pre">U</span></tt> is qualified with both <tt class="docutils literal"><span class="pre">const</span></tt> and
+<tt class="docutils literal"><span class="pre">__unsafe_unretained</span></tt>; or</li>
+<li>either <tt class="docutils literal"><span class="pre">T</span></tt> or <tt class="docutils literal"><span class="pre">U</span></tt> is <tt class="docutils literal"><span class="pre">cv</span> <span class="pre">void</span></tt>, where <tt class="docutils literal"><span class="pre">cv</span></tt> is an optional sequence
+of non-ownership qualifiers; or</li>
+<li>the conversion is requested with a <tt class="docutils literal"><span class="pre">reinterpret_cast</span></tt> in Objective-C++; or</li>
+<li>the conversion is a well-formed <a class="reference internal" href="#arc-ownership-restrictions-pass-by-writeback"><em>pass-by-writeback</em></a>.</li>
+</ul>
+<p>The analogous rule applies to <tt class="docutils literal"><span class="pre">T&</span></tt> and <tt class="docutils literal"><span class="pre">U&</span></tt> in Objective-C++.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">These rules provide a reasonable level of type-safety for indirect pointers,
+as long as the underlying memory is not deallocated.  The conversion to
+<tt class="docutils literal"><span class="pre">const</span> <span class="pre">__unsafe_unretained</span></tt> is permitted because the semantics of reads are
+equivalent across all these ownership semantics, and that’s a very useful and
+common pattern.  The interconversion with <tt class="docutils literal"><span class="pre">void*</span></tt> is useful for allocating
+memory or otherwise escaping the type system, but use it carefully.
+<tt class="docutils literal"><span class="pre">reinterpret_cast</span></tt> is considered to be an obvious enough sign of taking
+responsibility for any problems.</p>
+</div>
+<p>It is undefined behavior to access an ownership-qualified object through an
+lvalue of a differently-qualified type, except that any non-<tt class="docutils literal"><span class="pre">__weak</span></tt> object
+may be read through an <tt class="docutils literal"><span class="pre">__unsafe_unretained</span></tt> lvalue.</p>
+<p>It is undefined behavior if a managed operation is performed on a <tt class="docutils literal"><span class="pre">__strong</span></tt>
+or <tt class="docutils literal"><span class="pre">__weak</span></tt> object without a guarantee that it contains a primitive zero
+bit-pattern, or if the storage for such an object is freed or reused without the
+object being first assigned a null pointer.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">ARC cannot differentiate between an assignment operator which is intended to
+“initialize” dynamic memory and one which is intended to potentially replace
+a value.  Therefore the object’s pointer must be valid before letting ARC at
+it.  Similarly, C and Objective-C do not provide any language hooks for
+destroying objects held in dynamic memory, so it is the programmer’s
+responsibility to avoid leaks (<tt class="docutils literal"><span class="pre">__strong</span></tt> objects) and consistency errors
+(<tt class="docutils literal"><span class="pre">__weak</span></tt> objects).</p>
+</div>
+<p>These requirements are followed automatically in Objective-C++ when creating
+objects of retainable object owner type with <tt class="docutils literal"><span class="pre">new</span></tt> or <tt class="docutils literal"><span class="pre">new[]</span></tt> and destroying
+them with <tt class="docutils literal"><span class="pre">delete</span></tt>, <tt class="docutils literal"><span class="pre">delete[]</span></tt>, or a pseudo-destructor expression.  Note
+that arrays of nontrivially-ownership-qualified type are not ABI compatible with
+non-ARC code because the element type is non-POD: such arrays that are
+<tt class="docutils literal"><span class="pre">new[]</span></tt>‘d in ARC translation units cannot be <tt class="docutils literal"><span class="pre">delete[]</span></tt>‘d in non-ARC
+translation units and vice-versa.</p>
+</div>
+<div class="section" id="passing-to-an-out-parameter-by-writeback">
+<span id="arc-ownership-restrictions-pass-by-writeback"></span><h4><a class="toc-backref" href="#id28">Passing to an out parameter by writeback</a><a class="headerlink" href="#passing-to-an-out-parameter-by-writeback" title="Permalink to this headline">¶</a></h4>
+<p>If the argument passed to a parameter of type <tt class="docutils literal"><span class="pre">T</span> <span class="pre">__autoreleasing</span> <span class="pre">*</span></tt> has type
+<tt class="docutils literal"><span class="pre">U</span> <span class="pre">oq</span> <span class="pre">*</span></tt>, where <tt class="docutils literal"><span class="pre">oq</span></tt> is an ownership qualifier, then the argument is a
+candidate for <span class="arc-term">pass-by-writeback`</span> if:</p>
+<ul class="simple">
+<li><tt class="docutils literal"><span class="pre">oq</span></tt> is <tt class="docutils literal"><span class="pre">__strong</span></tt> or <tt class="docutils literal"><span class="pre">__weak</span></tt>, and</li>
+<li>it would be legal to initialize a <tt class="docutils literal"><span class="pre">T</span> <span class="pre">__strong</span> <span class="pre">*</span></tt> with a <tt class="docutils literal"><span class="pre">U</span> <span class="pre">__strong</span> <span class="pre">*</span></tt>.</li>
+</ul>
+<p>For purposes of overload resolution, an implicit conversion sequence requiring
+a pass-by-writeback is always worse than an implicit conversion sequence not
+requiring a pass-by-writeback.</p>
+<p>The pass-by-writeback is ill-formed if the argument expression does not have a
+legal form:</p>
+<ul class="simple">
+<li><tt class="docutils literal"><span class="pre">&var</span></tt>, where <tt class="docutils literal"><span class="pre">var</span></tt> is a scalar variable of automatic storage duration
+with retainable object pointer type</li>
+<li>a conditional expression where the second and third operands are both legal
+forms</li>
+<li>a cast whose operand is a legal form</li>
+<li>a null pointer constant</li>
+</ul>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">The restriction in the form of the argument serves two purposes.  First, it
+makes it impossible to pass the address of an array to the argument, which
+serves to protect against an otherwise serious risk of mis-inferring an
+“array” argument as an out-parameter.  Second, it makes it much less likely
+that the user will see confusing aliasing problems due to the implementation,
+below, where their store to the writeback temporary is not immediately seen
+in the original argument variable.</p>
+</div>
+<p>A pass-by-writeback is evaluated as follows:</p>
+<ol class="arabic simple">
+<li>The argument is evaluated to yield a pointer <tt class="docutils literal"><span class="pre">p</span></tt> of type <tt class="docutils literal"><span class="pre">U</span> <span class="pre">oq</span> <span class="pre">*</span></tt>.</li>
+<li>If <tt class="docutils literal"><span class="pre">p</span></tt> is a null pointer, then a null pointer is passed as the argument,
+and no further work is required for the pass-by-writeback.</li>
+<li>Otherwise, a temporary of type <tt class="docutils literal"><span class="pre">T</span> <span class="pre">__autoreleasing</span></tt> is created and
+initialized to a null pointer.</li>
+<li>If the parameter is not an Objective-C method parameter marked <tt class="docutils literal"><span class="pre">out</span></tt>,
+then <tt class="docutils literal"><span class="pre">*p</span></tt> is read, and the result is written into the temporary with
+primitive semantics.</li>
+<li>The address of the temporary is passed as the argument to the actual call.</li>
+<li>After the call completes, the temporary is loaded with primitive
+semantics, and that value is assigned into <tt class="docutils literal"><span class="pre">*p</span></tt>.</li>
+</ol>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">This is all admittedly convoluted.  In an ideal world, we would see that a
+local variable is being passed to an out-parameter and retroactively modify
+its type to be <tt class="docutils literal"><span class="pre">__autoreleasing</span></tt> rather than <tt class="docutils literal"><span class="pre">__strong</span></tt>.  This would be
+remarkably difficult and not always well-founded under the C type system.
+However, it was judged unacceptably invasive to require programmers to write
+<tt class="docutils literal"><span class="pre">__autoreleasing</span></tt> on all the variables they intend to use for
+out-parameters.  This was the least bad solution.</p>
+</div>
+</div>
+<div class="section" id="ownership-qualified-fields-of-structs-and-unions">
+<span id="arc-ownership-restrictions-records"></span><h4><a class="toc-backref" href="#id29">Ownership-qualified fields of structs and unions</a><a class="headerlink" href="#ownership-qualified-fields-of-structs-and-unions" title="Permalink to this headline">¶</a></h4>
+<p>A program is ill-formed if it declares a member of a C struct or union to have
+a nontrivially ownership-qualified type.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">The resulting type would be non-POD in the C++ sense, but C does not give us
+very good language tools for managing the lifetime of aggregates, so it is
+more convenient to simply forbid them.  It is still possible to manage this
+with a <tt class="docutils literal"><span class="pre">void*</span></tt> or an <tt class="docutils literal"><span class="pre">__unsafe_unretained</span></tt> object.</p>
+</div>
+<p>This restriction does not apply in Objective-C++.  However, nontrivally
+ownership-qualified types are considered non-POD: in C++11 terms, they are not
+trivially default constructible, copy constructible, move constructible, copy
+assignable, move assignable, or destructible.  It is a violation of C++’s One
+Definition Rule to use a class outside of ARC that, under ARC, would have a
+nontrivially ownership-qualified member.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">Unlike in C, we can express all the necessary ARC semantics for
+ownership-qualified subobjects as suboperations of the (default) special
+member functions for the class.  These functions then become non-trivial.
+This has the non-obvious result that the class will have a non-trivial copy
+constructor and non-trivial destructor; if this would not normally be true
+outside of ARC, objects of the type will be passed and returned in an
+ABI-incompatible manner.</p>
+</div>
+</div>
+</div>
+<div class="section" id="ownership-inference">
+<span id="arc-ownership-inference"></span><h3><a class="toc-backref" href="#id30">Ownership inference</a><a class="headerlink" href="#ownership-inference" title="Permalink to this headline">¶</a></h3>
+<div class="section" id="objects">
+<span id="arc-ownership-inference-variables"></span><h4><a class="toc-backref" href="#id31">Objects</a><a class="headerlink" href="#objects" title="Permalink to this headline">¶</a></h4>
+<p>If an object is declared with retainable object owner type, but without an
+explicit ownership qualifier, its type is implicitly adjusted to have
+<tt class="docutils literal"><span class="pre">__strong</span></tt> qualification.</p>
+<p>As a special case, if the object’s base type is <tt class="docutils literal"><span class="pre">Class</span></tt> (possibly
+protocol-qualified), the type is adjusted to have <tt class="docutils literal"><span class="pre">__unsafe_unretained</span></tt>
+qualification instead.</p>
+</div>
+<div class="section" id="indirect-parameters">
+<span id="arc-ownership-inference-indirect-parameters"></span><h4><a class="toc-backref" href="#id32">Indirect parameters</a><a class="headerlink" href="#indirect-parameters" title="Permalink to this headline">¶</a></h4>
+<p>If a function or method parameter has type <tt class="docutils literal"><span class="pre">T*</span></tt>, where <tt class="docutils literal"><span class="pre">T</span></tt> is an
+ownership-unqualified retainable object pointer type, then:</p>
+<ul class="simple">
+<li>if <tt class="docutils literal"><span class="pre">T</span></tt> is <tt class="docutils literal"><span class="pre">const</span></tt>-qualified or <tt class="docutils literal"><span class="pre">Class</span></tt>, then it is implicitly
+qualified with <tt class="docutils literal"><span class="pre">__unsafe_unretained</span></tt>;</li>
+<li>otherwise, it is implicitly qualified with <tt class="docutils literal"><span class="pre">__autoreleasing</span></tt>.</li>
+</ul>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last"><tt class="docutils literal"><span class="pre">__autoreleasing</span></tt> exists mostly for this case, the Cocoa convention for
+out-parameters.  Since a pointer to <tt class="docutils literal"><span class="pre">const</span></tt> is obviously not an
+out-parameter, we instead use a type more useful for passing arrays.  If the
+user instead intends to pass in a <em>mutable</em> array, inferring
+<tt class="docutils literal"><span class="pre">__autoreleasing</span></tt> is the wrong thing to do; this directs some of the
+caution in the following rules about writeback.</p>
+</div>
+<p>Such a type written anywhere else would be ill-formed by the general rule
+requiring ownership qualifiers.</p>
+<p>This rule does not apply in Objective-C++ if a parameter’s type is dependent in
+a template pattern and is only <em>instantiated</em> to a type which would be a
+pointer to an unqualified retainable object pointer type.  Such code is still
+ill-formed.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">The convention is very unlikely to be intentional in template code.</p>
+</div>
+</div>
+<div class="section" id="template-arguments">
+<span id="arc-ownership-inference-template-arguments"></span><h4><a class="toc-backref" href="#id33">Template arguments</a><a class="headerlink" href="#template-arguments" title="Permalink to this headline">¶</a></h4>
+<p>If a template argument for a template type parameter is an retainable object
+owner type that does not have an explicit ownership qualifier, it is adjusted
+to have <tt class="docutils literal"><span class="pre">__strong</span></tt> qualification.  This adjustment occurs regardless of
+whether the template argument was deduced or explicitly specified.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last"><tt class="docutils literal"><span class="pre">__strong</span></tt> is a useful default for containers (e.g., <tt class="docutils literal"><span class="pre">std::vector<id></span></tt>),
+which would otherwise require explicit qualification.  Moreover, unqualified
+retainable object pointer types are unlikely to be useful within templates,
+since they generally need to have a qualifier applied to the before being
+used.</p>
+</div>
+</div>
+</div>
+</div>
+<div class="section" id="method-families">
+<span id="arc-method-families"></span><h2><a class="toc-backref" href="#id34">Method families</a><a class="headerlink" href="#method-families" title="Permalink to this headline">¶</a></h2>
+<p>An Objective-C method may fall into a <span class="arc-term">method family</span>, which is a
+conventional set of behaviors ascribed to it by the Cocoa conventions.</p>
+<p>A method is in a certain method family if:</p>
+<ul class="simple">
+<li>it has a <tt class="docutils literal"><span class="pre">objc_method_family</span></tt> attribute placing it in that family; or if
+not that,</li>
+<li>it does not have an <tt class="docutils literal"><span class="pre">objc_method_family</span></tt> attribute placing it in a
+different or no family, and</li>
+<li>its selector falls into the corresponding selector family, and</li>
+<li>its signature obeys the added restrictions of the method family.</li>
+</ul>
+<p>A selector is in a certain selector family if, ignoring any leading
+underscores, the first component of the selector either consists entirely of
+the name of the method family or it begins with that name followed by a
+character other than a lowercase letter.  For example, <tt class="docutils literal"><span class="pre">_perform:with:</span></tt> and
+<tt class="docutils literal"><span class="pre">performWith:</span></tt> would fall into the <tt class="docutils literal"><span class="pre">perform</span></tt> family (if we recognized one),
+but <tt class="docutils literal"><span class="pre">performing:with</span></tt> would not.</p>
+<p>The families and their added restrictions are:</p>
+<ul>
+<li><p class="first"><tt class="docutils literal"><span class="pre">alloc</span></tt> methods must return a retainable object pointer type.</p>
+</li>
+<li><p class="first"><tt class="docutils literal"><span class="pre">copy</span></tt> methods must return a retainable object pointer type.</p>
+</li>
+<li><p class="first"><tt class="docutils literal"><span class="pre">mutableCopy</span></tt> methods must return a retainable object pointer type.</p>
+</li>
+<li><p class="first"><tt class="docutils literal"><span class="pre">new</span></tt> methods must return a retainable object pointer type.</p>
+</li>
+<li><p class="first"><tt class="docutils literal"><span class="pre">init</span></tt> methods must be instance methods and must return an Objective-C
+pointer type.  Additionally, a program is ill-formed if it declares or
+contains a call to an <tt class="docutils literal"><span class="pre">init</span></tt> method whose return type is neither <tt class="docutils literal"><span class="pre">id</span></tt> nor
+a pointer to a super-class or sub-class of the declaring class (if the method
+was declared on a class) or the static receiver type of the call (if it was
+declared on a protocol).</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p>There are a fair number of existing methods with <tt class="docutils literal"><span class="pre">init</span></tt>-like selectors
+which nonetheless don’t follow the <tt class="docutils literal"><span class="pre">init</span></tt> conventions.  Typically these
+are either accidental naming collisions or helper methods called during
+initialization.  Because of the peculiar retain/release behavior of
+<tt class="docutils literal"><span class="pre">init</span></tt> methods, it’s very important not to treat these methods as
+<tt class="docutils literal"><span class="pre">init</span></tt> methods if they aren’t meant to be.  It was felt that implicitly
+defining these methods out of the family based on the exact relationship
+between the return type and the declaring class would be much too subtle
+and fragile.  Therefore we identify a small number of legitimate-seeming
+return types and call everything else an error.  This serves the secondary
+purpose of encouraging programmers not to accidentally give methods names
+in the <tt class="docutils literal"><span class="pre">init</span></tt> family.</p>
+<p class="last">Note that a method with an <tt class="docutils literal"><span class="pre">init</span></tt>-family selector which returns a
+non-Objective-C type (e.g. <tt class="docutils literal"><span class="pre">void</span></tt>) is perfectly well-formed; it simply
+isn’t in the <tt class="docutils literal"><span class="pre">init</span></tt> family.</p>
+</div>
+</li>
+</ul>
+<p>A program is ill-formed if a method’s declarations, implementations, and
+overrides do not all have the same method family.</p>
+<div class="section" id="explicit-method-family-control">
+<span id="arc-family-attribute"></span><h3><a class="toc-backref" href="#id35">Explicit method family control</a><a class="headerlink" href="#explicit-method-family-control" title="Permalink to this headline">¶</a></h3>
+<p>A method may be annotated with the <tt class="docutils literal"><span class="pre">objc_method_family</span></tt> attribute to
+precisely control which method family it belongs to.  If a method in an
+<tt class="docutils literal"><span class="pre">@implementation</span></tt> does not have this attribute, but there is a method
+declared in the corresponding <tt class="docutils literal"><span class="pre">@interface</span></tt> that does, then the attribute is
+copied to the declaration in the <tt class="docutils literal"><span class="pre">@implementation</span></tt>.  The attribute is
+available outside of ARC, and may be tested for with the preprocessor query
+<tt class="docutils literal"><span class="pre">__has_attribute(objc_method_family)</span></tt>.</p>
+<p>The attribute is spelled
+<tt class="docutils literal"><span class="pre">__attribute__((objc_method_family(</span></tt> <em>family</em> <tt class="docutils literal"><span class="pre">)))</span></tt>.  If <em>family</em> is
+<tt class="docutils literal"><span class="pre">none</span></tt>, the method has no family, even if it would otherwise be considered to
+have one based on its selector and type.  Otherwise, <em>family</em> must be one of
+<tt class="docutils literal"><span class="pre">alloc</span></tt>, <tt class="docutils literal"><span class="pre">copy</span></tt>, <tt class="docutils literal"><span class="pre">init</span></tt>, <tt class="docutils literal"><span class="pre">mutableCopy</span></tt>, or <tt class="docutils literal"><span class="pre">new</span></tt>, in which case the
+method is considered to belong to the corresponding family regardless of its
+selector.  It is an error if a method that is explicitly added to a family in
+this way does not meet the requirements of the family other than the selector
+naming convention.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">The rules codified in this document describe the standard conventions of
+Objective-C.  However, as these conventions have not heretofore been enforced
+by an unforgiving mechanical system, they are only imperfectly kept,
+especially as they haven’t always even been precisely defined.  While it is
+possible to define low-level ownership semantics with attributes like
+<tt class="docutils literal"><span class="pre">ns_returns_retained</span></tt>, this attribute allows the user to communicate
+semantic intent, which is of use both to ARC (which, e.g., treats calls to
+<tt class="docutils literal"><span class="pre">init</span></tt> specially) and the static analyzer.</p>
+</div>
+</div>
+<div class="section" id="semantics-of-method-families">
+<span id="arc-family-semantics"></span><h3><a class="toc-backref" href="#id36">Semantics of method families</a><a class="headerlink" href="#semantics-of-method-families" title="Permalink to this headline">¶</a></h3>
+<p>A method’s membership in a method family may imply non-standard semantics for
+its parameters and return type.</p>
+<p>Methods in the <tt class="docutils literal"><span class="pre">alloc</span></tt>, <tt class="docutils literal"><span class="pre">copy</span></tt>, <tt class="docutils literal"><span class="pre">mutableCopy</span></tt>, and <tt class="docutils literal"><span class="pre">new</span></tt> families —
+that is, methods in all the currently-defined families except <tt class="docutils literal"><span class="pre">init</span></tt> —
+implicitly <a class="reference internal" href="#arc-object-operands-retained-return-values"><em>return a retained object</em></a> as if they were annotated with
+the <tt class="docutils literal"><span class="pre">ns_returns_retained</span></tt> attribute.  This can be overridden by annotating
+the method with either of the <tt class="docutils literal"><span class="pre">ns_returns_autoreleased</span></tt> or
+<tt class="docutils literal"><span class="pre">ns_returns_not_retained</span></tt> attributes.</p>
+<p>Properties also follow same naming rules as methods.  This means that those in
+the <tt class="docutils literal"><span class="pre">alloc</span></tt>, <tt class="docutils literal"><span class="pre">copy</span></tt>, <tt class="docutils literal"><span class="pre">mutableCopy</span></tt>, and <tt class="docutils literal"><span class="pre">new</span></tt> families provide access
+to <a class="reference internal" href="#arc-object-operands-retained-return-values"><em>retained objects</em></a>.  This
+can be overridden by annotating the property with <tt class="docutils literal"><span class="pre">ns_returns_not_retained</span></tt>
+attribute.</p>
+<div class="section" id="semantics-of-init">
+<span id="arc-family-semantics-init"></span><h4><a class="toc-backref" href="#id37">Semantics of <tt class="docutils literal"><span class="pre">init</span></tt></a><a class="headerlink" href="#semantics-of-init" title="Permalink to this headline">¶</a></h4>
+<p>Methods in the <tt class="docutils literal"><span class="pre">init</span></tt> family implicitly <a class="reference internal" href="#arc-objects-operands-consumed"><em>consume</em></a> their <tt class="docutils literal"><span class="pre">self</span></tt> parameter and <a class="reference internal" href="#arc-object-operands-retained-return-values"><em>return a
+retained object</em></a>.  Neither of
+these properties can be altered through attributes.</p>
+<p>A call to an <tt class="docutils literal"><span class="pre">init</span></tt> method with a receiver that is either <tt class="docutils literal"><span class="pre">self</span></tt> (possibly
+parenthesized or casted) or <tt class="docutils literal"><span class="pre">super</span></tt> is called a <span class="arc-term">delegate init
+call</span>.  It is an error for a delegate init call to be made except from an
+<tt class="docutils literal"><span class="pre">init</span></tt> method, and excluding blocks within such methods.</p>
+<p>As an exception to the <a class="reference internal" href="#arc-misc-self"><em>usual rule</em></a>, the variable <tt class="docutils literal"><span class="pre">self</span></tt>
+is mutable in an <tt class="docutils literal"><span class="pre">init</span></tt> method and has the usual semantics for a <tt class="docutils literal"><span class="pre">__strong</span></tt>
+variable.  However, it is undefined behavior and the program is ill-formed, no
+diagnostic required, if an <tt class="docutils literal"><span class="pre">init</span></tt> method attempts to use the previous value
+of <tt class="docutils literal"><span class="pre">self</span></tt> after the completion of a delegate init call.  It is conventional,
+but not required, for an <tt class="docutils literal"><span class="pre">init</span></tt> method to return <tt class="docutils literal"><span class="pre">self</span></tt>.</p>
+<p>It is undefined behavior for a program to cause two or more calls to <tt class="docutils literal"><span class="pre">init</span></tt>
+methods on the same object, except that each <tt class="docutils literal"><span class="pre">init</span></tt> method invocation may
+perform at most one delegate init call.</p>
+</div>
+<div class="section" id="related-result-types">
+<span id="arc-family-semantics-result-type"></span><h4><a class="toc-backref" href="#id38">Related result types</a><a class="headerlink" href="#related-result-types" title="Permalink to this headline">¶</a></h4>
+<p>Certain methods are candidates to have <span class="arc-term">related result types</span>:</p>
+<ul class="simple">
+<li>class methods in the <tt class="docutils literal"><span class="pre">alloc</span></tt> and <tt class="docutils literal"><span class="pre">new</span></tt> method families</li>
+<li>instance methods in the <tt class="docutils literal"><span class="pre">init</span></tt> family</li>
+<li>the instance method <tt class="docutils literal"><span class="pre">self</span></tt></li>
+<li>outside of ARC, the instance methods <tt class="docutils literal"><span class="pre">retain</span></tt> and <tt class="docutils literal"><span class="pre">autorelease</span></tt></li>
+</ul>
+<p>If the formal result type of such a method is <tt class="docutils literal"><span class="pre">id</span></tt> or protocol-qualified
+<tt class="docutils literal"><span class="pre">id</span></tt>, or a type equal to the declaring class or a superclass, then it is said
+to have a related result type.  In this case, when invoked in an explicit
+message send, it is assumed to return a type related to the type of the
+receiver:</p>
+<ul class="simple">
+<li>if it is a class method, and the receiver is a class name <tt class="docutils literal"><span class="pre">T</span></tt>, the message
+send expression has type <tt class="docutils literal"><span class="pre">T*</span></tt>; otherwise</li>
+<li>if it is an instance method, and the receiver has type <tt class="docutils literal"><span class="pre">T</span></tt>, the message
+send expression has type <tt class="docutils literal"><span class="pre">T</span></tt>; otherwise</li>
+<li>the message send expression has the normal result type of the method.</li>
+</ul>
+<p>This is a new rule of the Objective-C language and applies outside of ARC.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">ARC’s automatic code emission is more prone than most code to signature
+errors, i.e. errors where a call was emitted against one method signature,
+but the implementing method has an incompatible signature.  Having more
+precise type information helps drastically lower this risk, as well as
+catching a number of latent bugs.</p>
+</div>
+</div>
+</div>
+</div>
+<div class="section" id="optimization">
+<span id="arc-optimization"></span><h2><a class="toc-backref" href="#id39">Optimization</a><a class="headerlink" href="#optimization" title="Permalink to this headline">¶</a></h2>
+<p>Within this section, the word <span class="arc-term">function</span> will be used to
+refer to any structured unit of code, be it a C function, an
+Objective-C method, or a block.</p>
+<p>This specification describes ARC as performing specific <tt class="docutils literal"><span class="pre">retain</span></tt> and
+<tt class="docutils literal"><span class="pre">release</span></tt> operations on retainable object pointers at specific
+points during the execution of a program.  These operations make up a
+non-contiguous subsequence of the computation history of the program.
+The portion of this sequence for a particular retainable object
+pointer for which a specific function execution is directly
+responsible is the <span class="arc-term">formal local retain history</span> of the
+object pointer.  The corresponding actual sequence executed is the
+<cite>dynamic local retain history</cite>.</p>
+<p>However, under certain circumstances, ARC is permitted to re-order and
+eliminate operations in a manner which may alter the overall
+computation history beyond what is permitted by the general “as if”
+rule of C/C++ and the <a class="reference internal" href="#arc-objects-retains"><em>restrictions</em></a> on
+the implementation of <tt class="docutils literal"><span class="pre">retain</span></tt> and <tt class="docutils literal"><span class="pre">release</span></tt>.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p>Specifically, ARC is sometimes permitted to optimize <tt class="docutils literal"><span class="pre">release</span></tt>
+operations in ways which might cause an object to be deallocated
+before it would otherwise be.  Without this, it would be almost
+impossible to eliminate any <tt class="docutils literal"><span class="pre">retain</span></tt>/<tt class="docutils literal"><span class="pre">release</span></tt> pairs.  For
+example, consider the following code:</p>
+<div class="highlight-objc"><div class="highlight"><pre><span class="kt">id</span> <span class="n">x</span> <span class="o">=</span> <span class="n">_ivar</span><span class="p">;</span>
+<span class="p">[</span><span class="n">x</span> <span class="n">foo</span><span class="p">];</span>
+</pre></div>
+</div>
+<p class="last">If we were not permitted in any event to shorten the lifetime of the
+object in <tt class="docutils literal"><span class="pre">x</span></tt>, then we would not be able to eliminate this retain
+and release unless we could prove that the message send could not
+modify <tt class="docutils literal"><span class="pre">_ivar</span></tt> (or deallocate <tt class="docutils literal"><span class="pre">self</span></tt>).  Since message sends are
+opaque to the optimizer, this is not possible, and so ARC’s hands
+would be almost completely tied.</p>
+</div>
+<p>ARC makes no guarantees about the execution of a computation history
+which contains undefined behavior.  In particular, ARC makes no
+guarantees in the presence of race conditions.</p>
+<p>ARC may assume that any retainable object pointers it receives or
+generates are instantaneously valid from that point until a point
+which, by the concurrency model of the host language, happens-after
+the generation of the pointer and happens-before a release of that
+object (possibly via an aliasing pointer or indirectly due to
+destruction of a different object).</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">There is very little point in trying to guarantee correctness in the
+presence of race conditions.  ARC does not have a stack-scanning
+garbage collector, and guaranteeing the atomicity of every load and
+store operation would be prohibitive and preclude a vast amount of
+optimization.</p>
+</div>
+<p>ARC may assume that non-ARC code engages in sensible balancing
+behavior and does not rely on exact or minimum retain count values
+except as guaranteed by <tt class="docutils literal"><span class="pre">__strong</span></tt> object invariants or +1 transfer
+conventions.  For example, if an object is provably double-retained
+and double-released, ARC may eliminate the inner retain and release;
+it does not need to guard against code which performs an unbalanced
+release followed by a “balancing” retain.</p>
+<div class="section" id="object-liveness">
+<span id="arc-optimization-liveness"></span><h3><a class="toc-backref" href="#id40">Object liveness</a><a class="headerlink" href="#object-liveness" title="Permalink to this headline">¶</a></h3>
+<p>ARC may not allow a retainable object <tt class="docutils literal"><span class="pre">X</span></tt> to be deallocated at a
+time <tt class="docutils literal"><span class="pre">T</span></tt> in a computation history if:</p>
+<ul class="simple">
+<li><tt class="docutils literal"><span class="pre">X</span></tt> is the value stored in a <tt class="docutils literal"><span class="pre">__strong</span></tt> object <tt class="docutils literal"><span class="pre">S</span></tt> with
+<a class="reference internal" href="#arc-optimization-precise"><em>precise lifetime semantics</em></a>, or</li>
+<li><tt class="docutils literal"><span class="pre">X</span></tt> is the value stored in a <tt class="docutils literal"><span class="pre">__strong</span></tt> object <tt class="docutils literal"><span class="pre">S</span></tt> with
+imprecise lifetime semantics and, at some point after <tt class="docutils literal"><span class="pre">T</span></tt> but
+before the next store to <tt class="docutils literal"><span class="pre">S</span></tt>, the computation history features a
+load from <tt class="docutils literal"><span class="pre">S</span></tt> and in some way depends on the value loaded, or</li>
+<li><tt class="docutils literal"><span class="pre">X</span></tt> is a value described as being released at the end of the
+current full-expression and, at some point after <tt class="docutils literal"><span class="pre">T</span></tt> but before
+the end of the full-expression, the computation history depends
+on that value.</li>
+</ul>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p>The intent of the second rule is to say that objects held in normal
+<tt class="docutils literal"><span class="pre">__strong</span></tt> local variables may be released as soon as the value in
+the variable is no longer being used: either the variable stops
+being used completely or a new value is stored in the variable.</p>
+<p class="last">The intent of the third rule is to say that return values may be
+released after they’ve been used.</p>
+</div>
+<p>A computation history depends on a pointer value <tt class="docutils literal"><span class="pre">P</span></tt> if it:</p>
+<ul class="simple">
+<li>performs a pointer comparison with <tt class="docutils literal"><span class="pre">P</span></tt>,</li>
+<li>loads from <tt class="docutils literal"><span class="pre">P</span></tt>,</li>
+<li>stores to <tt class="docutils literal"><span class="pre">P</span></tt>,</li>
+<li>depends on a pointer value <tt class="docutils literal"><span class="pre">Q</span></tt> derived via pointer arithmetic
+from <tt class="docutils literal"><span class="pre">P</span></tt> (including an instance-variable or field access), or</li>
+<li>depends on a pointer value <tt class="docutils literal"><span class="pre">Q</span></tt> loaded from <tt class="docutils literal"><span class="pre">P</span></tt>.</li>
+</ul>
+<p>Dependency applies only to values derived directly or indirectly from
+a particular expression result and does not occur merely because a
+separate pointer value dynamically aliases <tt class="docutils literal"><span class="pre">P</span></tt>.  Furthermore, this
+dependency is not carried by values that are stored to objects.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p>The restrictions on dependency are intended to make this analysis
+feasible by an optimizer with only incomplete information about a
+program.  Essentially, dependence is carried to “obvious” uses of a
+pointer.  Merely passing a pointer argument to a function does not
+itself cause dependence, but since generally the optimizer will not
+be able to prove that the function doesn’t depend on that parameter,
+it will be forced to conservatively assume it does.</p>
+<p>Dependency propagates to values loaded from a pointer because those
+values might be invalidated by deallocating the object.  For
+example, given the code <tt class="docutils literal"><span class="pre">__strong</span> <span class="pre">id</span> <span class="pre">x</span> <span class="pre">=</span> <span class="pre">p->ivar;</span></tt>, ARC must not
+move the release of <tt class="docutils literal"><span class="pre">p</span></tt> to between the load of <tt class="docutils literal"><span class="pre">p->ivar</span></tt> and the
+retain of that value for storing into <tt class="docutils literal"><span class="pre">x</span></tt>.</p>
+<p>Dependency does not propagate through stores of dependent pointer
+values because doing so would allow dependency to outlive the
+full-expression which produced the original value.  For example, the
+address of an instance variable could be written to some global
+location and then freely accessed during the lifetime of the local,
+or a function could return an inner pointer of an object and store
+it to a local.  These cases would be potentially impossible to
+reason about and so would basically prevent any optimizations based
+on imprecise lifetime.  There are also uncommon enough to make it
+reasonable to require the precise-lifetime annotation if someone
+really wants to rely on them.</p>
+<p class="last">Dependency does propagate through return values of pointer type.
+The compelling source of need for this rule is a property accessor
+which returns an un-autoreleased result; the calling function must
+have the chance to operate on the value, e.g. to retain it, before
+ARC releases the original pointer.  Note again, however, that
+dependence does not survive a store, so ARC does not guarantee the
+continued validity of the return value past the end of the
+full-expression.</p>
+</div>
+</div>
+<div class="section" id="no-object-lifetime-extension">
+<span id="arc-optimization-object-lifetime"></span><h3><a class="toc-backref" href="#id41">No object lifetime extension</a><a class="headerlink" href="#no-object-lifetime-extension" title="Permalink to this headline">¶</a></h3>
+<p>If, in the formal computation history of the program, an object <tt class="docutils literal"><span class="pre">X</span></tt>
+has been deallocated by the time of an observable side-effect, then
+ARC must cause <tt class="docutils literal"><span class="pre">X</span></tt> to be deallocated by no later than the occurrence
+of that side-effect, except as influenced by the re-ordering of the
+destruction of objects.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p>This rule is intended to prohibit ARC from observably extending the
+lifetime of a retainable object, other than as specified in this
+document.  Together with the rule limiting the transformation of
+releases, this rule requires ARC to eliminate retains and release
+only in pairs.</p>
+<p class="last">ARC’s power to reorder the destruction of objects is critical to its
+ability to do any optimization, for essentially the same reason that
+it must retain the power to decrease the lifetime of an object.
+Unfortunately, while it’s generally poor style for the destruction
+of objects to have arbitrary side-effects, it’s certainly possible.
+Hence the caveat.</p>
+</div>
+</div>
+<div class="section" id="precise-lifetime-semantics">
+<span id="arc-optimization-precise"></span><h3><a class="toc-backref" href="#id42">Precise lifetime semantics</a><a class="headerlink" href="#precise-lifetime-semantics" title="Permalink to this headline">¶</a></h3>
+<p>In general, ARC maintains an invariant that a retainable object pointer held in
+a <tt class="docutils literal"><span class="pre">__strong</span></tt> object will be retained for the full formal lifetime of the
+object.  Objects subject to this invariant have <span class="arc-term">precise lifetime
+semantics</span>.</p>
+<p>By default, local variables of automatic storage duration do not have precise
+lifetime semantics.  Such objects are simply strong references which hold
+values of retainable object pointer type, and these values are still fully
+subject to the optimizations on values under local control.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">Applying these precise-lifetime semantics strictly would be prohibitive.
+Many useful optimizations that might theoretically decrease the lifetime of
+an object would be rendered impossible.  Essentially, it promises too much.</p>
+</div>
+<p>A local variable of retainable object owner type and automatic storage duration
+may be annotated with the <tt class="docutils literal"><span class="pre">objc_precise_lifetime</span></tt> attribute to indicate that
+it should be considered to be an object with precise lifetime semantics.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">Nonetheless, it is sometimes useful to be able to force an object to be
+released at a precise time, even if that object does not appear to be used.
+This is likely to be uncommon enough that the syntactic weight of explicitly
+requesting these semantics will not be burdensome, and may even make the code
+clearer.</p>
+</div>
+</div>
+</div>
+<div class="section" id="miscellaneous">
+<span id="arc-misc"></span><h2><a class="toc-backref" href="#id43">Miscellaneous</a><a class="headerlink" href="#miscellaneous" title="Permalink to this headline">¶</a></h2>
+<div class="section" id="special-methods">
+<span id="arc-misc-special-methods"></span><h3><a class="toc-backref" href="#id44">Special methods</a><a class="headerlink" href="#special-methods" title="Permalink to this headline">¶</a></h3>
+<div class="section" id="memory-management-methods">
+<span id="arc-misc-special-methods-retain"></span><h4><a class="toc-backref" href="#id45">Memory management methods</a><a class="headerlink" href="#memory-management-methods" title="Permalink to this headline">¶</a></h4>
+<p>A program is ill-formed if it contains a method definition, message send, or
+<tt class="docutils literal"><span class="pre">@selector</span></tt> expression for any of the following selectors:</p>
+<ul class="simple">
+<li><tt class="docutils literal"><span class="pre">autorelease</span></tt></li>
+<li><tt class="docutils literal"><span class="pre">release</span></tt></li>
+<li><tt class="docutils literal"><span class="pre">retain</span></tt></li>
+<li><tt class="docutils literal"><span class="pre">retainCount</span></tt></li>
+</ul>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p><tt class="docutils literal"><span class="pre">retainCount</span></tt> is banned because ARC robs it of consistent semantics.  The
+others were banned after weighing three options for how to deal with message
+sends:</p>
+<p><strong>Honoring</strong> them would work out very poorly if a programmer naively or
+accidentally tried to incorporate code written for manual retain/release code
+into an ARC program.  At best, such code would do twice as much work as
+necessary; quite frequently, however, ARC and the explicit code would both
+try to balance the same retain, leading to crashes.  The cost is losing the
+ability to perform “unrooted” retains, i.e. retains not logically
+corresponding to a strong reference in the object graph.</p>
+<p><strong>Ignoring</strong> them would badly violate user expectations about their code.
+While it <em>would</em> make it easier to develop code simultaneously for ARC and
+non-ARC, there is very little reason to do so except for certain library
+developers.  ARC and non-ARC translation units share an execution model and
+can seamlessly interoperate.  Within a translation unit, a developer who
+faithfully maintains their code in non-ARC mode is suffering all the
+restrictions of ARC for zero benefit, while a developer who isn’t testing the
+non-ARC mode is likely to be unpleasantly surprised if they try to go back to
+it.</p>
+<p><strong>Banning</strong> them has the disadvantage of making it very awkward to migrate
+existing code to ARC.  The best answer to that, given a number of other
+changes and restrictions in ARC, is to provide a specialized tool to assist
+users in that migration.</p>
+<p class="last">Implementing these methods was banned because they are too integral to the
+semantics of ARC; many tricks which worked tolerably under manual reference
+counting will misbehave if ARC performs an ephemeral extra retain or two.  If
+absolutely required, it is still possible to implement them in non-ARC code,
+for example in a category; the implementations must obey the <a class="reference internal" href="#arc-objects-retains"><em>semantics</em></a> laid out elsewhere in this document.</p>
+</div>
+</div>
+<div class="section" id="dealloc">
+<span id="arc-misc-special-methods-dealloc"></span><h4><a class="toc-backref" href="#id46"><tt class="docutils literal"><span class="pre">dealloc</span></tt></a><a class="headerlink" href="#dealloc" title="Permalink to this headline">¶</a></h4>
+<p>A program is ill-formed if it contains a message send or <tt class="docutils literal"><span class="pre">@selector</span></tt>
+expression for the selector <tt class="docutils literal"><span class="pre">dealloc</span></tt>.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">There are no legitimate reasons to call <tt class="docutils literal"><span class="pre">dealloc</span></tt> directly.</p>
+</div>
+<p>A class may provide a method definition for an instance method named
+<tt class="docutils literal"><span class="pre">dealloc</span></tt>.  This method will be called after the final <tt class="docutils literal"><span class="pre">release</span></tt> of the
+object but before it is deallocated or any of its instance variables are
+destroyed.  The superclass’s implementation of <tt class="docutils literal"><span class="pre">dealloc</span></tt> will be called
+automatically when the method returns.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">Even though ARC destroys instance variables automatically, there are still
+legitimate reasons to write a <tt class="docutils literal"><span class="pre">dealloc</span></tt> method, such as freeing
+non-retainable resources.  Failing to call <tt class="docutils literal"><span class="pre">[super</span> <span class="pre">dealloc]</span></tt> in such a
+method is nearly always a bug.  Sometimes, the object is simply trying to
+prevent itself from being destroyed, but <tt class="docutils literal"><span class="pre">dealloc</span></tt> is really far too late
+for the object to be raising such objections.  Somewhat more legitimately, an
+object may have been pool-allocated and should not be deallocated with
+<tt class="docutils literal"><span class="pre">free</span></tt>; for now, this can only be supported with a <tt class="docutils literal"><span class="pre">dealloc</span></tt>
+implementation outside of ARC.  Such an implementation must be very careful
+to do all the other work that <tt class="docutils literal"><span class="pre">NSObject</span></tt>‘s <tt class="docutils literal"><span class="pre">dealloc</span></tt> would, which is
+outside the scope of this document to describe.</p>
+</div>
+<p>The instance variables for an ARC-compiled class will be destroyed at some
+point after control enters the <tt class="docutils literal"><span class="pre">dealloc</span></tt> method for the root class of the
+class.  The ordering of the destruction of instance variables is unspecified,
+both within a single class and between subclasses and superclasses.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p>The traditional, non-ARC pattern for destroying instance variables is to
+destroy them immediately before calling <tt class="docutils literal"><span class="pre">[super</span> <span class="pre">dealloc]</span></tt>.  Unfortunately,
+message sends from the superclass are quite capable of reaching methods in
+the subclass, and those methods may well read or write to those instance
+variables.  Making such message sends from dealloc is generally discouraged,
+since the subclass may well rely on other invariants that were broken during
+<tt class="docutils literal"><span class="pre">dealloc</span></tt>, but it’s not so inescapably dangerous that we felt comfortable
+calling it undefined behavior.  Therefore we chose to delay destroying the
+instance variables to a point at which message sends are clearly disallowed:
+the point at which the root class’s deallocation routines take over.</p>
+<p class="last">In most code, the difference is not observable.  It can, however, be observed
+if an instance variable holds a strong reference to an object whose
+deallocation will trigger a side-effect which must be carefully ordered with
+respect to the destruction of the super class.  Such code violates the design
+principle that semantically important behavior should be explicit.  A simple
+fix is to clear the instance variable manually during <tt class="docutils literal"><span class="pre">dealloc</span></tt>; a more
+holistic solution is to move semantically important side-effects out of
+<tt class="docutils literal"><span class="pre">dealloc</span></tt> and into a separate teardown phase which can rely on working with
+well-formed objects.</p>
+</div>
+</div>
+</div>
+<div class="section" id="autoreleasepool">
+<span id="arc-misc-autoreleasepool"></span><h3><a class="toc-backref" href="#id47"><tt class="docutils literal"><span class="pre">@autoreleasepool</span></tt></a><a class="headerlink" href="#autoreleasepool" title="Permalink to this headline">¶</a></h3>
+<p>To simplify the use of autorelease pools, and to bring them under the control
+of the compiler, a new kind of statement is available in Objective-C.  It is
+written <tt class="docutils literal"><span class="pre">@autoreleasepool</span></tt> followed by a <em>compound-statement</em>, i.e.  by a new
+scope delimited by curly braces.  Upon entry to this block, the current state
+of the autorelease pool is captured.  When the block is exited normally,
+whether by fallthrough or directed control flow (such as <tt class="docutils literal"><span class="pre">return</span></tt> or
+<tt class="docutils literal"><span class="pre">break</span></tt>), the autorelease pool is restored to the saved state, releasing all
+the objects in it.  When the block is exited with an exception, the pool is not
+drained.</p>
+<p><tt class="docutils literal"><span class="pre">@autoreleasepool</span></tt> may be used in non-ARC translation units, with equivalent
+semantics.</p>
+<p>A program is ill-formed if it refers to the <tt class="docutils literal"><span class="pre">NSAutoreleasePool</span></tt> class.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">Autorelease pools are clearly important for the compiler to reason about, but
+it is far too much to expect the compiler to accurately reason about control
+dependencies between two calls.  It is also very easy to accidentally forget
+to drain an autorelease pool when using the manual API, and this can
+significantly inflate the process’s high-water-mark.  The introduction of a
+new scope is unfortunate but basically required for sane interaction with the
+rest of the language.  Not draining the pool during an unwind is apparently
+required by the Objective-C exceptions implementation.</p>
+</div>
+</div>
+<div class="section" id="self">
+<span id="arc-misc-self"></span><h3><a class="toc-backref" href="#id48"><tt class="docutils literal"><span class="pre">self</span></tt></a><a class="headerlink" href="#self" title="Permalink to this headline">¶</a></h3>
+<p>The <tt class="docutils literal"><span class="pre">self</span></tt> parameter variable of an Objective-C method is never actually
+retained by the implementation.  It is undefined behavior, or at least
+dangerous, to cause an object to be deallocated during a message send to that
+object.</p>
+<p>To make this safe, for Objective-C instance methods <tt class="docutils literal"><span class="pre">self</span></tt> is implicitly
+<tt class="docutils literal"><span class="pre">const</span></tt> unless the method is in the <a class="reference internal" href="#arc-family-semantics-init"><em>init family</em></a>.  Further, <tt class="docutils literal"><span class="pre">self</span></tt> is <strong>always</strong> implicitly
+<tt class="docutils literal"><span class="pre">const</span></tt> within a class method.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">The cost of retaining <tt class="docutils literal"><span class="pre">self</span></tt> in all methods was found to be prohibitive, as
+it tends to be live across calls, preventing the optimizer from proving that
+the retain and release are unnecessary — for good reason, as it’s quite
+possible in theory to cause an object to be deallocated during its execution
+without this retain and release.  Since it’s extremely uncommon to actually
+do so, even unintentionally, and since there’s no natural way for the
+programmer to remove this retain/release pair otherwise (as there is for
+other parameters by, say, making the variable <tt class="docutils literal"><span class="pre">__unsafe_unretained</span></tt>), we
+chose to make this optimizing assumption and shift some amount of risk to the
+user.</p>
+</div>
+</div>
+<div class="section" id="fast-enumeration-iteration-variables">
+<span id="arc-misc-enumeration"></span><h3><a class="toc-backref" href="#id49">Fast enumeration iteration variables</a><a class="headerlink" href="#fast-enumeration-iteration-variables" title="Permalink to this headline">¶</a></h3>
+<p>If a variable is declared in the condition of an Objective-C fast enumeration
+loop, and the variable has no explicit ownership qualifier, then it is
+qualified with <tt class="docutils literal"><span class="pre">const</span> <span class="pre">__strong</span></tt> and objects encountered during the
+enumeration are not actually retained.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">This is an optimization made possible because fast enumeration loops promise
+to keep the objects retained during enumeration, and the collection itself
+cannot be synchronously modified.  It can be overridden by explicitly
+qualifying the variable with <tt class="docutils literal"><span class="pre">__strong</span></tt>, which will make the variable
+mutable again and cause the loop to retain the objects it encounters.</p>
+</div>
+</div>
+<div class="section" id="blocks">
+<span id="arc-misc-blocks"></span><h3><a class="toc-backref" href="#id50">Blocks</a><a class="headerlink" href="#blocks" title="Permalink to this headline">¶</a></h3>
+<p>The implicit <tt class="docutils literal"><span class="pre">const</span></tt> capture variables created when evaluating a block
+literal expression have the same ownership semantics as the local variables
+they capture.  The capture is performed by reading from the captured variable
+and initializing the capture variable with that value; the capture variable is
+destroyed when the block literal is, i.e. at the end of the enclosing scope.</p>
+<p>The <a class="reference internal" href="#arc-ownership-inference"><em>inference</em></a> rules apply equally to
+<tt class="docutils literal"><span class="pre">__block</span></tt> variables, which is a shift in semantics from non-ARC, where
+<tt class="docutils literal"><span class="pre">__block</span></tt> variables did not implicitly retain during capture.</p>
+<p><tt class="docutils literal"><span class="pre">__block</span></tt> variables of retainable object owner type are moved off the stack
+by initializing the heap copy with the result of moving from the stack copy.</p>
+<p>With the exception of retains done as part of initializing a <tt class="docutils literal"><span class="pre">__strong</span></tt>
+parameter variable or reading a <tt class="docutils literal"><span class="pre">__weak</span></tt> variable, whenever these semantics
+call for retaining a value of block-pointer type, it has the effect of a
+<tt class="docutils literal"><span class="pre">Block_copy</span></tt>.  The optimizer may remove such copies when it sees that the
+result is used only as an argument to a call.</p>
+</div>
+<div class="section" id="exceptions">
+<span id="arc-misc-exceptions"></span><h3><a class="toc-backref" href="#id51">Exceptions</a><a class="headerlink" href="#exceptions" title="Permalink to this headline">¶</a></h3>
+<p>By default in Objective C, ARC is not exception-safe for normal releases:</p>
+<ul class="simple">
+<li>It does not end the lifetime of <tt class="docutils literal"><span class="pre">__strong</span></tt> variables when their scopes are
+abnormally terminated by an exception.</li>
+<li>It does not perform releases which would occur at the end of a
+full-expression if that full-expression throws an exception.</li>
+</ul>
+<p>A program may be compiled with the option <tt class="docutils literal"><span class="pre">-fobjc-arc-exceptions</span></tt> in order to
+enable these, or with the option <tt class="docutils literal"><span class="pre">-fno-objc-arc-exceptions</span></tt> to explicitly
+disable them, with the last such argument “winning”.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">The standard Cocoa convention is that exceptions signal programmer error and
+are not intended to be recovered from.  Making code exceptions-safe by
+default would impose severe runtime and code size penalties on code that
+typically does not actually care about exceptions safety.  Therefore,
+ARC-generated code leaks by default on exceptions, which is just fine if the
+process is going to be immediately terminated anyway.  Programs which do care
+about recovering from exceptions should enable the option.</p>
+</div>
+<p>In Objective-C++, <tt class="docutils literal"><span class="pre">-fobjc-arc-exceptions</span></tt> is enabled by default.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">C++ already introduces pervasive exceptions-cleanup code of the sort that ARC
+introduces.  C++ programmers who have not already disabled exceptions are
+much more likely to actual require exception-safety.</p>
+</div>
+<p>ARC does end the lifetimes of <tt class="docutils literal"><span class="pre">__weak</span></tt> objects when an exception terminates
+their scope unless exceptions are disabled in the compiler.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">The consequence of a local <tt class="docutils literal"><span class="pre">__weak</span></tt> object not being destroyed is very
+likely to be corruption of the Objective-C runtime, so we want to be safer
+here.  Of course, potentially massive leaks are about as likely to take down
+the process as this corruption is if the program does try to recover from
+exceptions.</p>
+</div>
+</div>
+<div class="section" id="interior-pointers">
+<span id="arc-misc-interior"></span><h3><a class="toc-backref" href="#id52">Interior pointers</a><a class="headerlink" href="#interior-pointers" title="Permalink to this headline">¶</a></h3>
+<p>An Objective-C method returning a non-retainable pointer may be annotated with
+the <tt class="docutils literal"><span class="pre">objc_returns_inner_pointer</span></tt> attribute to indicate that it returns a
+handle to the internal data of an object, and that this reference will be
+invalidated if the object is destroyed.  When such a message is sent to an
+object, the object’s lifetime will be extended until at least the earliest of:</p>
+<ul class="simple">
+<li>the last use of the returned pointer, or any pointer derived from it, in the
+calling function or</li>
+<li>the autorelease pool is restored to a previous state.</li>
+</ul>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p>Rationale: not all memory and resources are managed with reference counts; it
+is common for objects to manage private resources in their own, private way.
+Typically these resources are completely encapsulated within the object, but
+some classes offer their users direct access for efficiency.  If ARC is not
+aware of methods that return such “interior” pointers, its optimizations can
+cause the owning object to be reclaimed too soon.  This attribute informs ARC
+that it must tread lightly.</p>
+<p class="last">The extension rules are somewhat intentionally vague.  The autorelease pool
+limit is there to permit a simple implementation to simply retain and
+autorelease the receiver.  The other limit permits some amount of
+optimization.  The phrase “derived from” is intended to encompass the results
+both of pointer transformations, such as casts and arithmetic, and of loading
+from such derived pointers; furthermore, it applies whether or not such
+derivations are applied directly in the calling code or by other utility code
+(for example, the C library routine <tt class="docutils literal"><span class="pre">strchr</span></tt>).  However, the implementation
+never need account for uses after a return from the code which calls the
+method returning an interior pointer.</p>
+</div>
+<p>As an exception, no extension is required if the receiver is loaded directly
+from a <tt class="docutils literal"><span class="pre">__strong</span></tt> object with <a class="reference internal" href="#arc-optimization-precise"><em>precise lifetime semantics</em></a>.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">Implicit autoreleases carry the risk of significantly inflating memory use,
+so it’s important to provide users a way of avoiding these autoreleases.
+Tying this to precise lifetime semantics is ideal, as for local variables
+this requires a very explicit annotation, which allows ARC to trust the user
+with good cheer.</p>
+</div>
+</div>
+<div class="section" id="c-retainable-pointer-types">
+<span id="arc-misc-c-retainable"></span><h3><a class="toc-backref" href="#id53">C retainable pointer types</a><a class="headerlink" href="#c-retainable-pointer-types" title="Permalink to this headline">¶</a></h3>
+<p>A type is a <span class="arc-term">C retainable pointer type</span> if it is a pointer to
+(possibly qualified) <tt class="docutils literal"><span class="pre">void</span></tt> or a pointer to a (possibly qualifier) <tt class="docutils literal"><span class="pre">struct</span></tt>
+or <tt class="docutils literal"><span class="pre">class</span></tt> type.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">ARC does not manage pointers of CoreFoundation type (or any of the related
+families of retainable C pointers which interoperate with Objective-C for
+retain/release operation).  In fact, ARC does not even know how to
+distinguish these types from arbitrary C pointer types.  The intent of this
+concept is to filter out some obviously non-object types while leaving a hook
+for later tightening if a means of exhaustively marking CF types is made
+available.</p>
+</div>
+<div class="section" id="auditing-of-c-retainable-pointer-interfaces">
+<span id="arc-misc-c-retainable-audit"></span><h4><a class="toc-backref" href="#id54">Auditing of C retainable pointer interfaces</a><a class="headerlink" href="#auditing-of-c-retainable-pointer-interfaces" title="Permalink to this headline">¶</a></h4>
+<p><span class="when-revised">[beginning Apple 4.0, LLVM 3.1]</span></p>
+<p>A C function may be marked with the <tt class="docutils literal"><span class="pre">cf_audited_transfer</span></tt> attribute to
+express that, except as otherwise marked with attributes, it obeys the
+parameter (consuming vs. non-consuming) and return (retained vs. non-retained)
+conventions for a C function of its name, namely:</p>
+<ul class="simple">
+<li>A parameter of C retainable pointer type is assumed to not be consumed
+unless it is marked with the <tt class="docutils literal"><span class="pre">cf_consumed</span></tt> attribute, and</li>
+<li>A result of C retainable pointer type is assumed to not be returned retained
+unless the function is either marked <tt class="docutils literal"><span class="pre">cf_returns_retained</span></tt> or it follows
+the create/copy naming convention and is not marked
+<tt class="docutils literal"><span class="pre">cf_returns_not_retained</span></tt>.</li>
+</ul>
+<p>A function obeys the <span class="arc-term">create/copy</span> naming convention if its name
+contains as a substring:</p>
+<ul class="simple">
+<li>either “Create” or “Copy” not followed by a lowercase letter, or</li>
+<li>either “create” or “copy” not followed by a lowercase letter and
+not preceded by any letter, whether uppercase or lowercase.</li>
+</ul>
+<p>A second attribute, <tt class="docutils literal"><span class="pre">cf_unknown_transfer</span></tt>, signifies that a function’s
+transfer semantics cannot be accurately captured using any of these
+annotations.  A program is ill-formed if it annotates the same function with
+both <tt class="docutils literal"><span class="pre">cf_audited_transfer</span></tt> and <tt class="docutils literal"><span class="pre">cf_unknown_transfer</span></tt>.</p>
+<p>A pragma is provided to facilitate the mass annotation of interfaces:</p>
+<div class="highlight-objc"><div class="highlight"><pre><span class="cp">#pragma clang arc_cf_code_audited begin</span>
+<span class="p">...</span>
+<span class="cp">#pragma clang arc_cf_code_audited end</span>
+</pre></div>
+</div>
+<p>All C functions declared within the extent of this pragma are treated as if
+annotated with the <tt class="docutils literal"><span class="pre">cf_audited_transfer</span></tt> attribute unless they otherwise have
+the <tt class="docutils literal"><span class="pre">cf_unknown_transfer</span></tt> attribute.  The pragma is accepted in all language
+modes.  A program is ill-formed if it attempts to change files, whether by
+including a file or ending the current file, within the extent of this pragma.</p>
+<p>It is possible to test for all the features in this section with
+<tt class="docutils literal"><span class="pre">__has_feature(arc_cf_code_audited)</span></tt>.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">A significant inconvenience in ARC programming is the necessity of
+interacting with APIs based around C retainable pointers.  These features are
+designed to make it relatively easy for API authors to quickly review and
+annotate their interfaces, in turn improving the fidelity of tools such as
+the static analyzer and ARC.  The single-file restriction on the pragma is
+designed to eliminate the risk of accidentally annotating some other header’s
+interfaces.</p>
+</div>
+</div>
+</div>
+</div>
+<div class="section" id="runtime-support">
+<span id="arc-runtime"></span><h2><a class="toc-backref" href="#id55">Runtime support</a><a class="headerlink" href="#runtime-support" title="Permalink to this headline">¶</a></h2>
+<p>This section describes the interaction between the ARC runtime and the code
+generated by the ARC compiler.  This is not part of the ARC language
+specification; instead, it is effectively a language-specific ABI supplement,
+akin to the “Itanium” generic ABI for C++.</p>
+<p>Ownership qualification does not alter the storage requirements for objects,
+except that it is undefined behavior if a <tt class="docutils literal"><span class="pre">__weak</span></tt> object is inadequately
+aligned for an object of type <tt class="docutils literal"><span class="pre">id</span></tt>.  The other qualifiers may be used on
+explicitly under-aligned memory.</p>
+<p>The runtime tracks <tt class="docutils literal"><span class="pre">__weak</span></tt> objects which holds non-null values.  It is
+undefined behavior to direct modify a <tt class="docutils literal"><span class="pre">__weak</span></tt> object which is being tracked
+by the runtime except through an
+<a class="reference internal" href="#arc-runtime-objc-storeweak"><em>objc_storeWeak</em></a>,
+<a class="reference internal" href="#arc-runtime-objc-destroyweak"><em>objc_destroyWeak</em></a>, or
+<a class="reference internal" href="#arc-runtime-objc-moveweak"><em>objc_moveWeak</em></a> call.</p>
+<p>The runtime must provide a number of new entrypoints which the compiler may
+emit, which are described in the remainder of this section.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p>Several of these functions are semantically equivalent to a message send; we
+emit calls to C functions instead because:</p>
+<ul class="simple">
+<li>the machine code to do so is significantly smaller,</li>
+<li>it is much easier to recognize the C functions in the ARC optimizer, and</li>
+<li>a sufficient sophisticated runtime may be able to avoid the message send in
+common cases.</li>
+</ul>
+<p class="last">Several other of these functions are “fused” operations which can be
+described entirely in terms of other operations.  We use the fused operations
+primarily as a code-size optimization, although in some cases there is also a
+real potential for avoiding redundant operations in the runtime.</p>
+</div>
+<div class="section" id="arc-runtime-objc-autorelease">
+<span id="id-objc-autorelease-id-value"></span><h3><a class="toc-backref" href="#id56"><tt class="docutils literal"><span class="pre">id</span> <span class="pre">objc_autorelease(id</span> <span class="pre">value);</span></tt></a><a class="headerlink" href="#arc-runtime-objc-autorelease" title="Permalink to this headline">¶</a></h3>
+<p><em>Precondition:</em> <tt class="docutils literal"><span class="pre">value</span></tt> is null or a pointer to a valid object.</p>
+<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is null, this call has no effect.  Otherwise, it adds the object
+to the innermost autorelease pool exactly as if the object had been sent the
+<tt class="docutils literal"><span class="pre">autorelease</span></tt> message.</p>
+<p>Always returns <tt class="docutils literal"><span class="pre">value</span></tt>.</p>
+</div>
+<div class="section" id="void-objc-autoreleasepoolpop-void-pool">
+<span id="arc-runtime-objc-autoreleasepoolpop"></span><h3><a class="toc-backref" href="#id57"><tt class="docutils literal"><span class="pre">void</span> <span class="pre">objc_autoreleasePoolPop(void</span> <span class="pre">*pool);</span></tt></a><a class="headerlink" href="#void-objc-autoreleasepoolpop-void-pool" title="Permalink to this headline">¶</a></h3>
+<p><em>Precondition:</em> <tt class="docutils literal"><span class="pre">pool</span></tt> is the result of a previous call to
+<a class="reference internal" href="#arc-runtime-objc-autoreleasepoolpush"><em>objc_autoreleasePoolPush</em></a> on the
+current thread, where neither <tt class="docutils literal"><span class="pre">pool</span></tt> nor any enclosing pool have previously
+been popped.</p>
+<p>Releases all the objects added to the given autorelease pool and any
+autorelease pools it encloses, then sets the current autorelease pool to the
+pool directly enclosing <tt class="docutils literal"><span class="pre">pool</span></tt>.</p>
+</div>
+<div class="section" id="void-objc-autoreleasepoolpush-void">
+<span id="arc-runtime-objc-autoreleasepoolpush"></span><h3><a class="toc-backref" href="#id58"><tt class="docutils literal"><span class="pre">void</span> <span class="pre">*objc_autoreleasePoolPush(void);</span></tt></a><a class="headerlink" href="#void-objc-autoreleasepoolpush-void" title="Permalink to this headline">¶</a></h3>
+<p>Creates a new autorelease pool that is enclosed by the current pool, makes that
+the current pool, and returns an opaque “handle” to it.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">While the interface is described as an explicit hierarchy of pools, the rules
+allow the implementation to just keep a stack of objects, using the stack
+depth as the opaque pool handle.</p>
+</div>
+</div>
+<div class="section" id="arc-runtime-objc-autoreleasereturnvalue">
+<span id="id-objc-autoreleasereturnvalue-id-value"></span><h3><a class="toc-backref" href="#id59"><tt class="docutils literal"><span class="pre">id</span> <span class="pre">objc_autoreleaseReturnValue(id</span> <span class="pre">value);</span></tt></a><a class="headerlink" href="#arc-runtime-objc-autoreleasereturnvalue" title="Permalink to this headline">¶</a></h3>
+<p><em>Precondition:</em> <tt class="docutils literal"><span class="pre">value</span></tt> is null or a pointer to a valid object.</p>
+<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is null, this call has no effect.  Otherwise, it makes a best
+effort to hand off ownership of a retain count on the object to a call to
+<a class="reference internal" href="#arc-runtime-objc-retainautoreleasedreturnvalue"><em>objc_retainAutoreleasedReturnValue</em></a> for the same object in an
+enclosing call frame.  If this is not possible, the object is autoreleased as
+above.</p>
+<p>Always returns <tt class="docutils literal"><span class="pre">value</span></tt>.</p>
+</div>
+<div class="section" id="void-objc-copyweak-id-dest-id-src">
+<span id="arc-runtime-objc-copyweak"></span><h3><a class="toc-backref" href="#id60"><tt class="docutils literal"><span class="pre">void</span> <span class="pre">objc_copyWeak(id</span> <span class="pre">*dest,</span> <span class="pre">id</span> <span class="pre">*src);</span></tt></a><a class="headerlink" href="#void-objc-copyweak-id-dest-id-src" title="Permalink to this headline">¶</a></h3>
+<p><em>Precondition:</em> <tt class="docutils literal"><span class="pre">src</span></tt> is a valid pointer which either contains a null pointer
+or has been registered as a <tt class="docutils literal"><span class="pre">__weak</span></tt> object.  <tt class="docutils literal"><span class="pre">dest</span></tt> is a valid pointer
+which has not been registered as a <tt class="docutils literal"><span class="pre">__weak</span></tt> object.</p>
+<p><tt class="docutils literal"><span class="pre">dest</span></tt> is initialized to be equivalent to <tt class="docutils literal"><span class="pre">src</span></tt>, potentially registering it
+with the runtime.  Equivalent to the following code:</p>
+<div class="highlight-objc"><div class="highlight"><pre><span class="kt">void</span> <span class="nf">objc_copyWeak</span><span class="p">(</span><span class="kt">id</span> <span class="o">*</span><span class="n">dest</span><span class="p">,</span> <span class="kt">id</span> <span class="o">*</span><span class="n">src</span><span class="p">)</span> <span class="p">{</span>
+  <span class="n">objc_release</span><span class="p">(</span><span class="n">objc_initWeak</span><span class="p">(</span><span class="n">dest</span><span class="p">,</span> <span class="n">objc_loadWeakRetained</span><span class="p">(</span><span class="n">src</span><span class="p">)));</span>
+<span class="p">}</span>
+</pre></div>
+</div>
+<p>Must be atomic with respect to calls to <tt class="docutils literal"><span class="pre">objc_storeWeak</span></tt> on <tt class="docutils literal"><span class="pre">src</span></tt>.</p>
+</div>
+<div class="section" id="void-objc-destroyweak-id-object">
+<span id="arc-runtime-objc-destroyweak"></span><h3><a class="toc-backref" href="#id61"><tt class="docutils literal"><span class="pre">void</span> <span class="pre">objc_destroyWeak(id</span> <span class="pre">*object);</span></tt></a><a class="headerlink" href="#void-objc-destroyweak-id-object" title="Permalink to this headline">¶</a></h3>
+<p><em>Precondition:</em> <tt class="docutils literal"><span class="pre">object</span></tt> is a valid pointer which either contains a null
+pointer or has been registered as a <tt class="docutils literal"><span class="pre">__weak</span></tt> object.</p>
+<p><tt class="docutils literal"><span class="pre">object</span></tt> is unregistered as a weak object, if it ever was.  The current value
+of <tt class="docutils literal"><span class="pre">object</span></tt> is left unspecified; otherwise, equivalent to the following code:</p>
+<div class="highlight-objc"><div class="highlight"><pre><span class="kt">void</span> <span class="nf">objc_destroyWeak</span><span class="p">(</span><span class="kt">id</span> <span class="o">*</span><span class="n">object</span><span class="p">)</span> <span class="p">{</span>
+  <span class="n">objc_storeWeak</span><span class="p">(</span><span class="n">object</span><span class="p">,</span> <span class="nb">nil</span><span class="p">);</span>
+<span class="p">}</span>
+</pre></div>
+</div>
+<p>Does not need to be atomic with respect to calls to <tt class="docutils literal"><span class="pre">objc_storeWeak</span></tt> on
+<tt class="docutils literal"><span class="pre">object</span></tt>.</p>
+</div>
+<div class="section" id="arc-runtime-objc-initweak">
+<span id="id-objc-initweak-id-object-id-value"></span><h3><a class="toc-backref" href="#id62"><tt class="docutils literal"><span class="pre">id</span> <span class="pre">objc_initWeak(id</span> <span class="pre">*object,</span> <span class="pre">id</span> <span class="pre">value);</span></tt></a><a class="headerlink" href="#arc-runtime-objc-initweak" title="Permalink to this headline">¶</a></h3>
+<p><em>Precondition:</em> <tt class="docutils literal"><span class="pre">object</span></tt> is a valid pointer which has not been registered as
+a <tt class="docutils literal"><span class="pre">__weak</span></tt> object.  <tt class="docutils literal"><span class="pre">value</span></tt> is null or a pointer to a valid object.</p>
+<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is a null pointer or the object to which it points has begun
+deallocation, <tt class="docutils literal"><span class="pre">object</span></tt> is zero-initialized.  Otherwise, <tt class="docutils literal"><span class="pre">object</span></tt> is
+registered as a <tt class="docutils literal"><span class="pre">__weak</span></tt> object pointing to <tt class="docutils literal"><span class="pre">value</span></tt>.  Equivalent to the
+following code:</p>
+<div class="highlight-objc"><div class="highlight"><pre><span class="kt">id</span> <span class="nf">objc_initWeak</span><span class="p">(</span><span class="kt">id</span> <span class="o">*</span><span class="n">object</span><span class="p">,</span> <span class="kt">id</span> <span class="n">value</span><span class="p">)</span> <span class="p">{</span>
+  <span class="o">*</span><span class="n">object</span> <span class="o">=</span> <span class="nb">nil</span><span class="p">;</span>
+  <span class="k">return</span> <span class="n">objc_storeWeak</span><span class="p">(</span><span class="n">object</span><span class="p">,</span> <span class="n">value</span><span class="p">);</span>
+<span class="p">}</span>
+</pre></div>
+</div>
+<p>Returns the value of <tt class="docutils literal"><span class="pre">object</span></tt> after the call.</p>
+<p>Does not need to be atomic with respect to calls to <tt class="docutils literal"><span class="pre">objc_storeWeak</span></tt> on
+<tt class="docutils literal"><span class="pre">object</span></tt>.</p>
+</div>
+<div class="section" id="arc-runtime-objc-loadweak">
+<span id="id-objc-loadweak-id-object"></span><h3><a class="toc-backref" href="#id63"><tt class="docutils literal"><span class="pre">id</span> <span class="pre">objc_loadWeak(id</span> <span class="pre">*object);</span></tt></a><a class="headerlink" href="#arc-runtime-objc-loadweak" title="Permalink to this headline">¶</a></h3>
+<p><em>Precondition:</em> <tt class="docutils literal"><span class="pre">object</span></tt> is a valid pointer which either contains a null
+pointer or has been registered as a <tt class="docutils literal"><span class="pre">__weak</span></tt> object.</p>
+<p>If <tt class="docutils literal"><span class="pre">object</span></tt> is registered as a <tt class="docutils literal"><span class="pre">__weak</span></tt> object, and the last value stored
+into <tt class="docutils literal"><span class="pre">object</span></tt> has not yet been deallocated or begun deallocation, retains and
+autoreleases that value and returns it.  Otherwise returns null.  Equivalent to
+the following code:</p>
+<div class="highlight-objc"><div class="highlight"><pre><span class="kt">id</span> <span class="nf">objc_loadWeak</span><span class="p">(</span><span class="kt">id</span> <span class="o">*</span><span class="n">object</span><span class="p">)</span> <span class="p">{</span>
+  <span class="k">return</span> <span class="n">objc_autorelease</span><span class="p">(</span><span class="n">objc_loadWeakRetained</span><span class="p">(</span><span class="n">object</span><span class="p">));</span>
+<span class="p">}</span>
+</pre></div>
+</div>
+<p>Must be atomic with respect to calls to <tt class="docutils literal"><span class="pre">objc_storeWeak</span></tt> on <tt class="docutils literal"><span class="pre">object</span></tt>.</p>
+<div class="admonition-rationale admonition">
+<p class="first admonition-title">Rationale</p>
+<p class="last">Loading weak references would be inherently prone to race conditions without
+the retain.</p>
+</div>
+</div>
+<div class="section" id="arc-runtime-objc-loadweakretained">
+<span id="id-objc-loadweakretained-id-object"></span><h3><a class="toc-backref" href="#id64"><tt class="docutils literal"><span class="pre">id</span> <span class="pre">objc_loadWeakRetained(id</span> <span class="pre">*object);</span></tt></a><a class="headerlink" href="#arc-runtime-objc-loadweakretained" title="Permalink to this headline">¶</a></h3>
+<p><em>Precondition:</em> <tt class="docutils literal"><span class="pre">object</span></tt> is a valid pointer which either contains a null
+pointer or has been registered as a <tt class="docutils literal"><span class="pre">__weak</span></tt> object.</p>
+<p>If <tt class="docutils literal"><span class="pre">object</span></tt> is registered as a <tt class="docutils literal"><span class="pre">__weak</span></tt> object, and the last value stored
+into <tt class="docutils literal"><span class="pre">object</span></tt> has not yet been deallocated or begun deallocation, retains
+that value and returns it.  Otherwise returns null.</p>
+<p>Must be atomic with respect to calls to <tt class="docutils literal"><span class="pre">objc_storeWeak</span></tt> on <tt class="docutils literal"><span class="pre">object</span></tt>.</p>
+</div>
+<div class="section" id="void-objc-moveweak-id-dest-id-src">
+<span id="arc-runtime-objc-moveweak"></span><h3><a class="toc-backref" href="#id65"><tt class="docutils literal"><span class="pre">void</span> <span class="pre">objc_moveWeak(id</span> <span class="pre">*dest,</span> <span class="pre">id</span> <span class="pre">*src);</span></tt></a><a class="headerlink" href="#void-objc-moveweak-id-dest-id-src" title="Permalink to this headline">¶</a></h3>
+<p><em>Precondition:</em> <tt class="docutils literal"><span class="pre">src</span></tt> is a valid pointer which either contains a null pointer
+or has been registered as a <tt class="docutils literal"><span class="pre">__weak</span></tt> object.  <tt class="docutils literal"><span class="pre">dest</span></tt> is a valid pointer
+which has not been registered as a <tt class="docutils literal"><span class="pre">__weak</span></tt> object.</p>
+<p><tt class="docutils literal"><span class="pre">dest</span></tt> is initialized to be equivalent to <tt class="docutils literal"><span class="pre">src</span></tt>, potentially registering it
+with the runtime.  <tt class="docutils literal"><span class="pre">src</span></tt> may then be left in its original state, in which
+case this call is equivalent to <a class="reference internal" href="#arc-runtime-objc-copyweak"><em>objc_copyWeak</em></a>, or it may be left as null.</p>
+<p>Must be atomic with respect to calls to <tt class="docutils literal"><span class="pre">objc_storeWeak</span></tt> on <tt class="docutils literal"><span class="pre">src</span></tt>.</p>
+</div>
+<div class="section" id="void-objc-release-id-value">
+<span id="arc-runtime-objc-release"></span><h3><a class="toc-backref" href="#id66"><tt class="docutils literal"><span class="pre">void</span> <span class="pre">objc_release(id</span> <span class="pre">value);</span></tt></a><a class="headerlink" href="#void-objc-release-id-value" title="Permalink to this headline">¶</a></h3>
+<p><em>Precondition:</em> <tt class="docutils literal"><span class="pre">value</span></tt> is null or a pointer to a valid object.</p>
+<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is null, this call has no effect.  Otherwise, it performs a
+release operation exactly as if the object had been sent the <tt class="docutils literal"><span class="pre">release</span></tt>
+message.</p>
+</div>
+<div class="section" id="arc-runtime-objc-retain">
+<span id="id-objc-retain-id-value"></span><h3><a class="toc-backref" href="#id67"><tt class="docutils literal"><span class="pre">id</span> <span class="pre">objc_retain(id</span> <span class="pre">value);</span></tt></a><a class="headerlink" href="#arc-runtime-objc-retain" title="Permalink to this headline">¶</a></h3>
+<p><em>Precondition:</em> <tt class="docutils literal"><span class="pre">value</span></tt> is null or a pointer to a valid object.</p>
+<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is null, this call has no effect.  Otherwise, it performs a retain
+operation exactly as if the object had been sent the <tt class="docutils literal"><span class="pre">retain</span></tt> message.</p>
+<p>Always returns <tt class="docutils literal"><span class="pre">value</span></tt>.</p>
+</div>
+<div class="section" id="arc-runtime-objc-retainautorelease">
+<span id="id-objc-retainautorelease-id-value"></span><h3><a class="toc-backref" href="#id68"><tt class="docutils literal"><span class="pre">id</span> <span class="pre">objc_retainAutorelease(id</span> <span class="pre">value);</span></tt></a><a class="headerlink" href="#arc-runtime-objc-retainautorelease" title="Permalink to this headline">¶</a></h3>
+<p><em>Precondition:</em> <tt class="docutils literal"><span class="pre">value</span></tt> is null or a pointer to a valid object.</p>
+<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is null, this call has no effect.  Otherwise, it performs a retain
+operation followed by an autorelease operation.  Equivalent to the following
+code:</p>
+<div class="highlight-objc"><div class="highlight"><pre><span class="kt">id</span> <span class="nf">objc_retainAutorelease</span><span class="p">(</span><span class="kt">id</span> <span class="n">value</span><span class="p">)</span> <span class="p">{</span>
+  <span class="k">return</span> <span class="n">objc_autorelease</span><span class="p">(</span><span class="n">objc_retain</span><span class="p">(</span><span class="n">value</span><span class="p">));</span>
+<span class="p">}</span>
+</pre></div>
+</div>
+<p>Always returns <tt class="docutils literal"><span class="pre">value</span></tt>.</p>
+</div>
+<div class="section" id="arc-runtime-objc-retainautoreleasereturnvalue">
+<span id="id-objc-retainautoreleasereturnvalue-id-value"></span><h3><a class="toc-backref" href="#id69"><tt class="docutils literal"><span class="pre">id</span> <span class="pre">objc_retainAutoreleaseReturnValue(id</span> <span class="pre">value);</span></tt></a><a class="headerlink" href="#arc-runtime-objc-retainautoreleasereturnvalue" title="Permalink to this headline">¶</a></h3>
+<p><em>Precondition:</em> <tt class="docutils literal"><span class="pre">value</span></tt> is null or a pointer to a valid object.</p>
+<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is null, this call has no effect.  Otherwise, it performs a retain
+operation followed by the operation described in
+<a class="reference internal" href="#arc-runtime-objc-autoreleasereturnvalue"><em>objc_autoreleaseReturnValue</em></a>.
+Equivalent to the following code:</p>
+<div class="highlight-objc"><div class="highlight"><pre><span class="kt">id</span> <span class="nf">objc_retainAutoreleaseReturnValue</span><span class="p">(</span><span class="kt">id</span> <span class="n">value</span><span class="p">)</span> <span class="p">{</span>
+  <span class="k">return</span> <span class="n">objc_autoreleaseReturnValue</span><span class="p">(</span><span class="n">objc_retain</span><span class="p">(</span><span class="n">value</span><span class="p">));</span>
+<span class="p">}</span>
+</pre></div>
+</div>
+<p>Always returns <tt class="docutils literal"><span class="pre">value</span></tt>.</p>
+</div>
+<div class="section" id="arc-runtime-objc-retainautoreleasedreturnvalue">
+<span id="id-objc-retainautoreleasedreturnvalue-id-value"></span><h3><a class="toc-backref" href="#id70"><tt class="docutils literal"><span class="pre">id</span> <span class="pre">objc_retainAutoreleasedReturnValue(id</span> <span class="pre">value);</span></tt></a><a class="headerlink" href="#arc-runtime-objc-retainautoreleasedreturnvalue" title="Permalink to this headline">¶</a></h3>
+<p><em>Precondition:</em> <tt class="docutils literal"><span class="pre">value</span></tt> is null or a pointer to a valid object.</p>
+<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is null, this call has no effect.  Otherwise, it attempts to
+accept a hand off of a retain count from a call to
+<a class="reference internal" href="#arc-runtime-objc-autoreleasereturnvalue"><em>objc_autoreleaseReturnValue</em></a> on
+<tt class="docutils literal"><span class="pre">value</span></tt> in a recently-called function or something it calls.  If that fails,
+it performs a retain operation exactly like <a class="reference internal" href="#arc-runtime-objc-retain"><em>objc_retain</em></a>.</p>
+<p>Always returns <tt class="docutils literal"><span class="pre">value</span></tt>.</p>
+</div>
+<div class="section" id="arc-runtime-objc-retainblock">
+<span id="id-objc-retainblock-id-value"></span><h3><a class="toc-backref" href="#id71"><tt class="docutils literal"><span class="pre">id</span> <span class="pre">objc_retainBlock(id</span> <span class="pre">value);</span></tt></a><a class="headerlink" href="#arc-runtime-objc-retainblock" title="Permalink to this headline">¶</a></h3>
+<p><em>Precondition:</em> <tt class="docutils literal"><span class="pre">value</span></tt> is null or a pointer to a valid block object.</p>
+<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is null, this call has no effect.  Otherwise, if the block pointed
+to by <tt class="docutils literal"><span class="pre">value</span></tt> is still on the stack, it is copied to the heap and the address
+of the copy is returned.  Otherwise a retain operation is performed on the
+block exactly as if it had been sent the <tt class="docutils literal"><span class="pre">retain</span></tt> message.</p>
+</div>
+<div class="section" id="arc-runtime-objc-storestrong">
+<span id="id-objc-storestrong-id-object-id-value"></span><h3><a class="toc-backref" href="#id72"><tt class="docutils literal"><span class="pre">id</span> <span class="pre">objc_storeStrong(id</span> <span class="pre">*object,</span> <span class="pre">id</span> <span class="pre">value);</span></tt></a><a class="headerlink" href="#arc-runtime-objc-storestrong" title="Permalink to this headline">¶</a></h3>
+<p><em>Precondition:</em> <tt class="docutils literal"><span class="pre">object</span></tt> is a valid pointer to a <tt class="docutils literal"><span class="pre">__strong</span></tt> object which is
+adequately aligned for a pointer.  <tt class="docutils literal"><span class="pre">value</span></tt> is null or a pointer to a valid
+object.</p>
+<p>Performs the complete sequence for assigning to a <tt class="docutils literal"><span class="pre">__strong</span></tt> object of
+non-block type <a class="footnote-reference" href="#id3" id="id2">[*]</a>.  Equivalent to the following code:</p>
+<div class="highlight-objc"><div class="highlight"><pre><span class="kt">id</span> <span class="nf">objc_storeStrong</span><span class="p">(</span><span class="kt">id</span> <span class="o">*</span><span class="n">object</span><span class="p">,</span> <span class="kt">id</span> <span class="n">value</span><span class="p">)</span> <span class="p">{</span>
+  <span class="n">value</span> <span class="o">=</span> <span class="p">[</span><span class="n">value</span> <span class="n">retain</span><span class="p">];</span>
+  <span class="kt">id</span> <span class="n">oldValue</span> <span class="o">=</span> <span class="o">*</span><span class="n">object</span><span class="p">;</span>
+  <span class="o">*</span><span class="n">object</span> <span class="o">=</span> <span class="n">value</span><span class="p">;</span>
+  <span class="p">[</span><span class="n">oldValue</span> <span class="n">release</span><span class="p">];</span>
+  <span class="k">return</span> <span class="n">value</span><span class="p">;</span>
+<span class="p">}</span>
+</pre></div>
+</div>
+<p>Always returns <tt class="docutils literal"><span class="pre">value</span></tt>.</p>
+<table class="docutils footnote" frame="void" id="id3" rules="none">
+<colgroup><col class="label" /><col /></colgroup>
+<tbody valign="top">
+<tr><td class="label"><a class="fn-backref" href="#id2">[*]</a></td><td>This does not imply that a <tt class="docutils literal"><span class="pre">__strong</span></tt> object of block type is an
+invalid argument to this function. Rather it implies that an <tt class="docutils literal"><span class="pre">objc_retain</span></tt>
+and not an <tt class="docutils literal"><span class="pre">objc_retainBlock</span></tt> operation will be emitted if the argument is
+a block.</td></tr>
+</tbody>
+</table>
+</div>
+<div class="section" id="arc-runtime-objc-storeweak">
+<span id="id-objc-storeweak-id-object-id-value"></span><h3><a class="toc-backref" href="#id73"><tt class="docutils literal"><span class="pre">id</span> <span class="pre">objc_storeWeak(id</span> <span class="pre">*object,</span> <span class="pre">id</span> <span class="pre">value);</span></tt></a><a class="headerlink" href="#arc-runtime-objc-storeweak" title="Permalink to this headline">¶</a></h3>
+<p><em>Precondition:</em> <tt class="docutils literal"><span class="pre">object</span></tt> is a valid pointer which either contains a null
+pointer or has been registered as a <tt class="docutils literal"><span class="pre">__weak</span></tt> object.  <tt class="docutils literal"><span class="pre">value</span></tt> is null or a
+pointer to a valid object.</p>
+<p>If <tt class="docutils literal"><span class="pre">value</span></tt> is a null pointer or the object to which it points has begun
+deallocation, <tt class="docutils literal"><span class="pre">object</span></tt> is assigned null and unregistered as a <tt class="docutils literal"><span class="pre">__weak</span></tt>
+object.  Otherwise, <tt class="docutils literal"><span class="pre">object</span></tt> is registered as a <tt class="docutils literal"><span class="pre">__weak</span></tt> object or has its
+registration updated to point to <tt class="docutils literal"><span class="pre">value</span></tt>.</p>
+<p>Returns the value of <tt class="docutils literal"><span class="pre">object</span></tt> after the call.</p>
+</div>
+</div>
+</div>
+
+
+      </div>
+      <div class="bottomnav">
+      
+        <p>
+        «  <a href="Block-ABI-Apple.html">Block Implementation Specification</a>
+          ::  
+        <a class="uplink" href="index.html">Contents</a>
+          ::  
+        <a href="AttributeReference.html">Attributes in Clang</a>  Â»
+        </p>
+
+      </div>
+
+    <div class="footer">
+        © Copyright 2007-2015, The Clang Team.
+      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.3.
+    </div>
+  </body>
+</html>
\ No newline at end of file




More information about the llvm-commits mailing list