[project @ 2002-02-12 16:55:22 by simonmar]
[ghc-hetmet.git] / ghc / docs / comm / the-beast / mangler.html
1 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
2 <html>
3   <head>
4     <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
5     <title>The GHC Commentary - The Evil Mangler</title>
6   </head>
7
8   <body BGCOLOR="FFFFFF">
9     <h1>The GHC Commentary - The Evil Mangler</h1>
10     <p>
11       The Evil Mangler (EM) is a Perl script invoked by the <a
12         href="driver.html">Glorious Driver</a> after the C compiler (gcc) has
13       translated the GHC-produced C code into assembly.  Consequently, it is
14       only of interest if <code>-fvia-C</code> is in effect (either explicitly
15       or implicitly).
16
17     <h4>Its purpose</h4>
18     <p>
19       The EM reads the assembly produced by gcc and re-arranges code blocks as
20       well as nukes instructions that it considers <em>non-essential.</em> It
21       derives it evilness from its utterly ad hoc, machine, compiler, and
22       whatnot dependent design and implementation.  More precisely, the EM
23       performs the following tasks:
24     <ul>
25       <li>The code executed when a closure is entered is moved adjacent to
26         that closure's infotable.  Moreover, the order of the info table
27         entries is reversed.
28       <li>Function prologue and epilogue code is removed.  (GHC generated code
29         manages its own stack and uses the system stack only for return
30         addresses and during calls to C code.)
31       <li>Certain code patterns are replaced by simpler code (eg, loads of
32         fast entry points followed by indirect jumps are replaced by direct
33         jumps to the fast entry point).
34     </ul>
35
36     <h4>Implementation</h4>
37     <p>
38       The EM is located in the Perl script <a
39       href="http://cvs.haskell.org/cgi-bin/cvsweb.cgi/fptools/ghc/driver/mangler/ghc-asm.lprl"><code>ghc-asm.lprl</code></a>.
40       The script reads the <code>.s</code> file and chops it up into
41       <em>chunks</em> (that's how they are actually called in the script) that
42       roughly correspond to basic blocks.  Each chunk is annotated with an
43       educated guess about what kind of code it contains (e.g., infotable,
44       fast entry point, slow entry point, etc.).  The annotations also contain
45       the symbol introducing the chunk of assembly and whether that chunk has
46       already been processed or not.
47     <p>
48       The parsing of the input into chunks as well as recognising assembly
49       instructions that are to be removed or altered is based on a large
50       number of Perl regular expressions sprinkled over the whole code.  These
51       expressions are rather fragile as they heavily rely on the structure of
52       the generated code - in fact, they even rely on the right amount of wide
53       space and thus on the formatting of the assembly.
54     <p>
55       Afterwards, the chunks are reordered, some of them purged, and some
56       stripped of some useless instructions.  Moreover, some instructions are
57       manipulated (eg, loads of fast entry points followed by indirect jumps
58       are replaced by direct jumps to the fast entry point).
59     <p>
60       The EM knows which part of the code belongs to function prologues and
61       epilogues as <a href="../rts-libs/stgc.html">STG C</a> adds tags of the
62       form <code>--- BEGIN ---</code> and <code>--- END ---</code> the
63       assembler just before and after the code proper of a function starts.
64       It adds these tags using gcc's <code>__asm__</code> feature.
65     <p>
66       <strong>Update:</strong> Gcc 2.96 upwards performs more aggressive basic
67       block re-ordering and dead code elimination.  This seems to make the
68       whole <code>--- END ---</code> tag business redundant -- in fact, if
69       proper code is generated, no <code>--- END ---</code> tags survive gcc
70       optimiser. 
71
72     <p><small>
73 <!-- hhmts start -->
74 Last modified: Wed Aug  8 19:27:22 EST 2001
75 <!-- hhmts end -->
76     </small>
77   </body>
78 </html>