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(..),
14 bindersOf, pairsFromCoreBinds, rhssOfBind,
16 mkGenApp, mkValApp, mkTyApp, mkUseApp,
18 mkValLam, mkTyLam, mkUseLam,
20 collectBinders, isValBinder, notValBinder,
22 collectArgs, isValArg, notValArg, numValArgs,
24 mkCoLetAny, mkCoLetNoUnboxed, mkCoLetUnboxedToCase,
25 mkCoLetsAny, mkCoLetsNoUnboxed, mkCoLetsUnboxedToCase,
26 mkCoLetrecAny, mkCoLetrecNoUnboxed,
30 -- Common type instantiation...
38 -- And not-so-common type instantiations...
39 TaggedCoreBinding(..),
43 TaggedCoreCaseAlts(..),
44 TaggedCoreCaseDefault(..),
46 SimplifiableCoreBinding(..),
47 SimplifiableCoreExpr(..),
48 SimplifiableCoreBinder(..),
49 SimplifiableCoreArg(..),
50 SimplifiableCoreCaseAlts(..),
51 SimplifiableCoreCaseDefault(..)
53 -- and to make the interface self-sufficient ...
60 --import PprCore ( GenCoreExpr{-instance-} )
61 --import PprStyle ( PprStyle(..) )
63 import CostCentre ( showCostCentre, CostCentre )
64 import Id ( idType, GenId{-instance Eq-} )
65 import Type ( isUnboxedType )
66 import Usage ( UVar(..) )
67 import Util ( panic, assertPanic {-pprTrace:ToDo:rm-} )
70 %************************************************************************
72 \subsection[CoreTopBinding_and_CoreBinding]{@CoreTopBinding@ and @GenCoreBinding@}
74 %************************************************************************
76 Core programs, bindings, expressions, etc., are parameterised with
77 respect to the information kept about binding and bound occurrences of
78 variables, called {\em binders} and {\em val_occ tyvar uvars}, respectively. [I
79 don't really like the pair of names; I prefer {\em binder} and {\em
80 bounder}. Or {\em binder} and {\em var}.]
82 A @GenCoreBinding@ is either a single non-recursive binding of a
83 ``binder'' to an expression, or a mutually-recursive blob of same.
85 data GenCoreBinding val_bdr val_occ tyvar uvar
86 = NonRec val_bdr (GenCoreExpr val_bdr val_occ tyvar uvar)
87 | Rec [(val_bdr, GenCoreExpr val_bdr val_occ tyvar uvar)]
91 bindersOf :: GenCoreBinding val_bdr val_occ tyvar uvar -> [val_bdr]
94 [GenCoreBinding val_bdr val_occ tyvar uvar] ->
95 [(val_bdr, GenCoreExpr val_bdr val_occ tyvar uvar)]
97 rhssOfBind :: GenCoreBinding val_bdr val_occ tyvar uvar -> [GenCoreExpr val_bdr val_occ tyvar uvar]
99 bindersOf (NonRec binder _) = [binder]
100 bindersOf (Rec pairs) = [binder | (binder, _) <- pairs]
102 pairsFromCoreBinds [] = []
103 pairsFromCoreBinds ((NonRec b e) : bs) = (b,e) : pairsFromCoreBinds bs
104 pairsFromCoreBinds ((Rec pairs) : bs) = pairs ++ pairsFromCoreBinds bs
106 rhssOfBind (NonRec _ rhs) = [rhs]
107 rhssOfBind (Rec pairs) = [rhs | (_,rhs) <- pairs]
110 %************************************************************************
112 \subsection[GenCoreExpr]{Core expressions: @GenCoreExpr@}
114 %************************************************************************
116 @GenCoreExpr@ is the heart of the ``core'' data types; it is
117 (more-or-less) boiled-down second-order polymorphic lambda calculus.
118 For types in the core world, we just keep using @Types@.
120 data GenCoreExpr val_bdr val_occ tyvar uvar
122 | Lit Literal -- literal constants
125 @Cons@ and @Prims@ are saturated constructor and primitive-op
126 applications (see the comment). Note: @Con@s are only set up by the
127 simplifier (and by the desugarer when it knows what it's doing). The
128 desugarer sets up constructors as applications of global @Vars@s.
131 | Con Id [GenCoreArg val_occ tyvar uvar]
132 -- Saturated constructor application:
133 -- The constructor is a function of the form:
134 -- /\ a1 -> ... /\ am -> \ b1 -> ... \ bn ->
135 -- <expr> where "/\" is a type lambda and "\" the
136 -- regular kind; there will be "m" Types and
137 -- "n" bindees in the Con args.
139 | Prim PrimOp [GenCoreArg val_occ tyvar uvar]
140 -- saturated primitive operation;
141 -- comment on Cons applies here, too.
144 Ye olde abstraction and application operators.
146 | Lam (GenCoreBinder val_bdr tyvar uvar)
147 (GenCoreExpr val_bdr val_occ tyvar uvar)
149 | App (GenCoreExpr val_bdr val_occ tyvar uvar)
150 (GenCoreArg val_occ tyvar uvar)
153 Case expressions (\tr{case <expr> of <List of alternatives>}): there
154 are really two flavours masquerading here---those for scrutinising
155 {\em algebraic} types and those for {\em primitive} types. Please see
156 under @GenCoreCaseAlts@.
158 | Case (GenCoreExpr val_bdr val_occ tyvar uvar)
159 (GenCoreCaseAlts val_bdr val_occ tyvar uvar)
162 A Core case expression \tr{case e of v -> ...} implies evaluation of
163 \tr{e}; it is not equivalent to \tr{let v = in ...} (as with a Haskell
166 Non-recursive @Lets@ only have one binding; having more than one
167 doesn't buy you much, and it is an easy way to mess up variable
170 | Let (GenCoreBinding val_bdr val_occ tyvar uvar)
171 (GenCoreExpr val_bdr val_occ tyvar uvar)
172 -- both recursive and non-.
173 -- The "GenCoreBinding" records that information
176 For cost centre scc expressions we introduce a new core construct
177 @SCC@ so transforming passes have to deal with it explicitly. The
178 alternative of using a new PrimativeOp may result in a bad
179 transformations of which we are unaware.
181 | SCC CostCentre -- label of scc
182 (GenCoreExpr val_bdr val_occ tyvar uvar) -- scc expression
186 %************************************************************************
188 \subsection{Core-constructing functions with checking}
190 %************************************************************************
192 When making @Lets@, we may want to take evasive action if the thing
193 being bound has unboxed type. We have different variants ...
195 @mkCoLet(s|rec)Any@ let-binds any binding, regardless of type
196 @mkCoLet(s|rec)NoUnboxed@ prohibits unboxed bindings
197 @mkCoLet(s)UnboxedToCase@ converts an unboxed binding to a case
198 (unboxed bindings in a letrec are still prohibited)
201 mkCoLetAny :: GenCoreBinding Id Id tyvar uvar
202 -> GenCoreExpr Id Id tyvar uvar
203 -> GenCoreExpr Id Id tyvar uvar
204 mkCoLetsAny :: [GenCoreBinding Id Id tyvar uvar] ->
205 GenCoreExpr Id Id tyvar uvar ->
206 GenCoreExpr Id Id tyvar uvar
208 mkCoLetrecAny :: [(val_bdr, GenCoreExpr val_bdr val_occ tyvar uvar)]
209 -> GenCoreExpr val_bdr val_occ tyvar uvar
210 -> GenCoreExpr val_bdr val_occ tyvar uvar
212 mkCoLetrecAny [] body = body
213 mkCoLetrecAny binds body = Let (Rec binds) body
215 mkCoLetsAny [] expr = expr
216 mkCoLetsAny binds expr = foldr mkCoLetAny expr binds
218 mkCoLetAny bind@(Rec binds) body = mkCoLetrecAny binds body
219 mkCoLetAny bind@(NonRec binder rhs) body
221 Var binder2 | binder == binder2
222 -> rhs -- hey, I have the rhs
228 --mkCoLetNoUnboxed ::
229 -- GenCoreBinding val_bdr val_occ tyvar uvar ->
230 -- GenCoreExpr val_bdr val_occ tyvar uvar ->
231 -- GenCoreExpr val_bdr val_occ tyvar uvar
233 mkCoLetNoUnboxed bind@(Rec binds) body
234 = mkCoLetrecNoUnboxed binds body
235 mkCoLetNoUnboxed bind@(NonRec binder rhs) body
236 = --ASSERT (not (isUnboxedType (idType binder)))
238 Var binder2 | binder == binder2
239 -> rhs -- hey, I have the rhs
243 mkCoLetsNoUnboxed [] expr = expr
244 mkCoLetsNoUnboxed binds expr = foldr mkCoLetNoUnboxed expr binds
246 --mkCoLetrecNoUnboxed :: [(Id, CoreExpr)] -- bindings
247 -- -> CoreExpr -- body
248 -- -> CoreExpr -- result
250 mkCoLetrecNoUnboxed [] body = body
251 mkCoLetrecNoUnboxed binds body
252 = ASSERT (all is_boxed_bind binds)
255 is_boxed_bind (binder, rhs)
256 = (not . isUnboxedType . idType) binder
260 --mkCoLetUnboxedToCase ::
261 -- GenCoreBinding val_bdr val_occ tyvar uvar ->
262 -- GenCoreExpr val_bdr val_occ tyvar uvar ->
263 -- GenCoreExpr val_bdr val_occ tyvar uvar
265 mkCoLetUnboxedToCase bind@(Rec binds) body
266 = mkCoLetrecNoUnboxed binds body
267 mkCoLetUnboxedToCase bind@(NonRec binder rhs) body
269 Var binder2 | binder == binder2
270 -> rhs -- hey, I have the rhs
272 -> if (not (isUnboxedType (idType binder))) then
273 Let bind body -- boxed...
275 Case rhs -- unboxed...
277 (BindDefault binder body))
279 mkCoLetsUnboxedToCase [] expr = expr
280 mkCoLetsUnboxedToCase binds expr = foldr mkCoLetUnboxedToCase expr binds
283 %************************************************************************
285 \subsection{Case alternatives in @GenCoreExpr@}
287 %************************************************************************
289 We have different kinds of @case@s, the differences being reflected in
290 the kinds of alternatives a case has. We maintain a distinction
291 between cases for scrutinising algebraic datatypes, as opposed to
292 primitive types. In both cases, we carry around a @TyCon@, as a
293 handle with which we can get info about the case (e.g., total number
294 of data constructors for this type).
302 Case e [ BindDefaultAlt x -> b ]
306 data GenCoreCaseAlts val_bdr val_occ tyvar uvar
307 = AlgAlts [(Id, -- alts: data constructor,
308 [val_bdr], -- constructor's parameters,
309 GenCoreExpr val_bdr val_occ tyvar uvar)] -- rhs.
310 (GenCoreCaseDefault val_bdr val_occ tyvar uvar)
312 | PrimAlts [(Literal, -- alts: unboxed literal,
313 GenCoreExpr val_bdr val_occ tyvar uvar)] -- rhs.
314 (GenCoreCaseDefault val_bdr val_occ tyvar uvar)
316 -- obvious things: if there are no alts in the list, then the default
317 -- can't be NoDefault.
319 data GenCoreCaseDefault val_bdr val_occ tyvar uvar
320 = NoDefault -- small con family: all
321 -- constructor accounted for
322 | BindDefault val_bdr -- form: var -> expr;
323 (GenCoreExpr val_bdr val_occ tyvar uvar) -- "val_bdr" may or may not
328 rhssOfAlts (AlgAlts alts deflt) = rhssOfDeflt deflt ++ [rhs | (_,_,rhs) <- alts]
329 rhssOfAlts (PrimAlts alts deflt) = rhssOfDeflt deflt ++ [rhs | (_,rhs) <- alts]
331 rhssOfDeflt NoDefault = []
332 rhssOfDeflt (BindDefault _ rhs) = [rhs]
335 %************************************************************************
337 \subsection{Core binders}
339 %************************************************************************
342 data GenCoreBinder val_bdr tyvar uvar
347 isValBinder (ValBinder _) = True
348 isValBinder _ = False
350 notValBinder = not . isValBinder
353 Clump Lams together if possible.
356 mkValLam :: [val_bdr]
357 -> GenCoreExpr val_bdr val_occ tyvar uvar
358 -> GenCoreExpr val_bdr val_occ tyvar uvar
360 -> GenCoreExpr val_bdr val_occ tyvar uvar
361 -> GenCoreExpr val_bdr val_occ tyvar uvar
363 -> GenCoreExpr val_bdr val_occ tyvar uvar
364 -> GenCoreExpr val_bdr val_occ tyvar uvar
366 mkValLam binders body = foldr (Lam . ValBinder) body binders
367 mkTyLam binders body = foldr (Lam . TyBinder) body binders
368 mkUseLam binders body = foldr (Lam . UsageBinder) body binders
370 mkLam :: [tyvar] -> [val_bdr] -- ToDo: could add a [uvar] arg...
371 -> GenCoreExpr val_bdr val_occ tyvar uvar
372 -> GenCoreExpr val_bdr val_occ tyvar uvar
374 mkLam tyvars valvars body
375 = mkTyLam tyvars (mkValLam valvars body)
378 We often want to strip off leading lambdas before getting down to
379 business. @collectBinders@ is your friend.
381 We expect (by convention) usage-, type-, and value- lambdas in that
386 GenCoreExpr val_bdr val_occ tyvar uvar ->
387 ([uvar], [tyvar], [val_bdr], GenCoreExpr val_bdr val_occ tyvar uvar)
392 usages (Lam (UsageBinder u) body) uacc = usages body (u:uacc)
394 = case (tyvars other []) of { (tacc, vacc, expr) ->
395 (reverse uacc, tacc, vacc, expr) }
397 tyvars (Lam (TyBinder t) body) tacc = tyvars body (t:tacc)
399 = ASSERT(not (usage_lambda other))
400 case (valvars other []) of { (vacc, expr) ->
401 (reverse tacc, vacc, expr) }
403 valvars (Lam (ValBinder v) body) vacc = valvars body (v:vacc)
405 = ASSERT(not (usage_lambda other))
406 ASSERT(not (tyvar_lambda other))
407 (reverse vacc, other)
409 ---------------------------------------
410 usage_lambda (Lam (UsageBinder _) _) = True
411 usage_lambda _ = False
413 tyvar_lambda (Lam (TyBinder _) _) = True
414 tyvar_lambda _ = False
417 %************************************************************************
419 \subsection{Core arguments (atoms)}
421 %************************************************************************
424 data GenCoreArg val_occ tyvar uvar
427 | TyArg (GenType tyvar uvar)
428 | UsageArg (GenUsage uvar)
431 General and specific forms:
433 mkGenApp :: GenCoreExpr val_bdr val_occ tyvar uvar
434 -> [GenCoreArg val_occ tyvar uvar]
435 -> GenCoreExpr val_bdr val_occ tyvar uvar
436 mkTyApp :: GenCoreExpr val_bdr val_occ tyvar uvar
437 -> [GenType tyvar uvar]
438 -> GenCoreExpr val_bdr val_occ tyvar uvar
439 mkUseApp :: GenCoreExpr val_bdr val_occ tyvar uvar
441 -> GenCoreExpr val_bdr val_occ tyvar uvar
442 mkValApp :: GenCoreExpr val_bdr val_occ tyvar uvar
443 -> [GenCoreArg val_occ tyvar uvar] -- but we ASSERT they are LitArg or VarArg
444 -> GenCoreExpr val_bdr val_occ tyvar uvar
446 mkGenApp f args = foldl App f args
447 mkTyApp f args = foldl (\ e a -> App e (TyArg a)) f args
448 mkUseApp f args = foldl (\ e a -> App e (UsageArg a)) f args
449 mkValApp f args = foldl (\ e a -> App e (is_Lit_or_Var a)) f args
455 = if isValArg a then a else panic "CoreSyn.mkValApps:not LitArg or VarArg"
458 isValArg (LitArg _) = True -- often used for sanity-checking
459 isValArg (VarArg _) = True
462 notValArg = not . isValArg -- exists only because it's a common use of isValArg
464 numValArgs as = length [ a | a <- as, isValArg a ] -- again, convenience
468 mkApp fun = mk_thing (mkGenApp fun)
469 mkCon con = mk_thing (Con con)
470 mkPrim op = mk_thing (Prim op)
472 mk_thing thing uses tys vals
473 = thing (map UsageArg uses ++ map TyArg tys ++ map is_Lit_or_Var vals)
476 @collectArgs@ takes an application expression, returning the function
477 and the arguments to which it is applied.
480 collectArgs :: GenCoreExpr val_bdr val_occ tyvar uvar
481 -> (GenCoreExpr val_bdr val_occ tyvar uvar,
483 [GenType tyvar uvar],
484 [GenCoreArg val_occ tyvar uvar]{-ValArgs-})
489 usages (App fun (UsageArg u)) uacc = usages fun (u:uacc)
491 = case (tyvars fun []) of { (expr, tacc, vacc) ->
492 (expr, uacc, tacc, vacc) }
494 tyvars (App fun (TyArg t)) tacc = tyvars fun (t:tacc)
496 = ASSERT(not (usage_app fun))
497 case (valvars fun []) of { (expr, vacc) ->
500 valvars (App fun v) vacc | isValArg v = valvars fun (v:vacc)
502 = --ASSERT(not (usage_app fun))
503 --ASSERT(not (ty_app fun))
504 (if (usage_app fun || ty_app fun) then trace "CoreSyn:valvars" {-(ppr PprDebug fun)-} else id) $
507 ---------------------------------------
508 usage_app (App _ (UsageArg _)) = True
511 ty_app (App _ (TyArg _)) = True
515 %************************************************************************
517 \subsection{The main @Core*@ instantiation of the @GenCore*@ types}
519 %************************************************************************
522 type CoreBinding = GenCoreBinding Id Id TyVar UVar
523 type CoreExpr = GenCoreExpr Id Id TyVar UVar
524 type CoreBinder = GenCoreBinder Id TyVar UVar
525 type CoreArg = GenCoreArg Id TyVar UVar
527 type CoreCaseAlts = GenCoreCaseAlts Id Id TyVar UVar
528 type CoreCaseDefault = GenCoreCaseDefault Id Id TyVar UVar
531 %************************************************************************
533 \subsection{The @TaggedCore*@ instantiation of the @GenCore*@ types}
535 %************************************************************************
537 Binders are ``tagged'' with a \tr{t}:
539 type Tagged t = (Id, t)
541 type TaggedCoreBinding t = GenCoreBinding (Tagged t) Id TyVar UVar
542 type TaggedCoreExpr t = GenCoreExpr (Tagged t) Id TyVar UVar
543 type TaggedCoreBinder t = GenCoreBinder (Tagged t) TyVar UVar
544 type TaggedCoreArg t = GenCoreArg Id TyVar UVar
546 type TaggedCoreCaseAlts t = GenCoreCaseAlts (Tagged t) Id TyVar UVar
547 type TaggedCoreCaseDefault t = GenCoreCaseDefault (Tagged t) Id TyVar UVar
550 %************************************************************************
552 \subsection{The @SimplifiableCore*@ instantiation of the @GenCore*@ types}
554 %************************************************************************
556 Binders are tagged with @BinderInfo@:
558 type Simplifiable = (Id, BinderInfo)
560 type SimplifiableCoreBinding = GenCoreBinding Simplifiable Id TyVar UVar
561 type SimplifiableCoreExpr = GenCoreExpr Simplifiable Id TyVar UVar
562 type SimplifiableCoreBinder = GenCoreBinder Simplifiable TyVar UVar
563 type SimplifiableCoreArg = GenCoreArg Id TyVar UVar
565 type SimplifiableCoreCaseAlts = GenCoreCaseAlts Simplifiable Id TyVar UVar
566 type SimplifiableCoreCaseDefault = GenCoreCaseDefault Simplifiable Id TyVar UVar