2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[DsBinds]{Pattern-matching bindings (HsBinds and MonoBinds)}
6 Handles @HsBinds@; those at the top level require different handling,
7 in that the @Rec@/@NonRec@/etc structure is thrown away (whereas at
8 lower levels it is preserved with @let@/@letrec@s).
11 module DsBinds ( dsMonoBinds, AutoScc(..) ) where
13 #include "HsVersions.h"
16 import {-# SOURCE #-} DsExpr( dsExpr )
18 import HsSyn -- lots of things
19 import CoreSyn -- lots of things
20 import CoreUtils ( coreExprType )
21 import TcHsSyn ( TypecheckedMonoBinds )
23 import DsGRHSs ( dsGuarded )
25 import Match ( matchWrapper )
27 import BasicTypes ( RecFlag(..) )
28 import CmdLineOpts ( opt_SccProfilingOn, opt_AutoSccsOnAllToplevs,
29 opt_AutoSccsOnExportedToplevs, opt_AutoSccsOnDicts
31 import CostCentre ( CostCentre, mkAutoCC, IsCafCC(..) )
32 import Id ( idType, Id )
34 import Name ( isExported )
35 import Type ( mkTyVarTy, isDictTy, substTy )
36 import TysWiredIn ( voidTy )
43 %************************************************************************
45 \subsection[dsMonoBinds]{Desugaring a @MonoBinds@}
47 %************************************************************************
50 dsMonoBinds :: AutoScc -- scc annotation policy (see below)
51 -> TypecheckedMonoBinds
52 -> [(Id,CoreExpr)] -- Put this on the end (avoid quadratic append)
53 -> DsM [(Id,CoreExpr)] -- Result
55 dsMonoBinds _ EmptyMonoBinds rest = returnDs rest
57 dsMonoBinds auto_scc (AndMonoBinds binds_1 binds_2) rest
58 = dsMonoBinds auto_scc binds_2 rest `thenDs` \ rest' ->
59 dsMonoBinds auto_scc binds_1 rest'
61 dsMonoBinds _ (CoreMonoBind var core_expr) rest
62 = returnDs ((var, core_expr) : rest)
64 dsMonoBinds _ (VarMonoBind var expr) rest
65 = dsExpr expr `thenDs` \ core_expr ->
67 -- Dictionary bindings are always VarMonoBinds, so
68 -- we only need do this here
69 addDictScc var core_expr `thenDs` \ core_expr' ->
71 returnDs ((var, core_expr') : rest)
73 dsMonoBinds auto_scc (FunMonoBind fun _ matches locn) rest
75 matchWrapper (FunMatch fun) matches error_string `thenDs` \ (args, body) ->
76 addAutoScc auto_scc (fun, mkLams args body) `thenDs` \ pair ->
77 returnDs (pair : rest)
79 error_string = "function " ++ showSDoc (ppr fun)
81 dsMonoBinds auto_scc (PatMonoBind pat grhss locn) rest
83 dsGuarded grhss `thenDs` \ body_expr ->
84 mkSelectorBinds pat body_expr `thenDs` \ sel_binds ->
85 mapDs (addAutoScc auto_scc) sel_binds `thenDs` \ sel_binds ->
86 returnDs (sel_binds ++ rest)
88 -- Common case: one exported variable
89 -- All non-recursive bindings come through this way
90 dsMonoBinds auto_scc (AbsBinds all_tyvars dicts exps@[(tyvars, global, local)] binds) rest
91 = ASSERT( all (`elem` tyvars) all_tyvars )
92 dsMonoBinds (addSccs auto_scc exps) binds [] `thenDs` \ core_prs ->
94 -- Always treat the binds as recursive, because the typechecker
95 -- makes rather mixed-up dictionary bindings
96 core_binds = [Rec core_prs]
97 global' = (global, mkLams tyvars $ mkLams dicts $
98 mkLets core_binds (Var local))
101 returnDs (global' : rest)
103 -- Another Common special case: no type or dictionary abstraction
104 dsMonoBinds auto_scc (AbsBinds [] [] exports binds) rest
105 = let exports' = [(global, Var local) | (_, global, local) <- exports] in
106 dsMonoBinds (addSccs auto_scc exports) binds (exports' ++ rest)
108 dsMonoBinds auto_scc (AbsBinds all_tyvars dicts exports binds) rest
109 = dsMonoBinds (addSccs auto_scc exports) binds []`thenDs` \ core_prs ->
111 core_binds = [Rec core_prs]
113 tup_expr = mkTupleExpr locals
114 tup_ty = coreExprType tup_expr
115 poly_tup_expr = mkLams all_tyvars $ mkLams dicts $
116 mkLets core_binds tup_expr
117 locals = [local | (_, _, local) <- exports]
118 local_tys = map idType locals
120 newSysLocalDs (coreExprType poly_tup_expr) `thenDs` \ poly_tup_id ->
122 dict_args = map Var dicts
124 mk_bind (tyvars, global, local) n -- locals !! n == local
125 = -- Need to make fresh locals to bind in the selector, because
126 -- some of the tyvars will be bound to voidTy
127 newSysLocalsDs (map (substTy env) local_tys) `thenDs` \ locals' ->
128 newSysLocalDs (substTy env tup_ty) `thenDs` \ tup_id ->
129 returnDs (global, mkLams tyvars $ mkLams dicts $
130 mkTupleSelector locals' (locals' !! n) tup_id $
131 mkApps (mkTyApps (Var poly_tup_id) ty_args) dict_args)
133 mk_ty_arg all_tyvar | all_tyvar `elem` tyvars = mkTyVarTy all_tyvar
135 ty_args = map mk_ty_arg all_tyvars
136 env = all_tyvars `zipVarEnv` ty_args
138 zipWithDs mk_bind exports [0..] `thenDs` \ export_binds ->
139 -- don't scc (auto-)annotate the tuple itself.
140 returnDs ((poly_tup_id, poly_tup_expr) : (export_binds ++ rest))
144 %************************************************************************
146 \subsection[addAutoScc]{Adding automatic sccs}
148 %************************************************************************
153 | TopLevelAddSccs (Id -> Maybe Id)
156 addSccs :: AutoScc -> [(a,Id,Id)] -> AutoScc
157 addSccs auto_scc@(TopLevelAddSccs _) exports = auto_scc
158 addSccs NoSccs exports = NoSccs
159 addSccs TopLevel exports
160 = TopLevelAddSccs (\id -> case [ exp | (_,exp,loc) <- exports, loc == id ] of
161 (exp:_) | opt_AutoSccsOnAllToplevs ||
163 opt_AutoSccsOnExportedToplevs)
167 addAutoScc :: AutoScc -- if needs be, decorate toplevs?
169 -> DsM (Id, CoreExpr)
171 addAutoScc (TopLevelAddSccs auto_scc_fn) pair@(bndr, core_expr)
172 | do_auto_scc && worthSCC core_expr
173 = getModuleAndGroupDs `thenDs` \ (mod,grp) ->
174 returnDs (bndr, Note (SCC (mkAutoCC top_bndr mod grp NotCafCC)) core_expr)
175 where do_auto_scc = isJust maybe_auto_scc
176 maybe_auto_scc = auto_scc_fn bndr
177 (Just top_bndr) = maybe_auto_scc
181 worthSCC (Note (SCC _) _) = False
182 worthSCC (Con _ _) = False
183 worthSCC core_expr = True
186 If profiling and dealing with a dict binding, wrap the dict in "_scc_ DICT <dict>":
189 addDictScc var rhs = returnDs rhs
191 {- DISABLED for now (need to somehow make up a name for the scc) -- SDM
192 | not ( opt_SccProfilingOn && opt_AutoSccsOnDicts)
193 || not (isDictTy (idType var))
194 = returnDs rhs -- That's easy: do nothing
197 = getModuleAndGroupDs `thenDs` \ (mod, grp) ->
198 -- ToDo: do -dicts-all flag (mark dict things with individual CCs)
199 returnDs (Note (SCC (mkAllDictsCC mod grp False)) rhs)