Commit 09d72f1d authored by chak's avatar chak
Browse files

[project @ 2005-03-05 11:58:41 by chak]

Extended the commentary with a section about the STG-related parts of GHC
(generation of STG from Core, STG passes, and generation of Cmm).

[BTW, it's a pity that nobody bothered to write up the new code generation
structure when it was implemented not long ago.]
parent 75d52d81
......@@ -6,7 +6,7 @@
<h1>The Glasgow Haskell Compiler (GHC) Commentary [v0.14]</h1>
<h1>The Glasgow Haskell Compiler (GHC) Commentary [v0.15]</h1>
<!-- Contributors: Whoever makes substantial additions or changes to the
document, please add your name and keep the order alphabetic. Moreover,
......@@ -70,6 +70,7 @@
<li><a href="the-beast/simplifier.html">The Mighty Simplifier</a>
<li><a href="the-beast/mangler.html">The Evil Mangler</a>
<li><a href="the-beast/alien.html">Alien Functions</a>
<li><a href="the-beast/stg.html">You Got Control: The STG-language</a>
<li><a href="the-beast/ncg.html">The Native Code Generator</a>
<li><a href="the-beast/ghci.html">GHCi</a>
<li><a href="the-beast/fexport.html">Implementation of
......@@ -112,7 +113,7 @@
<!-- hhmts start -->
Last modified: Sat Sep 13 01:15:05 BST 2003
Last modified: Sat Mar 5 19:52:33 EST 2005
<!-- hhmts end -->
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
<title>The GHC Commentary - You Got Control: The STG-language</title>
<h1>The GHC Commentary - You Got Control: The STG-language</h1>
GHC contains two completely independent backends: the byte code
generator and the machine code generator. The decision over which of
the two is invoked is made in <a
The machine code generator proceeds itself in a number of phases: First,
the <a href="desugar.html">Core</a> intermediate language is translated
into <em>STG-language</em>; second, STG-language is transformed into a
GHC-internal variant of <a href="">C--</a>;
and thirdly, this is either emitted as concrete C--, converted to GNU C,
or translated to native code (by the <a href="ncg.html">native code
generator</a> which targets IA32, Sparc, and PowerPC [as of March '5]).
In the following, we will have a look at the first step of machine code
generation, namely the translation steps involving the STG-language.
Details about the underlying abstract machine, the <em>Spineless Tagless
G-machine</em>, are in <a
lazy functional languages on stock hardware: the Spineless Tagless
G-machine</a>, SL Peyton Jones, Journal of Functional Programming 2(2),
Apr 1992, pp127-202. (Some details have changed since the publication of
this article, but it still gives a good introduction to the main
<h2>The STG Language</h2>
The AST of the STG-language and the generation of STG code from Core is
both located in the <a
directory; in the modules <a
and <a
Conceptually, the STG-language is a lambda calculus (including data
constructors and case expressions) whose syntax is restricted to make
all control flow explicit. As such, it can be regarded as a variant of
<em>administrative normal form (ANF).</em> (C.f., <a
href="">The essence of compiling
with continuations.</a> Cormac Flanagan, Amr Sabry, Bruce F. Duba, and
Matthias Felleisen. <em>ACM SIGPLAN Conference on Programming Language
Design and Implementation,</em> ACM Press, 1993.) Each syntactic from
has a precise operational interpretation, in addition to the
denotational interpretation inherited from the lambda calculus. The
concrete representation of the STG language inside GHC also includes
auxiliary attributes, such as <em>static reference tables (SRTs),</em>
which determine the top-level bindings referenced by each let binding
and case expression.
As usual in ANF, arguments to functions etc. are restricted to atoms
(i.e., constants or variables), which implies that all sub-expressions
are explicitly named and evaluation order is explicit. Specific to the
STG language is that all let bindings correspond to closure allocation
(thunks, function closures, and data constructors) and that case
expressions encode both computation and case selection. There are two
flavours of case expressions scrutinising boxed and unboxed values,
respectively. The former perform function calls including demanding the
evaluation of thunks, whereas the latter execute primitive operations
(such as arithmetic on fixed size integers and floating-point numbers).
The representation of STG language defined in <a
abstracts over both binders and occurences of variables. The type names
involved in this generic definition all carry the prefix
<code>Gen</code> (such as in <code>GenStgBinding</code>). Instances of
these generic definitions, where both binders and occurences are of type
are defined as type synonyms and use type names that drop the
<code>Gen</code> prefix (i.e., becoming plain <code>StgBinding</code>).
Complete programs in STG form are represented by values of type
<h2>From Core to STG</h2>
Although, the actual translation from Core AST into STG AST is performed
by the function <a
(or <a
for individual expressions), the translation crucial depends on <a
(resp. <a
which prepares Core code for code generation (for both byte code and
machine code generation). <code>CorePrep</code> saturates primitive and
constructor applications, turns the code into A-normal form, renames all
identifiers into globally unique names, generates bindings for
constructor workers, constructor wrappers, and record selectors plus
some further cleanup.
In other words, after Core code is prepared for code generation it is
structurally already in the form required by the STG language. The main
work performed by the actual transformation from Core to STG, as
performed by <a
is to compute the live and free variables as well as live CAFs (constant
applicative forms) at each let binding and case alternative. In
subsequent phases, the live CAF information is used to compute SRTs.
The live variable information is used to determine which stack slots
need to be zapped (to avoid space leaks) and the free variable
information is need to construct closures. Moreover, hints for
optimised code generation are computed, such as whether a closure needs
to be updated after is has been evaluated.
<h2>STG Passes</h2>
These days little actual work is performed on programs in STG form; in
particular, the code is not further optimised. All serious optimisation
(except low-level optimisations which are performed during native code
generation) has already been done on Core. The main task of <a
is to compute SRTs from the live CAF information determined during STG
generation. Other than that, <a
is executed when compiling for profiling and information may be dumped
for debugging purposes.
<h2>Towards C--</h2>
GHC's internal form of C-- is defined in the module <a
The definition is generic in that it abstracts over the type of static
data and of the contents of basic blocks (i.e., over the concrete
representation of constant data and instructions). These generic
definitions have names carrying the prefix <code>Gen</code> (such as
<code>GenCmm</code>). The same module also instantiates the generic
form to a concrete form where data is represented by
<code>CmmStatic</code> and instructions are represented by
<code>CmmStmt</code> (giving us, e.g., <code>Cmm</code> from
<code>GenCmm</code>). The concrete form more or less follows the
external <a href="">C--</a> language.
Programs in STG form are translated to <code>Cmm</code> by <a
<!-- hhmts start -->
Last modified: Sat Mar 5 22:55:25 EST 2005
<!-- hhmts end -->
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment