remove empty dir
[ghc-hetmet.git] / compiler / simplStg / SimplStg.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
3 %
4 \section[SimplStg]{Driver for simplifying @STG@ programs}
5
6 \begin{code}
7 module SimplStg ( stg2stg ) where
8
9 #include "HsVersions.h"
10
11 import StgSyn
12
13 import CostCentre       ( CollectedCCs )
14 import SCCfinal         ( stgMassageForProfiling )
15 import StgLint          ( lintStgBindings )
16 import StgStats         ( showStgStats )
17 import SRT              ( computeSRTs )
18
19 import Packages         ( HomeModules )
20 import DynFlags         ( DynFlags(..), DynFlag(..), dopt, StgToDo(..),
21                           getStgToDo )
22 import Id               ( Id )
23 import Module           ( Module )
24 import ErrUtils         ( doIfSet_dyn, dumpIfSet_dyn, showPass )
25 import UniqSupply       ( mkSplitUniqSupply, splitUniqSupply )
26 import Outputable
27 \end{code}
28
29 \begin{code}
30 stg2stg :: DynFlags                  -- includes spec of what stg-to-stg passes to do
31         -> HomeModules
32         -> Module                    -- module name (profiling only)
33         -> [StgBinding]              -- input...
34         -> IO ( [(StgBinding,[(Id,[Id])])]  -- output program...
35               , CollectedCCs)        -- cost centre information (declared and used)
36
37 stg2stg dflags pkg_deps module_name binds
38   = do  { showPass dflags "Stg2Stg"
39         ; us <- mkSplitUniqSupply 'g'
40
41         ; doIfSet_dyn dflags Opt_D_verbose_stg2stg 
42                       (printDump (text "VERBOSE STG-TO-STG:"))
43
44         ; (binds', us', ccs) <- end_pass us "Stg2Stg" ([],[],[]) binds
45
46                 -- Do the main business!
47         ; (processed_binds, _, cost_centres) 
48                 <- foldl_mn do_stg_pass (binds', us', ccs) (getStgToDo dflags)
49
50         ; let srt_binds = computeSRTs processed_binds
51
52         ; dumpIfSet_dyn dflags Opt_D_dump_stg "STG syntax:" 
53                         (pprStgBindingsWithSRTs srt_binds)
54
55         ; return (srt_binds, cost_centres)
56    }
57
58   where
59     stg_linter = if dopt Opt_DoStgLinting dflags
60                  then lintStgBindings
61                  else ( \ whodunnit binds -> binds )
62
63     -------------------------------------------
64     do_stg_pass (binds, us, ccs) to_do
65       = let
66             (us1, us2) = splitUniqSupply us
67         in
68         case to_do of
69           D_stg_stats ->
70              trace (showStgStats binds)
71              end_pass us2 "StgStats" ccs binds
72
73           StgDoMassageForProfiling ->
74              {-# SCC "ProfMassage" #-}
75              let
76                  (collected_CCs, binds3)
77                    = stgMassageForProfiling pkg_deps module_name us1 binds
78              in
79              end_pass us2 "ProfMassage" collected_CCs binds3
80
81     end_pass us2 what ccs binds2
82       = do -- report verbosely, if required
83            dumpIfSet_dyn dflags Opt_D_verbose_stg2stg what
84               (vcat (map ppr binds2))
85            let linted_binds = stg_linter what binds2
86            return (linted_binds, us2, ccs)
87             -- return: processed binds
88             --         UniqueSupply for the next guy to use
89             --         cost-centres to be declared/registered (specialised)
90             --         add to description of what's happened (reverse order)
91
92 -- here so it can be inlined...
93 foldl_mn f z []     = return z
94 foldl_mn f z (x:xs) = f z x     >>= \ zz ->
95                      foldl_mn f zz xs
96 \end{code}