[project @ 1996-04-09 10:27:46 by partain]
[ghc-hetmet.git] / ghc / compiler / coreSyn / CoreSyn.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
3 %
4 \section[CoreSyn]{A data type for the Haskell compiler midsection}
5
6 \begin{code}
7 #include "HsVersions.h"
8
9 module CoreSyn (
10         GenCoreBinding(..), GenCoreExpr(..),
11         GenCoreArg(..), GenCoreBinder(..), GenCoreCaseAlts(..),
12         GenCoreCaseDefault(..),
13
14         bindersOf, pairsFromCoreBinds, rhssOfBind,
15
16         mkGenApp, mkValApp, mkTyApp, mkUseApp,
17         mkApp, mkCon, mkPrim,
18         mkValLam, mkTyLam, mkUseLam,
19         mkLam,
20         collectBinders, isValBinder, notValBinder,
21         
22         collectArgs, isValArg, notValArg, numValArgs,
23
24         mkCoLetAny, mkCoLetNoUnboxed, mkCoLetUnboxedToCase,
25         mkCoLetsAny, mkCoLetsNoUnboxed, mkCoLetsUnboxedToCase,
26         mkCoLetrecAny, mkCoLetrecNoUnboxed,
27
28         rhssOfAlts,
29
30         -- Common type instantiation...
31         CoreBinding(..),
32         CoreExpr(..),
33         CoreBinder(..),
34         CoreArg(..),
35         CoreCaseAlts(..),
36         CoreCaseDefault(..),
37
38         -- And not-so-common type instantiations...
39         TaggedCoreBinding(..),
40         TaggedCoreExpr(..),
41         TaggedCoreBinder(..),
42         TaggedCoreArg(..),
43         TaggedCoreCaseAlts(..),
44         TaggedCoreCaseDefault(..),
45
46         SimplifiableCoreBinding(..),
47         SimplifiableCoreExpr(..),
48         SimplifiableCoreBinder(..),
49         SimplifiableCoreArg(..),
50         SimplifiableCoreCaseAlts(..),
51         SimplifiableCoreCaseDefault(..)
52
53         -- and to make the interface self-sufficient ...
54
55     ) where
56
57 import Ubiq{-uitous-}
58
59 -- ToDo:rm:
60 --import PprCore                ( GenCoreExpr{-instance-} )
61 --import PprStyle               ( PprStyle(..) )
62
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-} )
68 \end{code}
69
70 %************************************************************************
71 %*                                                                      *
72 \subsection[CoreTopBinding_and_CoreBinding]{@CoreTopBinding@ and @GenCoreBinding@}
73 %*                                                                      *
74 %************************************************************************
75
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}.]
81
82 A @GenCoreBinding@ is either a single non-recursive binding of a
83 ``binder'' to an expression, or a mutually-recursive blob of same.
84 \begin{code}
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)]
88 \end{code}
89
90 \begin{code}
91 bindersOf :: GenCoreBinding val_bdr val_occ tyvar uvar -> [val_bdr]
92
93 pairsFromCoreBinds ::
94   [GenCoreBinding val_bdr val_occ tyvar uvar] ->
95   [(val_bdr, GenCoreExpr val_bdr val_occ tyvar uvar)]
96
97 rhssOfBind :: GenCoreBinding val_bdr val_occ tyvar uvar -> [GenCoreExpr val_bdr val_occ tyvar uvar]
98
99 bindersOf (NonRec binder _) = [binder]
100 bindersOf (Rec pairs)       = [binder | (binder, _) <- pairs]
101
102 pairsFromCoreBinds []                  = []
103 pairsFromCoreBinds ((NonRec b e) : bs) = (b,e) :  pairsFromCoreBinds bs
104 pairsFromCoreBinds ((Rec  pairs) : bs) = pairs ++ pairsFromCoreBinds bs
105
106 rhssOfBind (NonRec _ rhs) = [rhs]
107 rhssOfBind (Rec pairs)    = [rhs | (_,rhs) <- pairs]
108 \end{code}
109
110 %************************************************************************
111 %*                                                                      *
112 \subsection[GenCoreExpr]{Core expressions: @GenCoreExpr@}
113 %*                                                                      *
114 %************************************************************************
115
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@.
119 \begin{code}
120 data GenCoreExpr val_bdr val_occ tyvar uvar
121      = Var    val_occ
122      | Lit    Literal   -- literal constants
123 \end{code}
124
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.
129
130 \begin{code}
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.
138
139      | Prim     PrimOp [GenCoreArg val_occ tyvar uvar]
140                 -- saturated primitive operation;
141                 -- comment on Cons applies here, too.
142 \end{code}
143
144 Ye olde abstraction and application operators.
145 \begin{code}
146      | Lam      (GenCoreBinder val_bdr tyvar uvar)
147                 (GenCoreExpr   val_bdr val_occ tyvar uvar)
148
149      | App      (GenCoreExpr val_bdr val_occ tyvar uvar)
150                 (GenCoreArg  val_occ tyvar uvar)
151 \end{code}
152
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@.
157 \begin{code}
158      | Case     (GenCoreExpr val_bdr val_occ tyvar uvar)
159                 (GenCoreCaseAlts val_bdr val_occ tyvar uvar)
160 \end{code}
161
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
164 \tr{case}).
165
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
168 scoping.
169 \begin{code}
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
174 \end{code}
175
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.
180 \begin{code}
181      | SCC      CostCentre                                  -- label of scc
182                 (GenCoreExpr val_bdr val_occ tyvar uvar)    -- scc expression
183 \end{code}
184
185
186 %************************************************************************
187 %*                                                                      *
188 \subsection{Core-constructing functions with checking}
189 %*                                                                      *
190 %************************************************************************
191
192 When making @Lets@, we may want to take evasive action if the thing
193 being bound has unboxed type. We have different variants ...
194
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)
199
200 \begin{code}
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
207
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
211
212 mkCoLetrecAny []    body = body
213 mkCoLetrecAny binds body = Let (Rec binds) body
214
215 mkCoLetsAny []    expr = expr
216 mkCoLetsAny binds expr = foldr mkCoLetAny expr binds
217
218 mkCoLetAny bind@(Rec binds) body = mkCoLetrecAny binds body
219 mkCoLetAny bind@(NonRec binder rhs) body
220   = case body of
221       Var binder2 | binder == binder2
222          -> rhs   -- hey, I have the rhs
223       other
224          -> Let bind body
225 \end{code}
226
227 \begin{code}
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
232
233 mkCoLetNoUnboxed bind@(Rec binds) body
234   = mkCoLetrecNoUnboxed binds body
235 mkCoLetNoUnboxed bind@(NonRec binder rhs) body
236   = --ASSERT (not (isUnboxedType (idType binder)))
237     case body of
238       Var binder2 | binder == binder2
239          -> rhs   -- hey, I have the rhs
240       other
241          -> Let bind body
242
243 mkCoLetsNoUnboxed []    expr = expr
244 mkCoLetsNoUnboxed binds expr = foldr mkCoLetNoUnboxed expr binds
245
246 --mkCoLetrecNoUnboxed :: [(Id, CoreExpr)]       -- bindings
247 --                  -> CoreExpr         -- body
248 --                  -> CoreExpr                 -- result
249
250 mkCoLetrecNoUnboxed []    body = body
251 mkCoLetrecNoUnboxed binds body
252   = ASSERT (all is_boxed_bind binds)
253     Let (Rec binds) body
254   where
255     is_boxed_bind (binder, rhs)
256       = (not . isUnboxedType . idType) binder
257 \end{code}
258
259 \begin{code}
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
264
265 mkCoLetUnboxedToCase bind@(Rec binds) body
266   = mkCoLetrecNoUnboxed binds body
267 mkCoLetUnboxedToCase bind@(NonRec binder rhs) body
268   = case body of
269       Var binder2 | binder == binder2
270          -> rhs   -- hey, I have the rhs
271       other
272          -> if (not (isUnboxedType (idType binder))) then
273                 Let bind body            -- boxed...
274             else
275                 Case rhs                  -- unboxed...
276                   (PrimAlts []
277                     (BindDefault binder body))
278
279 mkCoLetsUnboxedToCase []    expr = expr
280 mkCoLetsUnboxedToCase binds expr = foldr mkCoLetUnboxedToCase expr binds
281 \end{code}
282
283 %************************************************************************
284 %*                                                                      *
285 \subsection{Case alternatives in @GenCoreExpr@}
286 %*                                                                      *
287 %************************************************************************
288
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).
295
296 For example:
297 \begin{verbatim}
298 let# x=e in b
299 \end{verbatim}
300 becomes
301 \begin{verbatim}
302 Case e [ BindDefaultAlt x -> b ]
303 \end{verbatim}
304
305 \begin{code}
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)
311
312   | PrimAlts    [(Literal,                      -- alts: unboxed literal,
313                   GenCoreExpr val_bdr val_occ tyvar uvar)]      -- rhs.
314                 (GenCoreCaseDefault val_bdr val_occ tyvar uvar)
315
316 -- obvious things: if there are no alts in the list, then the default
317 -- can't be NoDefault.
318
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
324                                                 -- be used in RHS.
325 \end{code}
326
327 \begin{code}
328 rhssOfAlts (AlgAlts alts deflt)  = rhssOfDeflt deflt ++ [rhs | (_,_,rhs) <- alts]
329 rhssOfAlts (PrimAlts alts deflt) = rhssOfDeflt deflt ++ [rhs | (_,rhs)   <- alts]
330
331 rhssOfDeflt NoDefault           = []
332 rhssOfDeflt (BindDefault _ rhs) = [rhs]
333 \end{code}
334
335 %************************************************************************
336 %*                                                                      *
337 \subsection{Core binders}
338 %*                                                                      *
339 %************************************************************************
340
341 \begin{code}
342 data GenCoreBinder val_bdr tyvar uvar
343   = ValBinder   val_bdr
344   | TyBinder    tyvar
345   | UsageBinder uvar
346
347 isValBinder (ValBinder _) = True
348 isValBinder _             = False
349
350 notValBinder = not . isValBinder
351 \end{code}
352
353 Clump Lams together if possible.
354
355 \begin{code}
356 mkValLam :: [val_bdr]
357          -> GenCoreExpr val_bdr val_occ tyvar uvar
358          -> GenCoreExpr val_bdr val_occ tyvar uvar
359 mkTyLam  :: [tyvar]
360          -> GenCoreExpr val_bdr val_occ tyvar uvar
361          -> GenCoreExpr val_bdr val_occ tyvar uvar
362 mkUseLam :: [uvar]
363          -> GenCoreExpr val_bdr val_occ tyvar uvar
364          -> GenCoreExpr val_bdr val_occ tyvar uvar
365
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
369
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
373
374 mkLam tyvars valvars body
375   = mkTyLam tyvars (mkValLam valvars body)
376 \end{code}
377
378 We often want to strip off leading lambdas before getting down to
379 business.  @collectBinders@ is your friend.
380
381 We expect (by convention) usage-, type-, and value- lambdas in that
382 order.
383
384 \begin{code}
385 collectBinders ::
386   GenCoreExpr val_bdr val_occ tyvar uvar ->
387   ([uvar], [tyvar], [val_bdr], GenCoreExpr val_bdr val_occ tyvar uvar)
388
389 collectBinders expr
390   = usages expr []
391   where
392     usages (Lam (UsageBinder u) body) uacc = usages body (u:uacc)
393     usages other uacc
394       = case (tyvars other []) of { (tacc, vacc, expr) ->
395         (reverse uacc, tacc, vacc, expr) }
396
397     tyvars (Lam (TyBinder t)    body) tacc = tyvars body (t:tacc)
398     tyvars other tacc
399       = ASSERT(not (usage_lambda other))
400         case (valvars other []) of { (vacc, expr) ->
401         (reverse tacc, vacc, expr) }
402
403     valvars (Lam (ValBinder v)  body) vacc = valvars body (v:vacc)
404     valvars other vacc
405       = ASSERT(not (usage_lambda other))
406         ASSERT(not (tyvar_lambda other))
407         (reverse vacc, other)
408
409     ---------------------------------------
410     usage_lambda (Lam (UsageBinder _) _) = True
411     usage_lambda _                       = False
412
413     tyvar_lambda (Lam (TyBinder _) _)    = True
414     tyvar_lambda _                       = False
415 \end{code}
416
417 %************************************************************************
418 %*                                                                      *
419 \subsection{Core arguments (atoms)}
420 %*                                                                      *
421 %************************************************************************
422
423 \begin{code}
424 data GenCoreArg val_occ tyvar uvar
425   = LitArg      Literal
426   | VarArg      val_occ
427   | TyArg       (GenType tyvar uvar)
428   | UsageArg    (GenUsage uvar)
429 \end{code}
430
431 General and specific forms:
432 \begin{code}
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
440          -> [GenUsage 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
445
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
450
451 #ifndef DEBUG
452 is_Lit_or_Var a = a
453 #else
454 is_Lit_or_Var a
455   = if isValArg a then a else panic "CoreSyn.mkValApps:not LitArg or VarArg"
456 #endif
457
458 isValArg (LitArg _) = True  -- often used for sanity-checking
459 isValArg (VarArg _) = True
460 isValArg _          = False
461
462 notValArg = not . isValArg -- exists only because it's a common use of isValArg
463
464 numValArgs as = length [ a | a <- as, isValArg a ] -- again, convenience
465 \end{code}
466
467 \begin{code}
468 mkApp  fun = mk_thing (mkGenApp fun)
469 mkCon  con = mk_thing (Con      con)
470 mkPrim op  = mk_thing (Prim     op)
471
472 mk_thing thing uses tys vals
473   = thing (map UsageArg uses ++ map TyArg tys ++ map is_Lit_or_Var vals)
474 \end{code}
475
476 @collectArgs@ takes an application expression, returning the function
477 and the arguments to which it is applied.
478
479 \begin{code}
480 collectArgs :: GenCoreExpr val_bdr val_occ tyvar uvar
481             -> (GenCoreExpr val_bdr val_occ tyvar uvar,
482                 [GenUsage uvar],
483                 [GenType tyvar uvar],
484                 [GenCoreArg val_occ tyvar uvar]{-ValArgs-})
485
486 collectArgs expr
487   = usages expr []
488   where
489     usages (App fun (UsageArg u)) uacc = usages fun (u:uacc)
490     usages fun uacc
491       = case (tyvars fun []) of { (expr, tacc, vacc) ->
492         (expr, uacc, tacc, vacc) }
493
494     tyvars (App fun (TyArg t))    tacc = tyvars fun (t:tacc)
495     tyvars fun tacc
496       = ASSERT(not (usage_app fun))
497         case (valvars fun []) of { (expr, vacc) ->
498         (expr, tacc, vacc) }
499
500     valvars (App fun v) vacc | isValArg v = valvars fun (v:vacc)
501     valvars fun 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) $
505         (fun, vacc)
506
507     ---------------------------------------
508     usage_app (App _ (UsageArg _)) = True
509     usage_app _                    = False
510
511     ty_app    (App _ (TyArg _))    = True
512     ty_app    _                    = False
513 \end{code}
514
515 %************************************************************************
516 %*                                                                      *
517 \subsection{The main @Core*@ instantiation of the @GenCore*@ types}
518 %*                                                                      *
519 %************************************************************************
520
521 \begin{code}
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
526
527 type CoreCaseAlts    = GenCoreCaseAlts    Id Id TyVar UVar
528 type CoreCaseDefault = GenCoreCaseDefault Id Id TyVar UVar
529 \end{code}
530
531 %************************************************************************
532 %*                                                                      *
533 \subsection{The @TaggedCore*@ instantiation of the @GenCore*@ types}
534 %*                                                                      *
535 %************************************************************************
536
537 Binders are ``tagged'' with a \tr{t}:
538 \begin{code}
539 type Tagged t = (Id, t)
540
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
545
546 type TaggedCoreCaseAlts    t = GenCoreCaseAlts    (Tagged t) Id TyVar UVar
547 type TaggedCoreCaseDefault t = GenCoreCaseDefault (Tagged t) Id TyVar UVar
548 \end{code}
549
550 %************************************************************************
551 %*                                                                      *
552 \subsection{The @SimplifiableCore*@ instantiation of the @GenCore*@ types}
553 %*                                                                      *
554 %************************************************************************
555
556 Binders are tagged with @BinderInfo@:
557 \begin{code}
558 type Simplifiable = (Id, BinderInfo)
559
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
564
565 type SimplifiableCoreCaseAlts    = GenCoreCaseAlts    Simplifiable Id TyVar UVar
566 type SimplifiableCoreCaseDefault = GenCoreCaseDefault Simplifiable Id TyVar UVar
567 \end{code}