2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[CoreSyn]{A data type for the Haskell compiler midsection}
8 Expr(..), Alt, Bind(..), Arg(..), Note(..),
9 CoreExpr, CoreAlt, CoreBind, CoreArg, CoreBndr,
10 TaggedExpr, TaggedAlt, TaggedBind, TaggedArg,
13 mkApps, mkTyApps, mkValApps,
14 mkLit, mkStringLit, mkConApp, mkPrimApp, mkNote, mkNilExpr,
15 bindNonRec, mkIfThenElse, varToCoreExpr,
17 bindersOf, rhssOfBind, rhssOfAlts, isDeadBinder, isTyVar, isId,
18 collectBinders, collectTyBinders, collectValBinders, collectTyAndValBinders,
22 isValArg, isTypeArg, valArgCount,
24 -- Annotated expressions
25 AnnExpr, AnnExpr'(..), AnnBind(..), AnnAlt, deAnnotate
28 #include "HsVersions.h"
30 import TysWiredIn ( boolTy, stringTy, nilDataCon )
31 import CostCentre ( CostCentre, isDupdCC, noCostCentre )
32 import Var ( Var, GenId, Id, TyVar, IdOrTyVar, isTyVar, isId, idType )
33 import Id ( mkWildId, getInlinePragma )
34 import Type ( GenType, Type, mkTyVarTy, isUnLiftedType )
35 import IdInfo ( InlinePragInfo(..) )
36 import BasicTypes ( Unused )
37 import Const ( Con(..), DataCon, Literal(NoRepStr), PrimOp )
38 import TysWiredIn ( trueDataCon, falseDataCon )
42 %************************************************************************
44 \subsection{The main data types}
46 %************************************************************************
48 These data types are the heart of the compiler
51 data Expr b f -- "b" for the type of binders,
52 -- "f" for the flexi slot in types
54 | Con Con [Arg b f] -- Guaranteed saturated
55 | App (Expr b f) (Arg b f)
57 | Let (Bind b f) (Expr b f)
58 | Case (Expr b f) b [Alt b f] -- Binder gets bound to value of scrutinee
59 -- DEFAULT case must be last, if it occurs at all
60 | Note (Note f) (Expr b f)
61 | Type (GenType f) -- This should only show up at the top
64 type Arg b f = Expr b f -- Can be a Type
66 type Alt b f = (Con, [b], Expr b f)
67 -- (DEFAULT, [], rhs) is the default alternative
68 -- Remember, a Con can be a literal or a data constructor
70 data Bind b f = NonRec b (Expr b f)
71 | Rec [(b, (Expr b f))]
77 (GenType f) -- The to-type: type of whole coerce expression
78 (GenType f) -- The from-type: type of enclosed expression
80 | InlineCall -- Instructs simplifier to inline
85 %************************************************************************
87 \subsection{Useful synonyms}
89 %************************************************************************
94 type CoreBndr = IdOrTyVar
95 type CoreExpr = Expr CoreBndr Unused
96 type CoreArg = Arg CoreBndr Unused
97 type CoreBind = Bind CoreBndr Unused
98 type CoreAlt = Alt CoreBndr Unused
99 type CoreNote = Note Unused
102 Binders are ``tagged'' with a \tr{t}:
105 type Tagged t = (CoreBndr, t)
107 type TaggedBind t = Bind (Tagged t) Unused
108 type TaggedExpr t = Expr (Tagged t) Unused
109 type TaggedArg t = Arg (Tagged t) Unused
110 type TaggedAlt t = Alt (Tagged t) Unused
114 %************************************************************************
116 \subsection{Core-constructing functions with checking}
118 %************************************************************************
121 mkApps :: Expr b f -> [Arg b f] -> Expr b f
122 mkTyApps :: Expr b f -> [GenType f] -> Expr b f
123 mkValApps :: Expr b f -> [Expr b f] -> Expr b f
125 mkApps f args = foldl App f args
126 mkTyApps f args = foldl (\ e a -> App e (Type a)) f args
127 mkValApps f args = foldl (\ e a -> App e a) f args
129 mkLit :: Literal -> Expr b f
130 mkStringLit :: String -> Expr b f
131 mkConApp :: DataCon -> [Arg b f] -> Expr b f
132 mkPrimApp :: PrimOp -> [Arg b f] -> Expr b f
134 mkLit lit = Con (Literal lit) []
135 mkStringLit str = Con (Literal (NoRepStr (_PK_ str) stringTy)) []
136 mkConApp con args = Con (DataCon con) args
137 mkPrimApp op args = Con (PrimOp op) args
139 mkNilExpr :: Type -> CoreExpr
140 mkNilExpr ty = Con (DataCon nilDataCon) [Type ty]
142 varToCoreExpr :: CoreBndr -> CoreExpr
143 varToCoreExpr v | isId v = Var v
144 | otherwise = Type (mkTyVarTy v)
150 mkLets :: [Bind b f] -> Expr b f -> Expr b f
151 mkLets binds body = foldr Let body binds
153 mkLams :: [b] -> Expr b f -> Expr b f
154 mkLams binders body = foldr Lam body binders
158 bindNonRec :: Id -> CoreExpr -> CoreExpr -> CoreExpr
159 -- (bindNonRec x r b) produces either
162 -- case r of x { _DEFAULT_ -> b }
164 -- depending on whether x is unlifted or not
165 bindNonRec bndr rhs body
166 | isUnLiftedType (idType bndr) = Case rhs bndr [(DEFAULT,[],body)]
167 | otherwise = Let (NonRec bndr rhs) body
169 mkIfThenElse :: CoreExpr -> CoreExpr -> CoreExpr -> CoreExpr
170 mkIfThenElse guard then_expr else_expr
171 = Case guard (mkWildId boolTy)
172 [ (DataCon trueDataCon, [], then_expr),
173 (DataCon falseDataCon, [], else_expr) ]
176 mkNote removes redundant coercions, and SCCs where possible
179 mkNote :: Note f -> Expr b f -> Expr b f
180 mkNote (Coerce to_ty1 from_ty1) (Note (Coerce to_ty2 from_ty2) expr)
181 = ASSERT( from_ty1 == to_ty2 )
182 mkNote (Coerce to_ty1 from_ty2) expr
184 mkNote (SCC cc1) expr@(Note (SCC cc2) _)
185 | isDupdCC cc1 -- Discard the outer SCC provided we don't need
186 = expr -- to track its entry count
188 mkNote note@(SCC cc1) expr@(Lam x e) -- Move _scc_ inside lambda
189 = Lam x (mkNote note e)
191 -- Slide InlineCall in around the function
192 mkNote InlineCall (App f a) = App (mkNote InlineCall f) a
193 mkNote InlineCall (Var v) = Note InlineCall (Var v)
194 mkNote InlineCall expr = expr
196 mkNote note expr = Note note expr
199 %************************************************************************
201 \subsection{Simple access functions}
203 %************************************************************************
206 bindersOf :: Bind b f -> [b]
207 bindersOf (NonRec binder _) = [binder]
208 bindersOf (Rec pairs) = [binder | (binder, _) <- pairs]
210 rhssOfBind :: Bind b f -> [Expr b f]
211 rhssOfBind (NonRec _ rhs) = [rhs]
212 rhssOfBind (Rec pairs) = [rhs | (_,rhs) <- pairs]
214 rhssOfAlts :: [Alt b f] -> [Expr b f]
215 rhssOfAlts alts = [e | (_,_,e) <- alts]
217 isDeadBinder :: CoreBndr -> Bool
218 isDeadBinder bndr | isId bndr = case getInlinePragma bndr of
221 | otherwise = False -- TyVars count as not dead
224 We often want to strip off leading lambdas before getting down to
225 business. @collectBinders@ is your friend.
227 We expect (by convention) type-, and value- lambdas in that
231 collectBinders :: Expr b f -> ([b], Expr b f)
232 collectTyBinders :: CoreExpr -> ([TyVar], CoreExpr)
233 collectValBinders :: CoreExpr -> ([Id], CoreExpr)
234 collectTyAndValBinders :: CoreExpr -> ([TyVar], [Id], CoreExpr)
236 collectTyAndValBinders expr
239 (tvs, body1) = collectTyBinders expr
240 (ids, body) = collectValBinders body1
245 go tvs (Lam b e) = go (b:tvs) e
246 go tvs e = (reverse tvs, e)
248 collectTyBinders expr
251 go tvs (Lam b e) | isTyVar b = go (b:tvs) e
252 go tvs e = (reverse tvs, e)
254 collectValBinders expr
257 go ids (Lam b e) | isId b = go (b:ids) e
258 go ids body = (reverse ids, body)
262 @collectArgs@ takes an application expression, returning the function
263 and the arguments to which it is applied.
266 collectArgs :: Expr b f -> (Expr b f, [Arg b f])
270 go (App f a) as = go f (a:as)
274 coreExprCc gets the cost centre enclosing an expression, if any.
275 It looks inside lambdas because (scc "foo" \x.e) = \x.scc "foo" e
278 coreExprCc :: Expr b f -> CostCentre
279 coreExprCc (Note (SCC cc) e) = cc
280 coreExprCc (Note other_note e) = coreExprCc e
281 coreExprCc (Lam _ e) = coreExprCc e
282 coreExprCc other = noCostCentre
286 %************************************************************************
288 \subsection{Predicates}
290 %************************************************************************
293 isValArg (Type _) = False
294 isValArg other = True
296 isTypeArg (Type _) = True
297 isTypeArg other = False
299 valArgCount :: [Arg b f] -> Int
301 valArgCount (Type _ : args) = valArgCount args
302 valArgCount (other : args) = 1 + valArgCount args
306 %************************************************************************
308 \subsection{Annotated core; annotation at every node in the tree}
310 %************************************************************************
313 type AnnExpr bndr annot = (annot, AnnExpr' bndr annot)
315 data AnnExpr' bndr annot
317 | AnnCon Con [AnnExpr bndr annot]
318 | AnnLam bndr (AnnExpr bndr annot)
319 | AnnApp (AnnExpr bndr annot) (AnnExpr bndr annot)
320 | AnnCase (AnnExpr bndr annot) bndr [AnnAlt bndr annot]
321 | AnnLet (AnnBind bndr annot) (AnnExpr bndr annot)
322 | AnnNote (Note Unused) (AnnExpr bndr annot)
325 type AnnAlt bndr annot = (Con, [bndr], AnnExpr bndr annot)
327 data AnnBind bndr annot
328 = AnnNonRec bndr (AnnExpr bndr annot)
329 | AnnRec [(bndr, AnnExpr bndr annot)]
333 deAnnotate :: AnnExpr bndr annot -> Expr bndr Unused
335 deAnnotate (_, AnnType t) = Type t
336 deAnnotate (_, AnnVar v) = Var v
337 deAnnotate (_, AnnCon con args) = Con con (map deAnnotate args)
338 deAnnotate (_, AnnLam binder body)= Lam binder (deAnnotate body)
339 deAnnotate (_, AnnApp fun arg) = App (deAnnotate fun) (deAnnotate arg)
340 deAnnotate (_, AnnNote note body) = Note note (deAnnotate body)
342 deAnnotate (_, AnnLet bind body)
343 = Let (deAnnBind bind) (deAnnotate body)
345 deAnnBind (AnnNonRec var rhs) = NonRec var (deAnnotate rhs)
346 deAnnBind (AnnRec pairs) = Rec [(v,deAnnotate rhs) | (v,rhs) <- pairs]
348 deAnnotate (_, AnnCase scrut v alts)
349 = Case (deAnnotate scrut) v (map deAnnAlt alts)
351 deAnnAlt (con,args,rhs) = (con,args,deAnnotate rhs)