2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[CoreSyn]{A data type for the Haskell compiler midsection}
7 #include "HsVersions.h"
10 GenCoreBinding(..), GenCoreExpr(..),
11 GenCoreArg(..), GenCoreBinder(..), GenCoreCaseAlts(..),
12 GenCoreCaseDefault(..),
15 bindersOf, pairsFromCoreBinds, rhssOfBind,
17 mkGenApp, mkValApp, mkTyApp, mkUseApp,
19 mkValLam, mkTyLam, mkUseLam,
21 collectBinders, isValBinder, notValBinder,
23 collectArgs, isValArg, notValArg, numValArgs,
25 mkCoLetAny, mkCoLetNoUnboxed, mkCoLetUnboxedToCase,
26 mkCoLetsAny, mkCoLetsNoUnboxed, mkCoLetsUnboxedToCase,
27 mkCoLetrecAny, mkCoLetrecNoUnboxed,
31 -- Common type instantiation...
39 -- And not-so-common type instantiations...
40 TaggedCoreBinding(..),
44 TaggedCoreCaseAlts(..),
45 TaggedCoreCaseDefault(..),
47 SimplifiableCoreBinding(..),
48 SimplifiableCoreExpr(..),
49 SimplifiableCoreBinder(..),
50 SimplifiableCoreArg(..),
51 SimplifiableCoreCaseAlts(..),
52 SimplifiableCoreCaseDefault(..)
58 --import PprCore ( GenCoreExpr{-instance-} )
59 --import PprStyle ( PprStyle(..) )
61 import CostCentre ( showCostCentre, CostCentre )
62 import Id ( idType, GenId{-instance Eq-} )
63 import Type ( isUnboxedType )
64 import Usage ( UVar(..) )
65 import Util ( panic, assertPanic {-pprTrace:ToDo:rm-} )
68 %************************************************************************
70 \subsection[CoreTopBinding_and_CoreBinding]{@CoreTopBinding@ and @GenCoreBinding@}
72 %************************************************************************
74 Core programs, bindings, expressions, etc., are parameterised with
75 respect to the information kept about binding and bound occurrences of
76 variables, called {\em binders} and {\em val_occ tyvar uvars}, respectively. [I
77 don't really like the pair of names; I prefer {\em binder} and {\em
78 bounder}. Or {\em binder} and {\em var}.]
80 A @GenCoreBinding@ is either a single non-recursive binding of a
81 ``binder'' to an expression, or a mutually-recursive blob of same.
83 data GenCoreBinding val_bdr val_occ tyvar uvar
84 = NonRec val_bdr (GenCoreExpr val_bdr val_occ tyvar uvar)
85 | Rec [(val_bdr, GenCoreExpr val_bdr val_occ tyvar uvar)]
89 bindersOf :: GenCoreBinding val_bdr val_occ tyvar uvar -> [val_bdr]
92 [GenCoreBinding val_bdr val_occ tyvar uvar] ->
93 [(val_bdr, GenCoreExpr val_bdr val_occ tyvar uvar)]
95 rhssOfBind :: GenCoreBinding val_bdr val_occ tyvar uvar -> [GenCoreExpr val_bdr val_occ tyvar uvar]
97 bindersOf (NonRec binder _) = [binder]
98 bindersOf (Rec pairs) = [binder | (binder, _) <- pairs]
100 pairsFromCoreBinds [] = []
101 pairsFromCoreBinds ((NonRec b e) : bs) = (b,e) : pairsFromCoreBinds bs
102 pairsFromCoreBinds ((Rec pairs) : bs) = pairs ++ pairsFromCoreBinds bs
104 rhssOfBind (NonRec _ rhs) = [rhs]
105 rhssOfBind (Rec pairs) = [rhs | (_,rhs) <- pairs]
108 %************************************************************************
110 \subsection[GenCoreExpr]{Core expressions: @GenCoreExpr@}
112 %************************************************************************
114 @GenCoreExpr@ is the heart of the ``core'' data types; it is
115 (more-or-less) boiled-down second-order polymorphic lambda calculus.
116 For types in the core world, we just keep using @Types@.
118 data GenCoreExpr val_bdr val_occ tyvar uvar
120 | Lit Literal -- literal constants
123 @Cons@ and @Prims@ are saturated constructor and primitive-op
124 applications (see the comment). Note: @Con@s are only set up by the
125 simplifier (and by the desugarer when it knows what it's doing). The
126 desugarer sets up constructors as applications of global @Vars@s.
129 | Con Id [GenCoreArg val_occ tyvar uvar]
130 -- Saturated constructor application:
131 -- The constructor is a function of the form:
132 -- /\ a1 -> ... /\ am -> \ b1 -> ... \ bn ->
133 -- <expr> where "/\" is a type lambda and "\" the
134 -- regular kind; there will be "m" Types and
135 -- "n" bindees in the Con args.
137 | Prim PrimOp [GenCoreArg val_occ tyvar uvar]
138 -- saturated primitive operation;
139 -- comment on Cons applies here, too.
142 Ye olde abstraction and application operators.
144 | Lam (GenCoreBinder val_bdr tyvar uvar)
145 (GenCoreExpr val_bdr val_occ tyvar uvar)
147 | App (GenCoreExpr val_bdr val_occ tyvar uvar)
148 (GenCoreArg val_occ tyvar uvar)
151 Case expressions (\tr{case <expr> of <List of alternatives>}): there
152 are really two flavours masquerading here---those for scrutinising
153 {\em algebraic} types and those for {\em primitive} types. Please see
154 under @GenCoreCaseAlts@.
156 | Case (GenCoreExpr val_bdr val_occ tyvar uvar)
157 (GenCoreCaseAlts val_bdr val_occ tyvar uvar)
160 A Core case expression \tr{case e of v -> ...} implies evaluation of
161 \tr{e}; it is not equivalent to \tr{let v = in ...} (as with a Haskell
164 Non-recursive @Lets@ only have one binding; having more than one
165 doesn't buy you much, and it is an easy way to mess up variable
168 | Let (GenCoreBinding val_bdr val_occ tyvar uvar)
169 (GenCoreExpr val_bdr val_occ tyvar uvar)
170 -- both recursive and non-.
171 -- The "GenCoreBinding" records that information
174 For cost centre scc expressions we introduce a new core construct
175 @SCC@ so transforming passes have to deal with it explicitly. The
176 alternative of using a new PrimativeOp may result in a bad
177 transformations of which we are unaware.
179 | SCC CostCentre -- label of scc
180 (GenCoreExpr val_bdr val_occ tyvar uvar) -- scc expression
183 Coercions arise from uses of the constructor of a @newtype@
184 declaration, either in construction (resulting in a @CoreceIn@) or
185 pattern matching (resulting in a @CoerceOut@).
189 (GenType tyvar uvar) -- Type of the whole expression
190 (GenCoreExpr val_bdr val_occ tyvar uvar)
194 data Coercion = CoerceIn Id -- Apply this constructor
195 | CoerceOut Id -- Strip this constructor
199 %************************************************************************
201 \subsection{Core-constructing functions with checking}
203 %************************************************************************
205 When making @Lets@, we may want to take evasive action if the thing
206 being bound has unboxed type. We have different variants ...
208 @mkCoLet(s|rec)Any@ let-binds any binding, regardless of type
209 @mkCoLet(s|rec)NoUnboxed@ prohibits unboxed bindings
210 @mkCoLet(s)UnboxedToCase@ converts an unboxed binding to a case
211 (unboxed bindings in a letrec are still prohibited)
214 mkCoLetAny :: GenCoreBinding Id Id tyvar uvar
215 -> GenCoreExpr Id Id tyvar uvar
216 -> GenCoreExpr Id Id tyvar uvar
217 mkCoLetsAny :: [GenCoreBinding Id Id tyvar uvar] ->
218 GenCoreExpr Id Id tyvar uvar ->
219 GenCoreExpr Id Id tyvar uvar
221 mkCoLetrecAny :: [(val_bdr, GenCoreExpr val_bdr val_occ tyvar uvar)]
222 -> GenCoreExpr val_bdr val_occ tyvar uvar
223 -> GenCoreExpr val_bdr val_occ tyvar uvar
225 mkCoLetrecAny [] body = body
226 mkCoLetrecAny binds body = Let (Rec binds) body
228 mkCoLetsAny [] expr = expr
229 mkCoLetsAny binds expr = foldr mkCoLetAny expr binds
231 mkCoLetAny bind@(Rec binds) body = mkCoLetrecAny binds body
232 mkCoLetAny bind@(NonRec binder rhs) body
234 Var binder2 | binder == binder2
235 -> rhs -- hey, I have the rhs
241 --mkCoLetNoUnboxed ::
242 -- GenCoreBinding val_bdr val_occ tyvar uvar ->
243 -- GenCoreExpr val_bdr val_occ tyvar uvar ->
244 -- GenCoreExpr val_bdr val_occ tyvar uvar
246 mkCoLetNoUnboxed bind@(Rec binds) body
247 = mkCoLetrecNoUnboxed binds body
248 mkCoLetNoUnboxed bind@(NonRec binder rhs) body
249 = --ASSERT (not (isUnboxedType (idType binder)))
251 Var binder2 | binder == binder2
252 -> rhs -- hey, I have the rhs
256 mkCoLetsNoUnboxed [] expr = expr
257 mkCoLetsNoUnboxed binds expr = foldr mkCoLetNoUnboxed expr binds
259 mkCoLetrecNoUnboxed :: [(GenId (GenType a b), GenCoreExpr (GenId (GenType a b)) c d e)]
260 -> GenCoreExpr (GenId (GenType a b)) c d e
261 -> GenCoreExpr (GenId (GenType a b)) c d e
263 mkCoLetrecNoUnboxed [] body = body
264 mkCoLetrecNoUnboxed binds body
265 = ASSERT (all is_boxed_bind binds)
268 is_boxed_bind (binder, rhs)
269 = (not . isUnboxedType . idType) binder
273 --mkCoLetUnboxedToCase ::
274 -- GenCoreBinding val_bdr val_occ tyvar uvar ->
275 -- GenCoreExpr val_bdr val_occ tyvar uvar ->
276 -- GenCoreExpr val_bdr val_occ tyvar uvar
278 mkCoLetUnboxedToCase bind@(Rec binds) body
279 = mkCoLetrecNoUnboxed binds body
280 mkCoLetUnboxedToCase bind@(NonRec binder rhs) body
282 Var binder2 | binder == binder2
283 -> rhs -- hey, I have the rhs
285 -> if (not (isUnboxedType (idType binder))) then
286 Let bind body -- boxed...
288 Case rhs -- unboxed...
290 (BindDefault binder body))
292 mkCoLetsUnboxedToCase [] expr = expr
293 mkCoLetsUnboxedToCase binds expr = foldr mkCoLetUnboxedToCase expr binds
296 %************************************************************************
298 \subsection{Case alternatives in @GenCoreExpr@}
300 %************************************************************************
302 We have different kinds of @case@s, the differences being reflected in
303 the kinds of alternatives a case has. We maintain a distinction
304 between cases for scrutinising algebraic datatypes, as opposed to
305 primitive types. In both cases, we carry around a @TyCon@, as a
306 handle with which we can get info about the case (e.g., total number
307 of data constructors for this type).
315 Case e [ BindDefaultAlt x -> b ]
319 data GenCoreCaseAlts val_bdr val_occ tyvar uvar
320 = AlgAlts [(Id, -- alts: data constructor,
321 [val_bdr], -- constructor's parameters,
322 GenCoreExpr val_bdr val_occ tyvar uvar)] -- rhs.
323 (GenCoreCaseDefault val_bdr val_occ tyvar uvar)
325 | PrimAlts [(Literal, -- alts: unboxed literal,
326 GenCoreExpr val_bdr val_occ tyvar uvar)] -- rhs.
327 (GenCoreCaseDefault val_bdr val_occ tyvar uvar)
329 -- obvious things: if there are no alts in the list, then the default
330 -- can't be NoDefault.
332 data GenCoreCaseDefault val_bdr val_occ tyvar uvar
333 = NoDefault -- small con family: all
334 -- constructor accounted for
335 | BindDefault val_bdr -- form: var -> expr;
336 (GenCoreExpr val_bdr val_occ tyvar uvar) -- "val_bdr" may or may not
341 rhssOfAlts (AlgAlts alts deflt) = rhssOfDeflt deflt ++ [rhs | (_,_,rhs) <- alts]
342 rhssOfAlts (PrimAlts alts deflt) = rhssOfDeflt deflt ++ [rhs | (_,rhs) <- alts]
344 rhssOfDeflt NoDefault = []
345 rhssOfDeflt (BindDefault _ rhs) = [rhs]
348 %************************************************************************
350 \subsection{Core binders}
352 %************************************************************************
355 data GenCoreBinder val_bdr tyvar uvar
360 isValBinder (ValBinder _) = True
361 isValBinder _ = False
363 notValBinder = not . isValBinder
366 Clump Lams together if possible.
369 mkValLam :: [val_bdr]
370 -> GenCoreExpr val_bdr val_occ tyvar uvar
371 -> GenCoreExpr val_bdr val_occ tyvar uvar
373 -> GenCoreExpr val_bdr val_occ tyvar uvar
374 -> GenCoreExpr val_bdr val_occ tyvar uvar
376 -> GenCoreExpr val_bdr val_occ tyvar uvar
377 -> GenCoreExpr val_bdr val_occ tyvar uvar
379 mkValLam binders body = foldr (Lam . ValBinder) body binders
380 mkTyLam binders body = foldr (Lam . TyBinder) body binders
381 mkUseLam binders body = foldr (Lam . UsageBinder) body binders
383 mkLam :: [tyvar] -> [val_bdr] -- ToDo: could add a [uvar] arg...
384 -> GenCoreExpr val_bdr val_occ tyvar uvar
385 -> GenCoreExpr val_bdr val_occ tyvar uvar
387 mkLam tyvars valvars body
388 = mkTyLam tyvars (mkValLam valvars body)
391 We often want to strip off leading lambdas before getting down to
392 business. @collectBinders@ is your friend.
394 We expect (by convention) usage-, type-, and value- lambdas in that
399 GenCoreExpr val_bdr val_occ tyvar uvar ->
400 ([uvar], [tyvar], [val_bdr], GenCoreExpr val_bdr val_occ tyvar uvar)
405 usages (Lam (UsageBinder u) body) uacc = usages body (u:uacc)
407 = case (tyvars other []) of { (tacc, vacc, expr) ->
408 (reverse uacc, tacc, vacc, expr) }
410 tyvars (Lam (TyBinder t) body) tacc = tyvars body (t:tacc)
412 = ASSERT(not (usage_lambda other))
413 case (valvars other []) of { (vacc, expr) ->
414 (reverse tacc, vacc, expr) }
416 valvars (Lam (ValBinder v) body) vacc = valvars body (v:vacc)
418 = ASSERT(not (usage_lambda other))
419 ASSERT(not (tyvar_lambda other))
420 (reverse vacc, other)
422 ---------------------------------------
423 usage_lambda (Lam (UsageBinder _) _) = True
424 usage_lambda _ = False
426 tyvar_lambda (Lam (TyBinder _) _) = True
427 tyvar_lambda _ = False
430 %************************************************************************
432 \subsection{Core arguments (atoms)}
434 %************************************************************************
437 data GenCoreArg val_occ tyvar uvar
440 | TyArg (GenType tyvar uvar)
441 | UsageArg (GenUsage uvar)
444 General and specific forms:
446 mkGenApp :: GenCoreExpr val_bdr val_occ tyvar uvar
447 -> [GenCoreArg val_occ tyvar uvar]
448 -> GenCoreExpr val_bdr val_occ tyvar uvar
449 mkTyApp :: GenCoreExpr val_bdr val_occ tyvar uvar
450 -> [GenType tyvar uvar]
451 -> GenCoreExpr val_bdr val_occ tyvar uvar
452 mkUseApp :: GenCoreExpr val_bdr val_occ tyvar uvar
454 -> GenCoreExpr val_bdr val_occ tyvar uvar
455 mkValApp :: GenCoreExpr val_bdr val_occ tyvar uvar
456 -> [GenCoreArg val_occ tyvar uvar] -- but we ASSERT they are LitArg or VarArg
457 -> GenCoreExpr val_bdr val_occ tyvar uvar
459 mkGenApp f args = foldl App f args
460 mkTyApp f args = foldl (\ e a -> App e (TyArg a)) f args
461 mkUseApp f args = foldl (\ e a -> App e (UsageArg a)) f args
462 mkValApp f args = foldl (\ e a -> App e (is_Lit_or_Var a)) f args
468 = if isValArg a then a else panic "CoreSyn.mkValApps:not LitArg or VarArg"
471 isValArg (LitArg _) = True -- often used for sanity-checking
472 isValArg (VarArg _) = True
475 notValArg = not . isValArg -- exists only because it's a common use of isValArg
477 numValArgs as = length [ a | a <- as, isValArg a ] -- again, convenience
481 mkApp fun = mk_thing (mkGenApp fun)
482 mkCon con = mk_thing (Con con)
483 mkPrim op = mk_thing (Prim op)
485 mk_thing thing uses tys vals
486 = thing (map UsageArg uses ++ map TyArg tys ++ map is_Lit_or_Var vals)
489 @collectArgs@ takes an application expression, returning the function
490 and the arguments to which it is applied.
493 collectArgs :: GenCoreExpr val_bdr val_occ tyvar uvar
494 -> (GenCoreExpr val_bdr val_occ tyvar uvar,
496 [GenType tyvar uvar],
497 [GenCoreArg val_occ tyvar uvar]{-ValArgs-})
502 valvars (App fun v) vacc | isValArg v = valvars fun (v:vacc)
504 = case (tyvars fun []) of { (expr, uacc, tacc) ->
505 (expr, uacc, tacc, vacc) }
507 tyvars (App fun (TyArg t)) tacc = tyvars fun (t:tacc)
509 = case (usages fun []) of { (expr, uacc) ->
512 usages (App fun (UsageArg u)) uacc = usages fun (u:uacc)
517 %************************************************************************
519 \subsection{The main @Core*@ instantiation of the @GenCore*@ types}
521 %************************************************************************
524 type CoreBinding = GenCoreBinding Id Id TyVar UVar
525 type CoreExpr = GenCoreExpr Id Id TyVar UVar
526 type CoreBinder = GenCoreBinder Id TyVar UVar
527 type CoreArg = GenCoreArg Id TyVar UVar
529 type CoreCaseAlts = GenCoreCaseAlts Id Id TyVar UVar
530 type CoreCaseDefault = GenCoreCaseDefault Id Id TyVar UVar
533 %************************************************************************
535 \subsection{The @TaggedCore*@ instantiation of the @GenCore*@ types}
537 %************************************************************************
539 Binders are ``tagged'' with a \tr{t}:
541 type Tagged t = (Id, t)
543 type TaggedCoreBinding t = GenCoreBinding (Tagged t) Id TyVar UVar
544 type TaggedCoreExpr t = GenCoreExpr (Tagged t) Id TyVar UVar
545 type TaggedCoreBinder t = GenCoreBinder (Tagged t) TyVar UVar
546 type TaggedCoreArg t = GenCoreArg Id TyVar UVar
548 type TaggedCoreCaseAlts t = GenCoreCaseAlts (Tagged t) Id TyVar UVar
549 type TaggedCoreCaseDefault t = GenCoreCaseDefault (Tagged t) Id TyVar UVar
552 %************************************************************************
554 \subsection{The @SimplifiableCore*@ instantiation of the @GenCore*@ types}
556 %************************************************************************
558 Binders are tagged with @BinderInfo@:
560 type Simplifiable = (Id, BinderInfo)
562 type SimplifiableCoreBinding = GenCoreBinding Simplifiable Id TyVar UVar
563 type SimplifiableCoreExpr = GenCoreExpr Simplifiable Id TyVar UVar
564 type SimplifiableCoreBinder = GenCoreBinder Simplifiable TyVar UVar
565 type SimplifiableCoreArg = GenCoreArg Id TyVar UVar
567 type SimplifiableCoreCaseAlts = GenCoreCaseAlts Simplifiable Id TyVar UVar
568 type SimplifiableCoreCaseDefault = GenCoreCaseDefault Simplifiable Id TyVar UVar