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(..), AltCon(..), Arg, Note(..),
9 CoreExpr, CoreAlt, CoreBind, CoreArg, CoreBndr,
10 TaggedExpr, TaggedAlt, TaggedBind, TaggedArg,
13 mkApps, mkTyApps, mkValApps, mkVarApps,
14 mkLit, mkIntLitInt, mkIntLit,
15 mkStringLit, mkStringLitFS, mkConApp,
17 bindNonRec, mkIfThenElse, varToCoreExpr,
19 bindersOf, bindersOfBinds, rhssOfBind, rhssOfAlts, isTyVar, isId,
20 collectBinders, collectTyBinders, collectValBinders, collectTyAndValBinders,
21 collectArgs, collectBindersIgnoringNotes,
25 isValArg, isTypeArg, valArgCount, valBndrCount,
28 seqRules, seqExpr, seqExprs,
33 -- Annotated expressions
34 AnnExpr, AnnExpr'(..), AnnBind(..), AnnAlt, deAnnotate, deAnnotate',
37 CoreRules(..), -- Representation needed by friends
38 CoreRule(..), -- CoreSubst, CoreTidy, CoreFVs, PprCore only
40 emptyCoreRules, isEmptyCoreRules, rulesRhsFreeVars, rulesRules
43 #include "HsVersions.h"
45 import TysWiredIn ( boolTy, stringTy, nilDataCon )
46 import CostCentre ( CostCentre, noCostCentre )
47 import Var ( Var, Id, TyVar, isTyVar, isId, idType )
49 import Id ( mkWildId, idOccInfo, idInfo )
50 import Type ( Type, UsageAnn, mkTyVarTy, isUnLiftedType, seqType )
51 import IdInfo ( OccInfo(..), megaSeqIdInfo )
52 import Literal ( Literal(MachStr), mkMachInt )
53 import PrimOp ( PrimOp )
54 import DataCon ( DataCon, dataConId )
55 import TysWiredIn ( trueDataCon, falseDataCon )
56 import ThinAir ( unpackCStringId, unpackCString2Id, addr2IntegerId )
61 %************************************************************************
63 \subsection{The main data types}
65 %************************************************************************
67 These data types are the heart of the compiler
70 infixl 8 `App` -- App brackets to the left
72 data Expr b -- "b" for the type of binders,
75 | App (Expr b) (Arg b)
77 | Let (Bind b) (Expr b)
78 | Case (Expr b) b [Alt b] -- Binder gets bound to value of scrutinee
79 -- DEFAULT case must be last, if it occurs at all
81 | Type Type -- This should only show up at the top
84 type Arg b = Expr b -- Can be a Type
86 type Alt b = (AltCon, [b], Expr b) -- (DEFAULT, [], rhs) is the default alternative
88 data AltCon = DataAlt DataCon
93 data Bind b = NonRec b (Expr b)
100 Type -- The to-type: type of whole coerce expression
101 Type -- The from-type: type of enclosed expression
103 | InlineCall -- Instructs simplifier to inline
106 | InlineMe -- Instructs simplifer to treat the enclosed expression
107 -- as very small, and inline it at its call sites
109 | TermUsg -- A term-level usage annotation
110 UsageAnn -- (should not be a variable except during UsageSP inference)
114 %************************************************************************
116 \subsection{Transformation rules}
118 %************************************************************************
120 The CoreRule type and its friends are dealt with mainly in CoreRules,
121 but CoreFVs, Subst, PprCore, CoreTidy also inspect the representation.
126 VarSet -- Locally-defined free vars of RHSs
128 type RuleName = FAST_STRING
132 [CoreBndr] -- Forall'd variables
133 [CoreExpr] -- LHS args
136 | BuiltinRule -- Built-in rules are used for constant folding
137 -- and suchlike. It has no free variables.
138 ([CoreExpr] -> Maybe (RuleName, CoreExpr))
140 emptyCoreRules :: CoreRules
141 emptyCoreRules = Rules [] emptyVarSet
143 isEmptyCoreRules :: CoreRules -> Bool
144 isEmptyCoreRules (Rules rs _) = null rs
146 rulesRhsFreeVars :: CoreRules -> VarSet
147 rulesRhsFreeVars (Rules _ fvs) = fvs
149 rulesRules :: CoreRules -> [CoreRule]
150 rulesRules (Rules rules _) = rules
154 %************************************************************************
156 \subsection{The main data type}
158 %************************************************************************
161 -- The Ord is needed for the FiniteMap used in the lookForConstructor
162 -- in SimplEnv. If you declared that lookForConstructor *ignores*
163 -- constructor-applications with LitArg args, then you could get
166 instance Outputable AltCon where
167 ppr (DataAlt dc) = ppr dc
168 ppr (LitAlt lit) = ppr lit
169 ppr DEFAULT = ptext SLIT("__DEFAULT")
171 instance Show AltCon where
172 showsPrec p con = showsPrecSDoc p (ppr con)
176 %************************************************************************
178 \subsection{Useful synonyms}
180 %************************************************************************
186 type CoreExpr = Expr CoreBndr
187 type CoreArg = Arg CoreBndr
188 type CoreBind = Bind CoreBndr
189 type CoreAlt = Alt CoreBndr
193 Binders are ``tagged'' with a \tr{t}:
196 type Tagged t = (CoreBndr, t)
198 type TaggedBind t = Bind (Tagged t)
199 type TaggedExpr t = Expr (Tagged t)
200 type TaggedArg t = Arg (Tagged t)
201 type TaggedAlt t = Alt (Tagged t)
205 %************************************************************************
207 \subsection{Core-constructing functions with checking}
209 %************************************************************************
212 mkApps :: Expr b -> [Arg b] -> Expr b
213 mkTyApps :: Expr b -> [Type] -> Expr b
214 mkValApps :: Expr b -> [Expr b] -> Expr b
215 mkVarApps :: Expr b -> [Var] -> Expr b
217 mkApps f args = foldl App f args
218 mkTyApps f args = foldl (\ e a -> App e (Type a)) f args
219 mkValApps f args = foldl (\ e a -> App e a) f args
220 mkVarApps f vars = foldl (\ e a -> App e (varToCoreExpr a)) f vars
222 mkLit :: Literal -> Expr b
223 mkIntLit :: Integer -> Expr b
224 mkIntLitInt :: Int -> Expr b
225 mkStringLit :: String -> Expr b -- Makes a [Char] literal
226 mkStringLitFS :: FAST_STRING -> Expr b -- Makes a [Char] literal
227 mkConApp :: DataCon -> [Arg b] -> Expr b
230 mkConApp con args = mkApps (Var (dataConId con)) args
232 mkIntLit n = Lit (mkMachInt n)
233 mkIntLitInt n = Lit (mkMachInt (toInteger n))
235 mkStringLit str = mkStringLitFS (_PK_ str)
238 | any is_NUL (_UNPK_ str)
239 = -- Must cater for NULs in literal string
240 mkApps (Var unpackCString2Id)
242 mkIntLitInt (_LENGTH_ str)]
245 = -- No NULs in the string
246 App (Var unpackCStringId) (Lit (MachStr str))
251 varToCoreExpr :: CoreBndr -> Expr b
252 varToCoreExpr v | isId v = Var v
253 | otherwise = Type (mkTyVarTy v)
257 mkLams :: [b] -> Expr b -> Expr b
258 mkLams binders body = foldr Lam body binders
262 mkLets :: [Bind b] -> Expr b -> Expr b
263 mkLets binds body = foldr Let body binds
265 bindNonRec :: Id -> CoreExpr -> CoreExpr -> CoreExpr
266 -- (bindNonRec x r b) produces either
269 -- case r of x { _DEFAULT_ -> b }
271 -- depending on whether x is unlifted or not
272 -- It's used by the desugarer to avoid building bindings
273 -- that give Core Lint a heart attack. Actually the simplifier
274 -- deals with them perfectly well.
275 bindNonRec bndr rhs body
276 | isUnLiftedType (idType bndr) = Case rhs bndr [(DEFAULT,[],body)]
277 | otherwise = Let (NonRec bndr rhs) body
279 mkIfThenElse :: CoreExpr -> CoreExpr -> CoreExpr -> CoreExpr
280 mkIfThenElse guard then_expr else_expr
281 = Case guard (mkWildId boolTy)
282 [ (DataAlt trueDataCon, [], then_expr),
283 (DataAlt falseDataCon, [], else_expr) ]
288 mkAltExpr :: AltCon -> [CoreBndr] -> [Type] -> CoreExpr
289 -- This guy constructs the value that the scrutinee must have
290 -- when you are in one particular branch of a case
291 mkAltExpr (DataAlt con) args inst_tys
292 = mkConApp con (map Type inst_tys ++ map varToCoreExpr args)
293 mkAltExpr (LitAlt lit) [] []
298 %************************************************************************
300 \subsection{Simple access functions}
302 %************************************************************************
305 bindersOf :: Bind b -> [b]
306 bindersOf (NonRec binder _) = [binder]
307 bindersOf (Rec pairs) = [binder | (binder, _) <- pairs]
309 bindersOfBinds :: [Bind b] -> [b]
310 bindersOfBinds binds = foldr ((++) . bindersOf) [] binds
312 rhssOfBind :: Bind b -> [Expr b]
313 rhssOfBind (NonRec _ rhs) = [rhs]
314 rhssOfBind (Rec pairs) = [rhs | (_,rhs) <- pairs]
316 rhssOfAlts :: [Alt b] -> [Expr b]
317 rhssOfAlts alts = [e | (_,_,e) <- alts]
319 flattenBinds :: [Bind b] -> [(b, Expr b)] -- Get all the lhs/rhs pairs
320 flattenBinds (NonRec b r : binds) = (b,r) : flattenBinds binds
321 flattenBinds (Rec prs1 : binds) = prs1 ++ flattenBinds binds
325 We often want to strip off leading lambdas before getting down to
326 business. @collectBinders@ is your friend.
328 We expect (by convention) type-, and value- lambdas in that
332 collectBinders :: Expr b -> ([b], Expr b)
333 collectBindersIgnoringNotes :: Expr b -> ([b], Expr b)
334 collectTyBinders :: CoreExpr -> ([TyVar], CoreExpr)
335 collectValBinders :: CoreExpr -> ([Id], CoreExpr)
336 collectTyAndValBinders :: CoreExpr -> ([TyVar], [Id], CoreExpr)
341 go bs (Lam b e) = go (b:bs) e
342 go bs e = (reverse bs, e)
344 -- This one ignores notes. It's used in CoreUnfold and StrAnal
345 -- when we aren't going to put the expression back together from
346 -- the pieces, so we don't mind losing the Notes
347 collectBindersIgnoringNotes expr
350 go bs (Lam b e) = go (b:bs) e
351 go bs (Note _ e) = go bs e
352 go bs e = (reverse bs, e)
354 collectTyAndValBinders expr
357 (tvs, body1) = collectTyBinders expr
358 (ids, body) = collectValBinders body1
360 collectTyBinders expr
363 go tvs (Lam b e) | isTyVar b = go (b:tvs) e
364 go tvs e = (reverse tvs, e)
366 collectValBinders expr
369 go ids (Lam b e) | isId b = go (b:ids) e
370 go ids body = (reverse ids, body)
374 @collectArgs@ takes an application expression, returning the function
375 and the arguments to which it is applied.
378 collectArgs :: Expr b -> (Expr b, [Arg b])
382 go (App f a) as = go f (a:as)
386 coreExprCc gets the cost centre enclosing an expression, if any.
387 It looks inside lambdas because (scc "foo" \x.e) = \x.scc "foo" e
390 coreExprCc :: Expr b -> CostCentre
391 coreExprCc (Note (SCC cc) e) = cc
392 coreExprCc (Note other_note e) = coreExprCc e
393 coreExprCc (Lam _ e) = coreExprCc e
394 coreExprCc other = noCostCentre
398 %************************************************************************
400 \subsection{Predicates}
402 %************************************************************************
405 isValArg (Type _) = False
406 isValArg other = True
408 isTypeArg (Type _) = True
409 isTypeArg other = False
411 valBndrCount :: [CoreBndr] -> Int
413 valBndrCount (b : bs) | isId b = 1 + valBndrCount bs
414 | otherwise = valBndrCount bs
416 valArgCount :: [Arg b] -> Int
418 valArgCount (Type _ : args) = valArgCount args
419 valArgCount (other : args) = 1 + valArgCount args
423 %************************************************************************
425 \subsection{Seq stuff}
427 %************************************************************************
430 seqExpr :: CoreExpr -> ()
431 seqExpr (Var v) = v `seq` ()
432 seqExpr (Lit lit) = lit `seq` ()
433 seqExpr (App f a) = seqExpr f `seq` seqExpr a
434 seqExpr (Lam b e) = seqBndr b `seq` seqExpr e
435 seqExpr (Let b e) = seqBind b `seq` seqExpr e
436 seqExpr (Case e b as) = seqExpr e `seq` seqBndr b `seq` seqAlts as
437 seqExpr (Note n e) = seqNote n `seq` seqExpr e
438 seqExpr (Type t) = seqType t
441 seqExprs (e:es) = seqExpr e `seq` seqExprs es
443 seqNote (Coerce t1 t2) = seqType t1 `seq` seqType t2
446 seqBndr b = b `seq` ()
449 seqBndrs (b:bs) = seqBndr b `seq` seqBndrs bs
451 seqBind (NonRec b e) = seqBndr b `seq` seqExpr e
452 seqBind (Rec prs) = seqPairs prs
455 seqPairs ((b,e):prs) = seqBndr b `seq` seqExpr e `seq` seqPairs prs
458 seqAlts ((c,bs,e):alts) = seqBndrs bs `seq` seqExpr e `seq` seqAlts alts
460 seqRules :: CoreRules -> ()
461 seqRules (Rules rules fvs) = seq_rules rules `seq` seqVarSet fvs
464 seq_rules (Rule fs bs es e : rules) = seqBndrs bs `seq` seqExprs (e:es) `seq` seq_rules rules
465 seq_rules (BuiltinRule _ : rules) = seq_rules rules
469 coreBindsSize :: [CoreBind] -> Int
470 coreBindsSize bs = foldr ((+) . bindSize) 0 bs
472 exprSize :: CoreExpr -> Int
473 -- A measure of the size of the expressions
474 -- It also forces the expression pretty drastically as a side effect
475 exprSize (Var v) = varSize v
476 exprSize (Lit lit) = 1
477 exprSize (App f a) = exprSize f + exprSize a
478 exprSize (Lam b e) = varSize b + exprSize e
479 exprSize (Let b e) = bindSize b + exprSize e
480 exprSize (Case e b as) = exprSize e + varSize b + foldr ((+) . altSize) 0 as
481 exprSize (Note n e) = exprSize e
482 exprSize (Type t) = seqType t `seq`
485 exprsSize = foldr ((+) . exprSize) 0
487 varSize :: Var -> Int
488 varSize b | isTyVar b = 1
489 | otherwise = seqType (idType b) `seq`
490 megaSeqIdInfo (idInfo b) `seq`
493 varsSize = foldr ((+) . varSize) 0
495 bindSize (NonRec b e) = varSize b + exprSize e
496 bindSize (Rec prs) = foldr ((+) . pairSize) 0 prs
498 pairSize (b,e) = varSize b + exprSize e
500 altSize (c,bs,e) = c `seq` varsSize bs + exprSize e
504 %************************************************************************
506 \subsection{Annotated core; annotation at every node in the tree}
508 %************************************************************************
511 type AnnExpr bndr annot = (annot, AnnExpr' bndr annot)
513 data AnnExpr' bndr annot
516 | AnnLam bndr (AnnExpr bndr annot)
517 | AnnApp (AnnExpr bndr annot) (AnnExpr bndr annot)
518 | AnnCase (AnnExpr bndr annot) bndr [AnnAlt bndr annot]
519 | AnnLet (AnnBind bndr annot) (AnnExpr bndr annot)
520 | AnnNote Note (AnnExpr bndr annot)
523 type AnnAlt bndr annot = (AltCon, [bndr], AnnExpr bndr annot)
525 data AnnBind bndr annot
526 = AnnNonRec bndr (AnnExpr bndr annot)
527 | AnnRec [(bndr, AnnExpr bndr annot)]
531 deAnnotate :: AnnExpr bndr annot -> Expr bndr
532 deAnnotate (_, e) = deAnnotate' e
534 deAnnotate' (AnnType t) = Type t
535 deAnnotate' (AnnVar v) = Var v
536 deAnnotate' (AnnLit lit) = Lit lit
537 deAnnotate' (AnnLam binder body) = Lam binder (deAnnotate body)
538 deAnnotate' (AnnApp fun arg) = App (deAnnotate fun) (deAnnotate arg)
539 deAnnotate' (AnnNote note body) = Note note (deAnnotate body)
541 deAnnotate' (AnnLet bind body)
542 = Let (deAnnBind bind) (deAnnotate body)
544 deAnnBind (AnnNonRec var rhs) = NonRec var (deAnnotate rhs)
545 deAnnBind (AnnRec pairs) = Rec [(v,deAnnotate rhs) | (v,rhs) <- pairs]
547 deAnnotate' (AnnCase scrut v alts)
548 = Case (deAnnotate scrut) v (map deAnnAlt alts)
550 deAnnAlt (con,args,rhs) = (con,args,deAnnotate rhs)