[project @ 1996-06-05 06:44:31 by partain]
[ghc-hetmet.git] / ghc / compiler / coreSyn / CoreUnfold.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1994-1996
3 %
4 \section[CoreUnfold]{Core-syntax unfoldings}
5
6 Unfoldings (which can travel across module boundaries) are in Core
7 syntax (namely @CoreExpr@s).
8
9 The type @UnfoldingDetails@ sits ``above'' simply-Core-expressions
10 unfoldings, capturing ``higher-level'' things we know about a binding,
11 usually things that the simplifier found out (e.g., ``it's a
12 literal'').  In the corner of a @GenForm@ unfolding, you will
13 find, unsurprisingly, a Core expression.
14
15 \begin{code}
16 #include "HsVersions.h"
17
18 module CoreUnfold (
19         UnfoldingDetails(..), UnfoldingGuidance(..), -- types
20         FormSummary(..),
21
22         mkFormSummary,
23         mkGenForm, mkLitForm, mkConForm,
24         whnfDetails,
25         mkMagicUnfolding,
26         calcUnfoldingGuidance,
27         mentionedInUnfolding
28     ) where
29
30 IMP_Ubiq()
31 IMPORT_DELOOPER(IdLoop)  -- for paranoia checking;
32                  -- and also to get mkMagicUnfoldingFun
33 IMPORT_DELOOPER(PrelLoop)  -- for paranoia checking
34
35 import Bag              ( emptyBag, unitBag, unionBags, Bag )
36 import BinderInfo       ( oneTextualOcc, oneSafeOcc )
37 import CgCompInfo       ( uNFOLDING_CHEAP_OP_COST,
38                           uNFOLDING_DEAR_OP_COST,
39                           uNFOLDING_NOREP_LIT_COST
40                         )
41 import CoreSyn
42 import CoreUtils        ( coreExprType, manifestlyWHNF )
43 import CostCentre       ( ccMentionsId )
44 import Id               ( IdSet(..), GenId{-instances-} )
45 import IdInfo           ( bottomIsGuaranteed )
46 import Literal          ( isNoRepLit, isLitLitLit )
47 import Pretty
48 import PrimOp           ( primOpCanTriggerGC, PrimOp(..) )
49 import TyCon            ( tyConFamilySize )
50 import Type             ( getAppDataTyConExpandingDicts )
51 import UniqSet          ( emptyUniqSet, unitUniqSet, mkUniqSet,
52                           addOneToUniqSet, unionUniqSets
53                         )
54 import Usage            ( UVar(..) )
55 import Util             ( isIn, panic )
56
57 whatsMentionedInId = panic "whatsMentionedInId (CoreUnfold)"
58 getMentionedTyConsAndClassesFromType = panic "getMentionedTyConsAndClassesFromType (CoreUnfold)"
59 \end{code}
60
61 %************************************************************************
62 %*                                                                      *
63 \subsection{@UnfoldingDetails@ and @UnfoldingGuidance@ types}
64 %*                                                                      *
65 %************************************************************************
66
67 (And @FormSummary@, too.)
68
69 \begin{code}
70 data UnfoldingDetails
71   = NoUnfoldingDetails
72
73   | OtherLitForm
74         [Literal]               -- It is a literal, but definitely not one of these
75
76   | OtherConForm
77         [Id]                    -- It definitely isn't one of these constructors
78                                 -- This captures the situation in the default branch of
79                                 -- a case:  case x of
80                                 --              c1 ... -> ...
81                                 --              c2 ... -> ...
82                                 --              v -> default-rhs
83                                 -- Then in default-rhs we know that v isn't c1 or c2.
84                                 --
85                                 -- NB.  In the degenerate: case x of {v -> default-rhs}
86                                 -- x will be bound to
87                                 --      OtherConForm []
88                                 -- which captures the idea that x is eval'd but we don't
89                                 -- know which constructor.
90
91
92   | GenForm
93         FormSummary             -- Tells whether the template is a WHNF or bottom
94         TemplateOutExpr         -- The template
95         UnfoldingGuidance       -- Tells about the *size* of the template.
96
97   | MagicForm
98         Unique                  -- of the Id whose magic unfolding this is
99         MagicUnfoldingFun
100
101 type TemplateOutExpr = GenCoreExpr (Id, BinderInfo) Id TyVar UVar
102         -- An OutExpr with occurrence info attached.  This is used as
103         -- a template in GeneralForms.
104
105 mkMagicUnfolding :: Unique -> UnfoldingDetails
106 mkMagicUnfolding tag  = MagicForm tag (mkMagicUnfoldingFun tag)
107
108 data FormSummary
109   = WhnfForm            -- Expression is WHNF
110   | BottomForm          -- Expression is guaranteed to be bottom. We're more gung
111                         -- ho about inlining such things, because it can't waste work
112   | OtherForm           -- Anything else
113
114 instance Outputable FormSummary where
115    ppr sty WhnfForm   = ppStr "WHNF"
116    ppr sty BottomForm = ppStr "Bot"
117    ppr sty OtherForm  = ppStr "Other"
118
119 --???mkFormSummary :: StrictnessInfo -> GenCoreExpr bndr Id -> FormSummary
120 mkFormSummary si expr
121   | manifestlyWHNF     expr = WhnfForm
122   | bottomIsGuaranteed si   = BottomForm
123
124   -- Chances are that the Id will be decorated with strictness info
125   -- telling that the RHS is definitely bottom.  This *might* not be the
126   -- case, if it's been a while since strictness analysis, but leaving out
127   -- the test for manifestlyBottom makes things a little more efficient.
128   -- We can always put it back...
129   -- | manifestlyBottom expr  = BottomForm
130
131   | otherwise = OtherForm
132
133 whnfDetails :: UnfoldingDetails -> Bool         -- True => thing is evaluated
134 whnfDetails (GenForm WhnfForm _ _) = True
135 whnfDetails (OtherLitForm _)       = True
136 whnfDetails (OtherConForm _)       = True
137 whnfDetails other                  = False
138 \end{code}
139
140 \begin{code}
141 data UnfoldingGuidance
142   = UnfoldNever                 -- Don't do it!
143
144   | UnfoldAlways                -- There is no "original" definition,
145                                 -- so you'd better unfold.  Or: something
146                                 -- so cheap to unfold (e.g., 1#) that
147                                 -- you should do it absolutely always.
148
149   | EssentialUnfolding          -- Like UnfoldAlways, but you *must* do
150                                 -- it absolutely always.
151                                 -- This is what we use for data constructors
152                                 -- and PrimOps, because we don't feel like
153                                 -- generating curried versions "just in case".
154
155   | UnfoldIfGoodArgs    Int     -- if "m" type args and "n" value args; and
156                         Int     -- those val args are manifestly data constructors
157                         [Bool]  -- the val-arg positions marked True
158                                 -- (i.e., a simplification will definitely
159                                 -- be possible).
160                         Int     -- The "size" of the unfolding; to be elaborated
161                                 -- later. ToDo
162
163   | BadUnfolding                -- This is used by TcPragmas if the *lazy*
164                                 -- lintUnfolding test fails
165                                 -- It will never escape from the IdInfo as
166                                 -- it is caught by getInfo_UF and converted
167                                 -- to NoUnfoldingDetails
168 \end{code}
169
170 \begin{code}
171 instance Outputable UnfoldingGuidance where
172     ppr sty UnfoldNever         = ppStr "_N_"
173     ppr sty UnfoldAlways        = ppStr "_ALWAYS_"
174     ppr sty EssentialUnfolding  = ppStr "_ESSENTIAL_" -- shouldn't appear in an iface
175     ppr sty (UnfoldIfGoodArgs t v cs size)
176       = ppCat [ppStr "_IF_ARGS_", ppInt t, ppInt v,
177                if null cs       -- always print *something*
178                 then ppChar 'X'
179                 else ppBesides (map pp_c cs),
180                ppInt size ]
181       where
182         pp_c False = ppChar 'X'
183         pp_c True  = ppChar 'C'
184 \end{code}
185
186
187 %************************************************************************
188 %*                                                                      *
189 \subsection{@mkGenForm@ and friends}
190 %*                                                                      *
191 %************************************************************************
192
193 \begin{code}
194 mkGenForm :: FormSummary
195           -> TemplateOutExpr    -- Template
196           -> UnfoldingGuidance  -- Tells about the *size* of the template.
197           -> UnfoldingDetails
198
199 mkGenForm = GenForm
200
201 -- two shorthand variants:
202 mkLitForm lit      = mk_go_for_it (Lit lit)
203 mkConForm con args = mk_go_for_it (Con con args)
204
205 mk_go_for_it expr = mkGenForm WhnfForm expr UnfoldAlways
206 \end{code}
207
208 %************************************************************************
209 %*                                                                      *
210 \subsection[calcUnfoldingGuidance]{Calculate ``unfolding guidance'' for an expression}
211 %*                                                                      *
212 %************************************************************************
213
214 \begin{code}
215 calcUnfoldingGuidance
216         :: Bool             -- True <=> OK if _scc_s appear in expr
217         -> Int              -- bomb out if size gets bigger than this
218         -> CoreExpr    -- expression to look at
219         -> UnfoldingGuidance
220
221 calcUnfoldingGuidance scc_s_OK bOMB_OUT_SIZE expr
222   = let
223         (use_binders, ty_binders, val_binders, body) = collectBinders expr
224     in
225     case (sizeExpr scc_s_OK bOMB_OUT_SIZE val_binders body) of
226
227       Nothing                -> UnfoldNever
228
229       Just (size, cased_args)
230         -> let
231                uf = UnfoldIfGoodArgs
232                         (length ty_binders)
233                         (length val_binders)
234                         [ b `is_elem` cased_args | b <- val_binders ]
235                         size
236            in
237            -- pprTrace "calcUnfold:" (ppAbove (ppr PprDebug uf) (ppr PprDebug expr))
238            uf
239   where
240     is_elem = isIn "calcUnfoldingGuidance"
241 \end{code}
242
243 \begin{code}
244 sizeExpr :: Bool            -- True <=> _scc_s OK
245          -> Int             -- Bomb out if it gets bigger than this
246          -> [Id]            -- Arguments; we're interested in which of these
247                             -- get case'd
248          -> CoreExpr
249          -> Maybe (Int,     -- Size
250                    [Id]     -- Subset of args which are cased
251             )
252
253 sizeExpr scc_s_OK bOMB_OUT_SIZE args expr
254   = size_up expr
255   where
256     size_up (Var v)        = sizeOne
257     size_up (App fun arg)  = size_up fun `addSize` size_up_arg arg
258     size_up (Lit lit)      = if isNoRepLit lit
259                                then sizeN uNFOLDING_NOREP_LIT_COST
260                                else sizeOne
261
262     size_up (SCC _ (Con _ _)) = Nothing -- **** HACK *****
263     size_up (SCC lbl body)
264       = if scc_s_OK then size_up body else Nothing
265
266     size_up (Coerce _ _ body) = size_up body
267
268     size_up (Con con args) = -- 1 + # of val args
269                              sizeN (1 + numValArgs args)
270     size_up (Prim op args) = sizeN op_cost -- NB: no charge for PrimOp args
271       where
272         op_cost = if primOpCanTriggerGC op
273                   then uNFOLDING_DEAR_OP_COST
274                         -- these *tend* to be more expensive;
275                         -- number chosen to avoid unfolding (HACK)
276                   else uNFOLDING_CHEAP_OP_COST
277
278     size_up expr@(Lam _ _)
279       = let
280             (uvars, tyvars, args, body) = collectBinders expr
281         in
282         size_up body `addSizeN` length args
283
284     size_up (Let (NonRec binder rhs) body)
285       = size_up rhs
286                 `addSize`
287         size_up body
288                 `addSizeN`
289         1
290
291     size_up (Let (Rec pairs) body)
292       = foldr addSize sizeZero [size_up rhs | (_,rhs) <- pairs]
293                 `addSize`
294         size_up body
295                 `addSizeN`
296         length pairs
297
298     size_up (Case scrut alts)
299       = size_up_scrut scrut
300                 `addSize`
301         size_up_alts (coreExprType scrut) alts
302             -- We charge for the "case" itself in "size_up_alts"
303
304     ------------
305     size_up_arg arg = if isValArg arg then sizeOne else sizeZero{-it's free-}
306
307     ------------
308     size_up_alts scrut_ty (AlgAlts alts deflt)
309       = foldr (addSize . size_alg_alt) (size_up_deflt deflt) alts
310                 `addSizeN` (tyConFamilySize tycon)
311         -- NB: we charge N for an alg. "case", where N is
312         -- the number of constructors in the thing being eval'd.
313         -- (You'll eventually get a "discount" of N if you
314         -- think the "case" is likely to go away.)
315       where
316         size_alg_alt (con,args,rhs) = size_up rhs
317             -- Don't charge for args, so that wrappers look cheap
318
319         (tycon, _, _) = _trace "CoreUnfold.getAppDataTyConExpandingDicts" $ getAppDataTyConExpandingDicts scrut_ty
320
321     size_up_alts _ (PrimAlts alts deflt)
322       = foldr (addSize . size_prim_alt) (size_up_deflt deflt) alts
323             -- *no charge* for a primitive "case"!
324       where
325         size_prim_alt (lit,rhs) = size_up rhs
326
327     ------------
328     size_up_deflt NoDefault = sizeZero
329     size_up_deflt (BindDefault binder rhs) = size_up rhs
330
331     ------------
332         -- Scrutinees.  There are two things going on here.
333         -- First, we want to record if we're case'ing an argument
334         -- Second, we want to charge nothing for the srutinee if it's just
335         -- a variable.  That way wrapper-like things look cheap.
336     size_up_scrut (Var v) | v `is_elem` args = Just (0, [v])
337                             | otherwise        = Just (0, [])
338     size_up_scrut other                        = size_up other
339
340     is_elem :: Id -> [Id] -> Bool
341     is_elem = isIn "size_up_scrut"
342
343     ------------
344     sizeZero  = Just (0, [])
345     sizeOne   = Just (1, [])
346     sizeN n   = Just (n, [])
347     sizeVar v = Just (0, [v])
348
349     addSizeN Nothing _ = Nothing
350     addSizeN (Just (n, xs)) m
351       | tot < bOMB_OUT_SIZE = Just (tot, xs)
352       | otherwise = Nothing
353       where
354         tot = n+m
355
356     addSize Nothing _ = Nothing
357     addSize _ Nothing = Nothing
358     addSize (Just (n, xs)) (Just (m, ys))
359       | tot < bOMB_OUT_SIZE = Just (tot, xys)
360       | otherwise  = Nothing
361       where
362         tot = n+m
363         xys = xs ++ ys
364 \end{code}
365
366 %************************************************************************
367 %*                                                                      *
368 \subsection[unfoldings-for-ifaces]{Processing unfoldings for interfaces}
369 %*                                                                      *
370 %************************************************************************
371
372 Of course, the main thing we do to unfoldings-for-interfaces is {\em
373 print} them.  But, while we're at it, we collect info about
374 ``mentioned'' Ids, etc., etc.---we're going to need this stuff anyway.
375
376 %************************************************************************
377 %*                                                                      *
378 \subsubsection{Monad stuff for the unfolding-generation game}
379 %*                                                                      *
380 %************************************************************************
381
382 \begin{code}
383 type UnfoldM bndr thing
384         =  IdSet        -- in-scope Ids (passed downwards only)
385         -> (bndr -> Id) -- to extract an Id from a binder (down only)
386
387         -> (Bag Id,     -- mentioned global vars (ditto)
388             Bag TyCon,  -- ditto, tycons
389             Bag Class,  -- ditto, classes
390             Bool)       -- True <=> mentions something litlit-ish
391
392         -> (thing, (Bag Id, Bag TyCon, Bag Class, Bool)) -- accumulated...
393 \end{code}
394
395 A little stuff for in-scopery:
396 \begin{code}
397 no_in_scopes :: IdSet
398 add1         :: IdSet -> Id   -> IdSet
399 add_some     :: IdSet -> [Id] -> IdSet
400
401 no_in_scopes            = emptyUniqSet
402 in_scopes `add1`     x  = addOneToUniqSet in_scopes x
403 in_scopes `add_some` xs = in_scopes `unionUniqSets` mkUniqSet xs
404 \end{code}
405
406 The can-see-inside-monad functions are the usual sorts of things.
407
408 \begin{code}
409 thenUf :: UnfoldM bndr a -> (a -> UnfoldM bndr b) -> UnfoldM bndr b
410 thenUf m k in_scopes get_id mentioneds
411   = case m in_scopes get_id mentioneds of { (v, mentioneds1) ->
412     k v in_scopes get_id mentioneds1 }
413
414 thenUf_ :: UnfoldM bndr a -> UnfoldM bndr b -> UnfoldM bndr b
415 thenUf_ m k in_scopes get_id mentioneds
416   = case m in_scopes get_id mentioneds of { (_, mentioneds1) ->
417     k in_scopes get_id mentioneds1 }
418
419 mapUf :: (a -> UnfoldM bndr b) -> [a] -> UnfoldM bndr [b]
420 mapUf f []     = returnUf []
421 mapUf f (x:xs)
422   = f x         `thenUf` \ r ->
423     mapUf f xs  `thenUf` \ rs ->
424     returnUf (r:rs)
425
426 returnUf :: a -> UnfoldM bndr a
427 returnUf v in_scopes get_id mentioneds = (v, mentioneds)
428
429 addInScopesUf :: [Id] -> UnfoldM bndr a -> UnfoldM bndr a
430 addInScopesUf more_in_scopes m in_scopes get_id mentioneds
431   = m (in_scopes `add_some` more_in_scopes) get_id mentioneds
432
433 getInScopesUf :: UnfoldM bndr IdSet
434 getInScopesUf in_scopes get_id mentioneds = (in_scopes, mentioneds)
435
436 extractIdsUf :: [bndr] -> UnfoldM bndr [Id]
437 extractIdsUf binders in_scopes get_id mentioneds
438   = (map get_id binders, mentioneds)
439
440 consider_Id :: Id -> UnfoldM bndr ()
441 consider_Id var in_scopes get_id (ids, tcs, clss, has_litlit)
442   = let
443         (ids2, tcs2, clss2) = whatsMentionedInId in_scopes var
444     in
445     ((), (ids `unionBags` ids2,
446           tcs `unionBags` tcs2,
447           clss `unionBags`clss2,
448           has_litlit))
449 \end{code}
450
451 \begin{code}
452 addToMentionedIdsUf     :: Id -> UnfoldM bndr ()
453 addToMentionedTyConsUf  :: Bag TyCon -> UnfoldM bndr ()
454 addToMentionedClassesUf :: Bag Class -> UnfoldM bndr ()
455 litlit_oops             :: UnfoldM bndr ()
456
457 addToMentionedIdsUf add_me in_scopes get_id (ids, tcs, clss, has_litlit)
458   = ((), (ids `unionBags` unitBag add_me, tcs, clss, has_litlit))
459
460 addToMentionedTyConsUf add_mes in_scopes get_id (ids, tcs, clss, has_litlit)
461   = ((), (ids, tcs `unionBags` add_mes, clss, has_litlit))
462
463 addToMentionedClassesUf add_mes in_scopes get_id (ids, tcs, clss, has_litlit)
464   = ((), (ids, tcs, clss `unionBags` add_mes, has_litlit))
465
466 litlit_oops in_scopes get_id (ids, tcs, clss, _)
467   = ((), (ids, tcs, clss, True))
468 \end{code}
469
470
471 %************************************************************************
472 %*                                                                      *
473 \subsubsection{Gathering up info for an interface-unfolding}
474 %*                                                                      *
475 %************************************************************************
476
477 \begin{code}
478 {-
479 mentionedInUnfolding
480         :: (bndr -> Id)         -- so we can get Ids out of binders
481         -> GenCoreExpr bndr Id  -- input expression
482         -> (Bag Id, Bag TyCon, Bag Class,
483                                 -- what we found mentioned in the expr
484             Bool                -- True <=> mentions a ``litlit''-ish thing
485                                 -- (the guy on the other side of an interface
486                                 -- may not be able to handle it)
487            )
488 -}
489
490 mentionedInUnfolding get_id expr
491   = case (ment_expr expr no_in_scopes get_id (emptyBag, emptyBag, emptyBag, False)) of
492       (_, (ids_bag, tcs_bag, clss_bag, has_litlit)) ->
493         (ids_bag, tcs_bag, clss_bag, has_litlit)
494 \end{code}
495
496 \begin{code}
497 --ment_expr :: GenCoreExpr bndr Id -> UnfoldM bndr ()
498
499 ment_expr (Var v) = consider_Id  v
500 ment_expr (Lit l) = consider_lit l
501
502 ment_expr expr@(Lam _ _)
503   = let
504         (uvars, tyvars, args, body) = collectBinders expr
505     in
506     extractIdsUf args           `thenUf` \ bs_ids ->
507     addInScopesUf bs_ids (
508         -- this considering is just to extract any mentioned types/classes
509         mapUf consider_Id bs_ids   `thenUf_`
510         ment_expr body
511     )
512
513 ment_expr (App fun arg)
514   = ment_expr fun       `thenUf_`
515     ment_arg  arg
516
517 ment_expr (Con c as)
518   = consider_Id c       `thenUf_`
519     mapUf ment_arg as   `thenUf_`
520     returnUf ()
521
522 ment_expr (Prim op as)
523   = ment_op op          `thenUf_`
524     mapUf ment_arg as   `thenUf_`
525     returnUf ()
526   where
527     ment_op (CCallOp str is_asm may_gc arg_tys res_ty)
528       = mapUf ment_ty arg_tys   `thenUf_`
529         ment_ty res_ty
530     ment_op other_op = returnUf ()
531
532 ment_expr (Case scrutinee alts)
533   = ment_expr scrutinee `thenUf_`
534     ment_alts alts
535
536 ment_expr (Let (NonRec bind rhs) body)
537   = ment_expr rhs       `thenUf_`
538     extractIdsUf [bind] `thenUf` \ bi@[bind_id] ->
539     addInScopesUf bi    (
540     ment_expr body      `thenUf_`
541     consider_Id bind_id )
542
543 ment_expr (Let (Rec pairs) body)
544   = let
545         binders = map fst pairs
546         rhss    = map snd pairs
547     in
548     extractIdsUf binders        `thenUf` \ binder_ids ->
549     addInScopesUf binder_ids (
550         mapUf ment_expr rhss         `thenUf_`
551         mapUf consider_Id binder_ids `thenUf_`
552         ment_expr body )
553
554 ment_expr (SCC cc expr)
555   = (case (ccMentionsId cc) of
556       Just id -> consider_Id id
557       Nothing -> returnUf ()
558     )
559     `thenUf_` ment_expr expr
560
561 ment_expr (Coerce _ _ _) = panic "ment_expr:Coerce"
562
563 -------------
564 ment_ty ty
565   = let
566         (tycons, clss) = getMentionedTyConsAndClassesFromType ty
567     in
568     addToMentionedTyConsUf  tycons  `thenUf_`
569     addToMentionedClassesUf clss
570
571 -------------
572
573 ment_alts alg_alts@(AlgAlts alts deflt)
574   = mapUf ment_alt alts   `thenUf_`
575     ment_deflt deflt
576   where
577     ment_alt alt@(con, params, rhs)
578       = consider_Id con         `thenUf_`
579         extractIdsUf params     `thenUf` \ param_ids ->
580         addInScopesUf param_ids (
581           -- "consider" them so we can chk out their types...
582           mapUf consider_Id param_ids `thenUf_`
583           ment_expr rhs )
584
585 ment_alts (PrimAlts alts deflt)
586   = mapUf ment_alt alts   `thenUf_`
587     ment_deflt deflt
588   where
589     ment_alt alt@(lit, rhs) = ment_expr rhs
590
591 ----------------
592 ment_deflt NoDefault
593   = returnUf ()
594
595 ment_deflt d@(BindDefault b rhs)
596   = extractIdsUf [b]            `thenUf` \ bi@[b_id] ->
597     addInScopesUf bi            (
598         consider_Id b_id `thenUf_`
599         ment_expr rhs )
600
601 -----------
602 ment_arg (VarArg   v)  = consider_Id  v
603 ment_arg (LitArg   l)  = consider_lit l
604 ment_arg (TyArg    ty) = ment_ty ty
605 ment_arg (UsageArg _)  = returnUf ()
606
607 -----------
608 consider_lit lit
609   | isLitLitLit lit = litlit_oops `thenUf_` returnUf ()
610   | otherwise       = returnUf ()
611 \end{code}
612
613 %************************************************************************
614 %*                                                                      *
615 \subsubsection{Printing unfoldings in interfaces}
616 %*                                                                      *
617 %************************************************************************
618
619 Printing Core-expression unfoldings is sufficiently delicate that we
620 give it its own function.
621 \begin{code}
622 {- OLD:
623 pprCoreUnfolding
624         :: CoreExpr
625         -> Pretty
626
627 pprCoreUnfolding expr
628   = let
629         (_, renamed) = instCoreExpr uniqSupply_u expr
630             -- We rename every unfolding with a "steady" unique supply,
631             -- so that the names won't constantly change.
632             -- One place we *MUST NOT* use a splittable UniqueSupply!
633     in
634     ppr_uf_Expr emptyUniqSet renamed
635
636 ppr_Unfolding = PprUnfolding (panic "CoreUnfold:ppr_Unfolding")
637 \end{code}
638
639 \begin{code}
640 ppr_uf_Expr in_scopes (Var v) = pprIdInUnfolding in_scopes v
641 ppr_uf_Expr in_scopes (Lit l) = ppr ppr_Unfolding l
642
643 ppr_uf_Expr in_scopes (Con c as)
644   = ppBesides [ppPStr SLIT("_!_ "), pprIdInUnfolding no_in_scopes c, ppSP,
645            ppLbrack, ppIntersperse pp'SP{-'-} (map (pprParendUniType ppr_Unfolding) ts), ppRbrack,
646            ppSP, ppLbrack, ppIntersperse pp'SP{-'-} (map (ppr_uf_Atom in_scopes) as), ppRbrack]
647 ppr_uf_Expr in_scopes (Prim op as)
648   = ppBesides [ppPStr SLIT("_#_ "), ppr ppr_Unfolding op, ppSP,
649            ppLbrack, ppIntersperse pp'SP{-'-} (map (pprParendUniType ppr_Unfolding) ts), ppRbrack,
650            ppSP, ppLbrack, ppIntersperse pp'SP{-'-} (map (ppr_uf_Atom in_scopes) as), ppRbrack]
651
652 ppr_uf_Expr in_scopes (Lam binder body)
653   = ppCat [ppChar '\\', ppr_uf_Binder binder,
654            ppPStr SLIT("->"), ppr_uf_Expr (in_scopes `add1` binder) body]
655
656 ppr_uf_Expr in_scopes (CoTyLam tyvar expr)
657   = ppCat [ppPStr SLIT("_/\\_"), interppSP ppr_Unfolding (tyvar:tyvars), ppStr "->",
658            ppr_uf_Expr in_scopes body]
659   where
660     (tyvars, body) = collect_tyvars expr
661
662     collect_tyvars (CoTyLam tyv e) = ( tyv:tyvs, e_after )
663       where (tyvs, e_after) = collect_tyvars e
664     collect_tyvars other_e         = ( [], other_e )
665
666 ppr_uf_Expr in_scopes expr@(App fun_expr atom)
667   = let
668         (fun, args) = collect_args expr []
669     in
670     ppCat [ppPStr SLIT("_APP_ "), ppr_uf_Expr in_scopes fun, ppLbrack,
671            ppIntersperse pp'SP{-'-} (map (ppr_uf_Atom in_scopes) args), ppRbrack]
672   where
673     collect_args (App fun arg) args = collect_args fun (arg:args)
674     collect_args fun             args = (fun, args)
675
676 ppr_uf_Expr in_scopes (CoTyApp expr ty)
677   = ppCat [ppPStr SLIT("_TYAPP_ "), ppr_uf_Expr in_scopes expr,
678         ppChar '{', pprParendUniType ppr_Unfolding ty, ppChar '}']
679
680 ppr_uf_Expr in_scopes (Case scrutinee alts)
681   = ppCat [ppPStr SLIT("case"), ppr_uf_Expr in_scopes scrutinee, ppStr "of {",
682            pp_alts alts, ppChar '}']
683   where
684     pp_alts (AlgAlts  alts deflt)
685       = ppCat [ppPStr SLIT("_ALG_"),  ppCat (map pp_alg  alts), pp_deflt deflt]
686     pp_alts (PrimAlts alts deflt)
687       = ppCat [ppPStr SLIT("_PRIM_"), ppCat (map pp_prim alts), pp_deflt deflt]
688
689     pp_alg (con, params, rhs)
690       = ppBesides [pprIdInUnfolding no_in_scopes con, ppSP,
691                    ppIntersperse ppSP (map ppr_uf_Binder params),
692                    ppPStr SLIT(" -> "), ppr_uf_Expr (in_scopes `add_some` params) rhs, ppSemi]
693
694     pp_prim (lit, rhs)
695       = ppBesides [ppr ppr_Unfolding lit,
696                    ppPStr SLIT(" -> "), ppr_uf_Expr in_scopes rhs, ppSemi]
697
698     pp_deflt NoDefault = ppPStr SLIT("_NO_DEFLT_")
699     pp_deflt (BindDefault binder rhs)
700       = ppBesides [ppr_uf_Binder binder, ppPStr SLIT(" -> "),
701                    ppr_uf_Expr (in_scopes `add1` binder) rhs]
702
703 ppr_uf_Expr in_scopes (Let (NonRec binder rhs) body)
704   = ppBesides [ppStr "let {", ppr_uf_Binder binder, ppPStr SLIT(" = "), ppr_uf_Expr in_scopes rhs,
705         ppStr "} in ", ppr_uf_Expr (in_scopes `add1` binder) body]
706
707 ppr_uf_Expr in_scopes (Let (Rec pairs) body)
708   = ppBesides [ppStr "_LETREC_ {", ppIntersperse sep (map pp_pair pairs),
709         ppStr "} in ", ppr_uf_Expr new_in_scopes body]
710   where
711     sep = ppBeside ppSemi ppSP
712     new_in_scopes = in_scopes `add_some` map fst pairs
713
714     pp_pair (b, rhs) = ppCat [ppr_uf_Binder b, ppEquals, ppr_uf_Expr new_in_scopes rhs]
715
716 ppr_uf_Expr in_scopes (SCC cc body)
717   = ASSERT(not (noCostCentreAttached cc))
718     ASSERT(not (currentOrSubsumedCosts cc))
719     ppBesides [ppStr "_scc_ { ", ppStr (showCostCentre ppr_Unfolding False{-not as string-} cc), ppStr " } ",  ppr_uf_Expr in_scopes body]
720
721 ppr_uf_Expr in_scopes (Coerce _ _ _) = panic "ppr_uf_Expr:Coerce"
722 \end{code}
723
724 \begin{code}
725 ppr_uf_Binder :: Id -> Pretty
726 ppr_uf_Binder v
727   = ppBesides [ppLparen, pprIdInUnfolding (unitUniqSet v) v, ppPStr SLIT(" :: "),
728                ppr ppr_Unfolding (idType v), ppRparen]
729
730 ppr_uf_Atom in_scopes (LitArg l) = ppr ppr_Unfolding l
731 ppr_uf_Atom in_scopes (VarArg v) = pprIdInUnfolding in_scopes v
732 END OLD -}
733 \end{code}