2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1994
4 \section[Deforest]{Top level deforestation module}
6 >#include "HsVersions.h"
23 > import CmdLineOpts ( GlobalSwitch, SwitchResult )
25 > import Id ( getIdInfo, Id )
29 > import SimplEnv ( SwitchChecker(..) )
38 > domIdEnv = panic "Deforest: domIdEnv"
41 > :: SwitchChecker GlobalSwitch{-maybe-}
46 > deforestProgram sw prog uq =
48 > def_program = core2def sw prog
50 > defProg sw nullIdEnv def_program `thenSUs` \prog ->
56 We have to collect all the unfoldings (functions that were annotated
57 with DEFOREST) and pass them in an environment to subsequent calls of
60 Recursive functions are first transformed by the deforester. If the
61 function is annotated as deforestable, then it is converted to
62 treeless form for unfolding later on.
64 Also converting non-recursive functions that are annotated with
65 {-# DEFOREST #-} now. Probably don't need to convert these to treeless
66 form: just the inner recursive bindings they contain. eg:
68 repeat = \x -> letrec xs = x:xs in xs
70 is non-recursive, but we want to unfold it and annotate the binding
71 for xs as unfoldable, too.
74 > :: SwitchChecker GlobalSwitch{-maybe-}
77 > -> SUniqSM [DefBinding]
79 > defProg sw p [] = returnSUs []
81 > defProg sw p (CoNonRec v e : bs) =
82 > trace ("Processing: `" ++
83 > ppShow 80 (ppr PprDebug v) ++ "'\n") (
84 > tran sw p nullTyVarEnv e [] `thenSUs` \e ->
85 > mkLoops e `thenSUs` \(extracted,e) ->
86 > let e' = mkDefLetrec extracted e in
88 > if deforestable v then
89 > let (vs,es) = unzip extracted in
90 > convertToTreelessForm sw e `thenSUs` \e ->
91 > mapSUs (convertToTreelessForm sw) es `thenSUs` \es ->
92 > defProg sw (growIdEnvList p ((v,e):zip vs es)) bs
96 > returnSUs (CoNonRec v e' : bs)
99 > defProg sw p (CoRec bs : bs') =
100 > mapSUs (defRecBind sw p) bs `thenSUs` \res ->
102 > (resid, unfold) = unzip res
103 > p' = growIdEnvList p (concat unfold)
105 > defProg sw p' bs' `thenSUs` \bs' ->
106 > returnSUs (CoRec resid: bs')
110 > :: SwitchChecker GlobalSwitch{-maybe-}
113 > -> SUniqSM ((Id,DefExpr),[(Id,DefExpr)])
115 > defRecBind sw p (v,e) =
116 > trace ("Processing: `" ++
117 > ppShow 80 (ppr PprDebug v) ++ "'\n") (
118 > tran sw p nullTyVarEnv e [] `thenSUs` \e' ->
119 > mkLoops e' `thenSUs` \(bs,e') ->
120 > let e'' = mkDefLetrec bs e' in
122 > d2c e'' `thenSUs` \core_e ->
123 > let showBind (v,e) = ppShow 80 (ppr PprDebug v) ++
124 > "=\n" ++ ppShow 80 (ppr PprDebug e) ++ "\n"
126 > trace ("Extracting from `" ++
127 > ppShow 80 (ppr PprDebug v) ++ "'\n"
128 > ++ "{ result:\n" ++ showBind (v,core_e) ++ "}\n") $
132 > let (vs,es) = unzip bs in
133 > convertToTreelessForm sw e' `thenSUs` \e' ->
134 > mapSUs (convertToTreelessForm sw) es `thenSUs` \es ->
135 > returnSUs ((v,e''),(v,e'):zip vs es)
137 > trace (show (length bs)) (
138 > returnSUs ((v,e''),[])