2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
4 \section[SimplStg]{Driver for simplifying @STG@ programs}
7 module SimplStg ( stg2stg ) where
9 #include "HsVersions.h"
13 import LambdaLift ( liftProgram )
14 import CostCentre ( CostCentre, CostCentreStack )
15 import SCCfinal ( stgMassageForProfiling )
16 import StgLint ( lintStgBindings )
17 import StgStats ( showStgStats )
18 import StgVarInfo ( setStgVarInfo )
19 import SRT ( computeSRTs )
21 import CmdLineOpts ( opt_StgDoLetNoEscapes, opt_D_verbose_stg2stg,
22 opt_DoStgLinting, opt_D_dump_stg,
26 import Module ( Module, moduleString )
27 import ErrUtils ( doIfSet, dumpIfSet )
28 import UniqSupply ( splitUniqSupply, UniqSupply )
29 import IO ( hPutStr, stdout )
34 stg2stg :: [StgToDo] -- spec of what stg-to-stg passes to do
35 -> Module -- module name (profiling only)
36 -> UniqSupply -- a name supply
37 -> [StgBinding] -- input...
39 ([(StgBinding,[Id])], -- output program...
40 ([CostCentre], -- local cost-centres that need to be decl'd
41 [CostCentre], -- "extern" cost-centres
42 [CostCentreStack])) -- pre-defined "singleton" cost centre stacks
44 stg2stg stg_todos module_name us binds
45 = case (splitUniqSupply us) of { (us4now, us4later) ->
47 doIfSet opt_D_verbose_stg2stg (printErrs (text "VERBOSE STG-TO-STG:")) >>
49 end_pass us4now "Core2Stg" ([],[],[]) binds
50 >>= \ (binds', us, ccs) ->
52 -- Do the main business!
53 foldl_mn do_stg_pass (binds', us, ccs) stg_todos
54 >>= \ (processed_binds, _, cost_centres) ->
56 -- Do essential wind-up
58 -- Essential wind-up: part (b), do setStgVarInfo. It has to
59 -- happen regardless, because the code generator uses its
62 -- Why does it have to happen last? Because earlier passes
63 -- may move things around, which would change the live-var
64 -- info. Also, setStgVarInfo decides about let-no-escape
65 -- things, which in turn do a better job if arities are
66 -- correct, which is done by satStgRhs.
70 annotated_binds = setStgVarInfo opt_StgDoLetNoEscapes processed_binds
71 srt_binds = computeSRTs annotated_binds
74 dumpIfSet opt_D_dump_stg "STG syntax:"
75 (pprStgBindingsWithSRTs srt_binds) >>
77 return (srt_binds, cost_centres)
81 stg_linter = if opt_DoStgLinting
83 else ( \ whodunnit binds -> binds )
85 -------------------------------------------
86 do_stg_pass (binds, us, ccs) to_do
88 (us1, us2) = splitUniqSupply us
91 StgDoStaticArgs -> panic "STG static argument transformation deleted"
94 trace (showStgStats binds)
95 end_pass us2 "StgStats" ccs binds
99 -- NB We have to do setStgVarInfo first!
101 binds3 = liftProgram module_name us1 (setStgVarInfo opt_StgDoLetNoEscapes binds)
103 end_pass us2 "LambdaLift" ccs binds3
105 StgDoMassageForProfiling ->
108 (collected_CCs, binds3)
109 = stgMassageForProfiling module_name us1 binds
111 end_pass us2 "ProfMassage" collected_CCs binds3
113 end_pass us2 what ccs binds2
114 = -- report verbosely, if required
115 (if opt_D_verbose_stg2stg then
116 hPutStr stdout (showSDoc
117 (text ("*** "++what++":") $$ vcat (map ppr binds2)
121 linted_binds = stg_linter what binds2
123 return (linted_binds, us2, ccs)
124 -- return: processed binds
125 -- UniqueSupply for the next guy to use
126 -- cost-centres to be declared/registered (specialised)
127 -- add to description of what's happened (reverse order)
129 -- here so it can be inlined...
130 foldl_mn f z [] = return z
131 foldl_mn f z (x:xs) = f z x >>= \ zz ->