%
% (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
%
\section[SimplStg]{Driver for simplifying @STG@ programs}
\begin{code}
module SimplStg ( stg2stg ) where
#include "HsVersions.h"
import StgSyn
import CostCentre ( CollectedCCs )
import SCCfinal ( stgMassageForProfiling )
import StgLint ( lintStgBindings )
import StgStats ( showStgStats )
import UnariseStg ( unarise )
import SRT ( computeSRTs )
import DynFlags ( DynFlags(..), DynFlag(..), dopt, StgToDo(..),
getStgToDo )
import Id ( Id )
import Module ( Module )
import ErrUtils
import SrcLoc
import UniqSupply ( mkSplitUniqSupply, splitUniqSupply )
import Outputable
\end{code}
\begin{code}
stg2stg :: DynFlags
-> Module
-> [StgBinding]
-> IO ( [(StgBinding,[(Id,[Id])])]
, CollectedCCs)
stg2stg dflags module_name binds
= do { showPass dflags "Stg2Stg"
; us <- mkSplitUniqSupply 'g'
; doIfSet_dyn dflags Opt_D_verbose_stg2stg
(log_action dflags dflags SevDump noSrcSpan defaultDumpStyle (text "VERBOSE STG-TO-STG:"))
; (binds', us', ccs) <- end_pass us "Stg2Stg" ([],[],[]) binds
; let (us0, us1) = splitUniqSupply us'
; (processed_binds, _, cost_centres)
<- foldl_mn do_stg_pass (binds', us0, ccs) (getStgToDo dflags)
; let un_binds = unarise us1 processed_binds
; let srt_binds
| dopt Opt_TryNewCodeGen dflags = zip un_binds (repeat [])
| otherwise = computeSRTs un_binds
; dumpIfSet_dyn dflags Opt_D_dump_stg "STG syntax:"
(pprStgBindingsWithSRTs srt_binds)
; return (srt_binds, cost_centres)
}
where
stg_linter = if dopt Opt_DoStgLinting dflags
then lintStgBindings
else ( \ _whodunnit binds -> binds )
do_stg_pass (binds, us, ccs) to_do
= let
(us1, us2) = splitUniqSupply us
in
case to_do of
D_stg_stats ->
trace (showStgStats binds)
end_pass us2 "StgStats" ccs binds
StgDoMassageForProfiling ->
let
(collected_CCs, binds3)
= stgMassageForProfiling dflags module_name us1 binds
in
end_pass us2 "ProfMassage" collected_CCs binds3
end_pass us2 what ccs binds2
= do
dumpIfSet_dyn dflags Opt_D_verbose_stg2stg what
(vcat (map ppr binds2))
let linted_binds = stg_linter what binds2
return (linted_binds, us2, ccs)
foldl_mn :: (b -> a -> IO b) -> b -> [a] -> IO b
foldl_mn _ z [] = return z
foldl_mn f z (x:xs) = f z x >>= \ zz ->
foldl_mn f zz xs
\end{code}