a6f1868b6ea8c3580b97d9cbcdd730f6ec03715c
[ghc-hetmet.git] / ghc / compiler / stgSyn / StgSyn.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[StgSyn]{Shared term graph (STG) syntax for spineless-tagless code generation}
5
6 This data type represents programs just before code generation
7 (conversion to @AbstractC@): basically, what we have is a stylised
8 form of @CoreSyntax@, the style being one that happens to be ideally
9 suited to spineless tagless code generation.
10
11 \begin{code}
12 module StgSyn (
13         GenStgArg(..), 
14         GenStgLiveVars,
15
16         GenStgBinding(..), GenStgExpr(..), GenStgRhs(..),
17         GenStgAlt, AltType(..),
18
19         UpdateFlag(..), isUpdatable,
20
21         StgBinderInfo,
22         noBinderInfo, stgSatOcc, stgUnsatOcc, satCallsOnly,
23         combineStgBinderInfo,
24
25         -- a set of synonyms for the most common (only :-) parameterisation
26         StgArg, StgLiveVars,
27         StgBinding, StgExpr, StgRhs, StgAlt, 
28
29         -- StgOp
30         StgOp(..),
31
32         -- SRTs
33         SRT(..),
34
35         -- utils
36         stgBindHasCafRefs, stgArgHasCafRefs, stgRhsArity,
37         isDllConApp, isStgTypeArg,
38         stgArgType,
39
40         pprStgBinding, pprStgBindings, pprStgBindingsWithSRTs
41
42 #ifdef DEBUG
43         , pprStgLVs
44 #endif
45     ) where
46
47 #include "HsVersions.h"
48
49 import CostCentre       ( CostCentreStack, CostCentre )
50 import VarSet           ( IdSet, isEmptyVarSet )
51 import Var              ( isId )
52 import Id               ( Id, idName, idType, idCafInfo )
53 import IdInfo           ( mayHaveCafRefs )
54 import Name             ( isDllName )
55 import Literal          ( Literal, literalType )
56 import ForeignCall      ( ForeignCall )
57 import DataCon          ( DataCon, dataConName )
58 import CoreSyn          ( AltCon )
59 import PprCore          ( {- instances -} )
60 import PrimOp           ( PrimOp )
61 import Outputable
62 import Util             ( count )
63 import Type             ( Type )
64 import TyCon            ( TyCon )
65 import UniqSet          ( isEmptyUniqSet, uniqSetToList, UniqSet )
66 import Unique           ( Unique )
67 import Bitmap
68 import CmdLineOpts      ( opt_SccProfilingOn )
69 \end{code}
70
71 %************************************************************************
72 %*                                                                      *
73 \subsection{@GenStgBinding@}
74 %*                                                                      *
75 %************************************************************************
76
77 As usual, expressions are interesting; other things are boring.  Here
78 are the boring things [except note the @GenStgRhs@], parameterised
79 with respect to binder and occurrence information (just as in
80 @CoreSyn@):
81
82 There is one SRT for each group of bindings.
83
84 \begin{code}
85 data GenStgBinding bndr occ
86   = StgNonRec   bndr (GenStgRhs bndr occ)
87   | StgRec      [(bndr, GenStgRhs bndr occ)]
88 \end{code}
89
90 %************************************************************************
91 %*                                                                      *
92 \subsection{@GenStgArg@}
93 %*                                                                      *
94 %************************************************************************
95
96 \begin{code}
97 data GenStgArg occ
98   = StgVarArg   occ
99   | StgLitArg   Literal
100   | StgTypeArg  Type            -- For when we want to preserve all type info
101 \end{code}
102
103 \begin{code}
104 isStgTypeArg (StgTypeArg _) = True
105 isStgTypeArg other          = False
106
107 isDllArg :: StgArg -> Bool
108         -- Does this argument refer to something in a different DLL?
109 isDllArg (StgTypeArg v)  = False
110 isDllArg (StgVarArg v)   = isDllName (idName v)
111 isDllArg (StgLitArg lit) = False
112
113 isDllConApp :: DataCon -> [StgArg] -> Bool
114         -- Does this constructor application refer to 
115         -- anything in a different DLL?
116         -- If so, we can't allocate it statically
117 isDllConApp con args = isDllName (dataConName con) || any isDllArg args
118
119 stgArgType :: StgArg -> Type
120         -- Very half baked becase we have lost the type arguments
121 stgArgType (StgVarArg v)   = idType v
122 stgArgType (StgLitArg lit) = literalType lit
123 stgArgType (StgTypeArg lit) = panic "stgArgType called on stgTypeArg"
124 \end{code}
125
126 %************************************************************************
127 %*                                                                      *
128 \subsection{STG expressions}
129 %*                                                                      *
130 %************************************************************************
131
132 The @GenStgExpr@ data type is parameterised on binder and occurrence
133 info, as before.
134
135 %************************************************************************
136 %*                                                                      *
137 \subsubsection{@GenStgExpr@ application}
138 %*                                                                      *
139 %************************************************************************
140
141 An application is of a function to a list of atoms [not expressions].
142 Operationally, we want to push the arguments on the stack and call the
143 function.  (If the arguments were expressions, we would have to build
144 their closures first.)
145
146 There is no constructor for a lone variable; it would appear as
147 @StgApp var [] _@.
148 \begin{code}
149 type GenStgLiveVars occ = UniqSet occ
150
151 data GenStgExpr bndr occ
152   = StgApp
153         occ             -- function
154         [GenStgArg occ] -- arguments; may be empty
155 \end{code}
156
157 %************************************************************************
158 %*                                                                      *
159 \subsubsection{@StgConApp@ and @StgPrimApp@---saturated applications}
160 %*                                                                      *
161 %************************************************************************
162
163 There are a specialised forms of application, for
164 constructors, primitives, and literals.
165 \begin{code}
166   | StgLit      Literal
167   
168   | StgConApp   DataCon
169                 [GenStgArg occ] -- Saturated
170
171   | StgOpApp    StgOp           -- Primitive op or foreign call
172                 [GenStgArg occ] -- Saturated
173                 Type            -- Result type; we need to know the result type
174                                 -- so that we can assign result registers.
175 \end{code}
176
177 %************************************************************************
178 %*                                                                      *
179 \subsubsection{@StgLam@}
180 %*                                                                      *
181 %************************************************************************
182
183 StgLam is used *only* during CoreToStg's work.  Before CoreToStg has finished
184 it encodes (\x -> e) as (let f = \x -> e in f)
185
186 \begin{code}
187   | StgLam
188         Type            -- Type of whole lambda (useful when making a binder for it)
189         [bndr]
190         StgExpr         -- Body of lambda
191 \end{code}
192
193
194 %************************************************************************
195 %*                                                                      *
196 \subsubsection{@GenStgExpr@: case-expressions}
197 %*                                                                      *
198 %************************************************************************
199
200 This has the same boxed/unboxed business as Core case expressions.
201 \begin{code}
202   | StgCase
203         (GenStgExpr bndr occ)
204                         -- the thing to examine
205
206         (GenStgLiveVars occ) -- Live vars of whole case expression, 
207                         -- plus everything that happens after the case
208                         -- i.e., those which mustn't be overwritten
209
210         (GenStgLiveVars occ) -- Live vars of RHSs (plus what happens afterwards)
211                         -- i.e., those which must be saved before eval.
212                         --
213                         -- note that an alt's constructor's
214                         -- binder-variables are NOT counted in the
215                         -- free vars for the alt's RHS
216
217         bndr            -- binds the result of evaluating the scrutinee
218
219         SRT             -- The SRT for the continuation
220
221         AltType 
222
223         [GenStgAlt bndr occ]    -- The DEFAULT case is always *first* 
224                                 -- if it is there at all
225 \end{code}
226
227 %************************************************************************
228 %*                                                                      *
229 \subsubsection{@GenStgExpr@:  @let(rec)@-expressions}
230 %*                                                                      *
231 %************************************************************************
232
233 The various forms of let(rec)-expression encode most of the
234 interesting things we want to do.
235 \begin{enumerate}
236 \item
237 \begin{verbatim}
238 let-closure x = [free-vars] expr [args]
239 in e
240 \end{verbatim}
241 is equivalent to
242 \begin{verbatim}
243 let x = (\free-vars -> \args -> expr) free-vars
244 \end{verbatim}
245 \tr{args} may be empty (and is for most closures).  It isn't under
246 circumstances like this:
247 \begin{verbatim}
248 let x = (\y -> y+z)
249 \end{verbatim}
250 This gets mangled to
251 \begin{verbatim}
252 let-closure x = [z] [y] (y+z)
253 \end{verbatim}
254 The idea is that we compile code for @(y+z)@ in an environment in which
255 @z@ is bound to an offset from \tr{Node}, and @y@ is bound to an
256 offset from the stack pointer.
257
258 (A let-closure is an @StgLet@ with a @StgRhsClosure@ RHS.)
259
260 \item
261 \begin{verbatim}
262 let-constructor x = Constructor [args]
263 in e
264 \end{verbatim}
265
266 (A let-constructor is an @StgLet@ with a @StgRhsCon@ RHS.)
267
268 \item
269 Letrec-expressions are essentially the same deal as
270 let-closure/let-constructor, so we use a common structure and
271 distinguish between them with an @is_recursive@ boolean flag.
272
273 \item
274 \begin{verbatim}
275 let-unboxed u = an arbitrary arithmetic expression in unboxed values
276 in e
277 \end{verbatim}
278 All the stuff on the RHS must be fully evaluated.  No function calls either!
279
280 (We've backed away from this toward case-expressions with
281 suitably-magical alts ...)
282
283 \item
284 ~[Advanced stuff here!  Not to start with, but makes pattern matching
285 generate more efficient code.]
286
287 \begin{verbatim}
288 let-escapes-not fail = expr
289 in e'
290 \end{verbatim}
291 Here the idea is that @e'@ guarantees not to put @fail@ in a data structure,
292 or pass it to another function.  All @e'@ will ever do is tail-call @fail@.
293 Rather than build a closure for @fail@, all we need do is to record the stack
294 level at the moment of the @let-escapes-not@; then entering @fail@ is just
295 a matter of adjusting the stack pointer back down to that point and entering
296 the code for it.
297
298 Another example:
299 \begin{verbatim}
300 f x y = let z = huge-expression in
301         if y==1 then z else
302         if y==2 then z else
303         1
304 \end{verbatim}
305
306 (A let-escapes-not is an @StgLetNoEscape@.)
307
308 \item
309 We may eventually want:
310 \begin{verbatim}
311 let-literal x = Literal
312 in e
313 \end{verbatim}
314
315 (ToDo: is this obsolete?)
316 \end{enumerate}
317
318 And so the code for let(rec)-things:
319 \begin{code}
320   | StgLet
321         (GenStgBinding bndr occ)        -- right hand sides (see below)
322         (GenStgExpr bndr occ)           -- body
323
324   | StgLetNoEscape                      -- remember: ``advanced stuff''
325         (GenStgLiveVars occ)            -- Live in the whole let-expression
326                                         -- Mustn't overwrite these stack slots
327                                         -- *Doesn't* include binders of the let(rec).
328
329         (GenStgLiveVars occ)            -- Live in the right hand sides (only)
330                                         -- These are the ones which must be saved on
331                                         -- the stack if they aren't there already
332                                         -- *Does* include binders of the let(rec) if recursive.
333
334         (GenStgBinding bndr occ)        -- right hand sides (see below)
335         (GenStgExpr bndr occ)           -- body
336 \end{code}
337
338 %************************************************************************
339 %*                                                                      *
340 \subsubsection{@GenStgExpr@: @scc@ expressions}
341 %*                                                                      *
342 %************************************************************************
343
344 Finally for @scc@ expressions we introduce a new STG construct.
345
346 \begin{code}
347   | StgSCC
348         CostCentre              -- label of SCC expression
349         (GenStgExpr bndr occ)   -- scc expression
350   -- end of GenStgExpr
351 \end{code}
352
353 %************************************************************************
354 %*                                                                      *
355 \subsection{STG right-hand sides}
356 %*                                                                      *
357 %************************************************************************
358
359 Here's the rest of the interesting stuff for @StgLet@s; the first
360 flavour is for closures:
361 \begin{code}
362 data GenStgRhs bndr occ
363   = StgRhsClosure
364         CostCentreStack         -- CCS to be attached (default is CurrentCCS)
365         StgBinderInfo           -- Info about how this binder is used (see below)
366         [occ]                   -- non-global free vars; a list, rather than
367                                 -- a set, because order is important
368         !UpdateFlag             -- ReEntrant | Updatable | SingleEntry
369         SRT                     -- The SRT reference
370         [bndr]                  -- arguments; if empty, then not a function;
371                                 -- as above, order is important.
372         (GenStgExpr bndr occ)   -- body
373 \end{code}
374 An example may be in order.  Consider:
375 \begin{verbatim}
376 let t = \x -> \y -> ... x ... y ... p ... q in e
377 \end{verbatim}
378 Pulling out the free vars and stylising somewhat, we get the equivalent:
379 \begin{verbatim}
380 let t = (\[p,q] -> \[x,y] -> ... x ... y ... p ...q) p q
381 \end{verbatim}
382 Stg-operationally, the @[x,y]@ are on the stack, the @[p,q]@ are
383 offsets from @Node@ into the closure, and the code ptr for the closure
384 will be exactly that in parentheses above.
385
386 The second flavour of right-hand-side is for constructors (simple but important):
387 \begin{code}
388   | StgRhsCon
389         CostCentreStack         -- CCS to be attached (default is CurrentCCS).
390                                 -- Top-level (static) ones will end up with
391                                 -- DontCareCCS, because we don't count static
392                                 -- data in heap profiles, and we don't set CCCS
393                                 -- from static closure.
394         DataCon                 -- constructor
395         [GenStgArg occ] -- args
396 \end{code}
397
398 \begin{code}
399 stgRhsArity :: StgRhs -> Int
400 stgRhsArity (StgRhsClosure _ _ _ _ _ bndrs _) = count isId bndrs
401   -- The arity never includes type parameters, so
402   -- when keeping type arguments and binders in the Stg syntax 
403   -- (opt_RuntimeTypes) we have to fliter out the type binders.
404 stgRhsArity (StgRhsCon _ _ _) = 0
405 \end{code}
406
407 \begin{code}
408 stgBindHasCafRefs :: GenStgBinding bndr Id -> Bool
409 stgBindHasCafRefs (StgNonRec _ rhs) = rhsHasCafRefs rhs
410 stgBindHasCafRefs (StgRec binds)    = any rhsHasCafRefs (map snd binds)
411
412 rhsHasCafRefs (StgRhsClosure _ _ _ upd srt _ _) 
413   = isUpdatable upd || nonEmptySRT srt
414 rhsHasCafRefs (StgRhsCon _ _ args)
415   = any stgArgHasCafRefs args
416
417 stgArgHasCafRefs (StgVarArg id) = mayHaveCafRefs (idCafInfo id)
418 stgArgHasCafRefs _ = False
419 \end{code}
420
421 Here's the @StgBinderInfo@ type, and its combining op:
422 \begin{code}
423 data StgBinderInfo
424   = NoStgBinderInfo
425   | SatCallsOnly        -- All occurrences are *saturated* *function* calls
426                         -- This means we don't need to build an info table and 
427                         -- slow entry code for the thing
428                         -- Thunks never get this value
429
430 noBinderInfo = NoStgBinderInfo
431 stgUnsatOcc  = NoStgBinderInfo
432 stgSatOcc    = SatCallsOnly
433
434 satCallsOnly :: StgBinderInfo -> Bool
435 satCallsOnly SatCallsOnly    = True
436 satCallsOnly NoStgBinderInfo = False
437
438 combineStgBinderInfo :: StgBinderInfo -> StgBinderInfo -> StgBinderInfo
439 combineStgBinderInfo SatCallsOnly SatCallsOnly = SatCallsOnly
440 combineStgBinderInfo info1 info2               = NoStgBinderInfo
441
442 --------------
443 pp_binder_info NoStgBinderInfo = empty
444 pp_binder_info SatCallsOnly    = ptext SLIT("sat-only")
445 \end{code}
446
447 %************************************************************************
448 %*                                                                      *
449 \subsection[Stg-case-alternatives]{STG case alternatives}
450 %*                                                                      *
451 %************************************************************************
452
453 Very like in @CoreSyntax@ (except no type-world stuff).
454
455 The type constructor is guaranteed not to be abstract; that is, we can
456 see its representation.  This is important because the code generator
457 uses it to determine return conventions etc.  But it's not trivial
458 where there's a moduule loop involved, because some versions of a type
459 constructor might not have all the constructors visible.  So
460 mkStgAlgAlts (in CoreToStg) ensures that it gets the TyCon from the
461 constructors or literals (which are guaranteed to have the Real McCoy)
462 rather than from the scrutinee type.
463
464 \begin{code}
465 type GenStgAlt bndr occ
466   = (AltCon,            -- alts: data constructor,
467      [bndr],            -- constructor's parameters,
468      [Bool],            -- "use mask", same length as
469                         -- parameters; a True in a
470                         -- param's position if it is
471                         -- used in the ...
472      GenStgExpr bndr occ)       -- ...right-hand side.
473
474 data AltType
475   = PolyAlt             -- Polymorphic (a type variable)
476   | UbxTupAlt TyCon     -- Unboxed tuple
477   | AlgAlt    TyCon     -- Algebraic data type; the AltCons will be DataAlts
478   | PrimAlt   TyCon     -- Primitive data type; the AltCons will be LitAlts
479 \end{code}
480
481 %************************************************************************
482 %*                                                                      *
483 \subsection[Stg]{The Plain STG parameterisation}
484 %*                                                                      *
485 %************************************************************************
486
487 This happens to be the only one we use at the moment.
488
489 \begin{code}
490 type StgBinding     = GenStgBinding     Id Id
491 type StgArg         = GenStgArg         Id
492 type StgLiveVars    = GenStgLiveVars    Id
493 type StgExpr        = GenStgExpr        Id Id
494 type StgRhs         = GenStgRhs         Id Id
495 type StgAlt         = GenStgAlt         Id Id
496 \end{code}
497
498 %************************************************************************
499 %*                                                                      *
500 \subsubsection[UpdateFlag-datatype]{@UpdateFlag@}
501 %*                                                                      *
502 %************************************************************************
503
504 This is also used in @LambdaFormInfo@ in the @ClosureInfo@ module.
505
506 A @ReEntrant@ closure may be entered multiple times, but should not be
507 updated or blackholed.  An @Updatable@ closure should be updated after
508 evaluation (and may be blackholed during evaluation).  A @SingleEntry@
509 closure will only be entered once, and so need not be updated but may
510 safely be blackholed.
511
512 \begin{code}
513 data UpdateFlag = ReEntrant | Updatable | SingleEntry
514
515 instance Outputable UpdateFlag where
516     ppr u
517       = char (case u of { ReEntrant -> 'r';  Updatable -> 'u';  SingleEntry -> 's' })
518
519 isUpdatable ReEntrant   = False
520 isUpdatable SingleEntry = False
521 isUpdatable Updatable   = True
522 \end{code}
523
524 %************************************************************************
525 %*                                                                      *
526 \subsubsection{StgOp}
527 %*                                                                      *
528 %************************************************************************
529
530 An StgOp allows us to group together PrimOps and ForeignCalls.
531 It's quite useful to move these around together, notably
532 in StgOpApp and COpStmt.
533
534 \begin{code}
535 data StgOp = StgPrimOp  PrimOp
536
537            | StgFCallOp ForeignCall Unique
538                 -- The Unique is occasionally needed by the C pretty-printer
539                 -- (which lacks a unique supply), notably when generating a
540                 -- typedef for foreign-export-dynamic
541 \end{code}
542
543
544 %************************************************************************
545 %*                                                                      *
546 \subsubsection[Static Reference Tables]{@SRT@}
547 %*                                                                      *
548 %************************************************************************
549
550 There is one SRT per top-level function group.  Each local binding and
551 case expression within this binding group has a subrange of the whole
552 SRT, expressed as an offset and length.
553
554 In CoreToStg we collect the list of CafRefs at each SRT site, which is later 
555 converted into the length and offset form by the SRT pass.
556
557 \begin{code}
558 data SRT = NoSRT
559          | SRTEntries IdSet
560                 -- generated by CoreToStg
561          | SRT !Int{-offset-} !Int{-length-} !Bitmap{-bitmap-}
562                 -- generated by computeSRTs
563
564 noSRT :: SRT
565 noSRT = NoSRT
566
567 nonEmptySRT NoSRT           = False
568 nonEmptySRT (SRTEntries vs) = not (isEmptyVarSet vs)
569 nonEmptySRT _               = True
570
571 pprSRT (NoSRT) = ptext SLIT("_no_srt_")
572 pprSRT (SRTEntries ids) = text "SRT:" <> ppr ids
573 pprSRT (SRT off length bitmap) = parens (ppr off <> comma <> text "*bitmap*")
574 \end{code}
575
576 %************************************************************************
577 %*                                                                      *
578 \subsection[Stg-pretty-printing]{Pretty-printing}
579 %*                                                                      *
580 %************************************************************************
581
582 Robin Popplestone asked for semi-colon separators on STG binds; here's
583 hoping he likes terminators instead...  Ditto for case alternatives.
584
585 \begin{code}
586 pprGenStgBinding :: (Outputable bndr, Outputable bdee, Ord bdee)
587                  => GenStgBinding bndr bdee -> SDoc
588
589 pprGenStgBinding (StgNonRec bndr rhs)
590   = hang (hsep [ppr bndr, equals])
591         4 ((<>) (ppr rhs) semi)
592
593 pprGenStgBinding (StgRec pairs)
594   = vcat ((ifPprDebug (ptext SLIT("{- StgRec (begin) -}"))) :
595            (map (ppr_bind) pairs) ++ [(ifPprDebug (ptext SLIT("{- StgRec (end) -}")))])
596   where
597     ppr_bind (bndr, expr)
598       = hang (hsep [ppr bndr, equals])
599              4 ((<>) (ppr expr) semi)
600
601 pprStgBinding  :: StgBinding -> SDoc
602 pprStgBinding  bind  = pprGenStgBinding bind
603
604 pprStgBindings :: [StgBinding] -> SDoc
605 pprStgBindings binds = vcat (map pprGenStgBinding binds)
606
607 pprGenStgBindingWithSRT  
608         :: (Outputable bndr, Outputable bdee, Ord bdee) 
609         => (GenStgBinding bndr bdee,[(Id,[Id])]) -> SDoc
610
611 pprGenStgBindingWithSRT (bind,srts)
612   = vcat (pprGenStgBinding bind : map pprSRT srts)
613   where pprSRT (id,srt) = 
614            ptext SLIT("SRT") <> parens (ppr id) <> ptext SLIT(": ") <> ppr srt
615
616 pprStgBindingsWithSRTs :: [(StgBinding,[(Id,[Id])])] -> SDoc
617 pprStgBindingsWithSRTs binds = vcat (map pprGenStgBindingWithSRT binds)
618 \end{code}
619
620 \begin{code}
621 instance (Outputable bdee) => Outputable (GenStgArg bdee) where
622     ppr = pprStgArg
623
624 instance (Outputable bndr, Outputable bdee, Ord bdee)
625                 => Outputable (GenStgBinding bndr bdee) where
626     ppr = pprGenStgBinding
627
628 instance (Outputable bndr, Outputable bdee, Ord bdee)
629                 => Outputable (GenStgExpr bndr bdee) where
630     ppr = pprStgExpr
631
632 instance (Outputable bndr, Outputable bdee, Ord bdee)
633                 => Outputable (GenStgRhs bndr bdee) where
634     ppr rhs = pprStgRhs rhs
635 \end{code}
636
637 \begin{code}
638 pprStgArg :: (Outputable bdee) => GenStgArg bdee -> SDoc
639
640 pprStgArg (StgVarArg var) = ppr var
641 pprStgArg (StgLitArg con) = ppr con
642 pprStgArg (StgTypeArg ty) = char '@' <+> ppr ty
643 \end{code}
644
645 \begin{code}
646 pprStgExpr :: (Outputable bndr, Outputable bdee, Ord bdee)
647            => GenStgExpr bndr bdee -> SDoc
648 -- special case
649 pprStgExpr (StgLit lit)     = ppr lit
650
651 -- general case
652 pprStgExpr (StgApp func args)
653   = hang (ppr func)
654          4 (sep (map (ppr) args))
655 \end{code}
656
657 \begin{code}
658 pprStgExpr (StgConApp con args)
659   = hsep [ ppr con, brackets (interppSP args)]
660
661 pprStgExpr (StgOpApp op args _)
662   = hsep [ pprStgOp op, brackets (interppSP args)]
663
664 pprStgExpr (StgLam _ bndrs body)
665   =sep [ char '\\' <+> ppr bndrs <+> ptext SLIT("->"),
666          pprStgExpr body ]
667 \end{code}
668
669 \begin{code}
670 -- special case: let v = <very specific thing>
671 --               in
672 --               let ...
673 --               in
674 --               ...
675 --
676 -- Very special!  Suspicious! (SLPJ)
677
678 {-
679 pprStgExpr (StgLet srt (StgNonRec bndr (StgRhsClosure cc bi free_vars upd_flag args rhs))
680                         expr@(StgLet _ _))
681   = ($$)
682       (hang (hcat [ptext SLIT("let { "), ppr bndr, ptext SLIT(" = "),
683                           ppr cc,
684                           pp_binder_info bi,
685                           ptext SLIT(" ["), ifPprDebug (interppSP free_vars), ptext SLIT("] \\"),
686                           ppr upd_flag, ptext SLIT(" ["),
687                           interppSP args, char ']'])
688             8 (sep [hsep [ppr rhs, ptext SLIT("} in")]]))
689       (ppr expr)
690 -}
691
692 -- special case: let ... in let ...
693
694 pprStgExpr (StgLet bind expr@(StgLet _ _))
695   = ($$)
696       (sep [hang (ptext SLIT("let {"))
697                 2 (hsep [pprGenStgBinding bind, ptext SLIT("} in")])])
698       (ppr expr)
699
700 -- general case
701 pprStgExpr (StgLet bind expr)
702   = sep [hang (ptext SLIT("let {")) 2 (pprGenStgBinding bind),
703            hang (ptext SLIT("} in ")) 2 (ppr expr)]
704
705 pprStgExpr (StgLetNoEscape lvs_whole lvs_rhss bind expr)
706   = sep [hang (ptext SLIT("let-no-escape {"))
707                 2 (pprGenStgBinding bind),
708            hang ((<>) (ptext SLIT("} in "))
709                    (ifPprDebug (
710                     nest 4 (
711                       hcat [ptext  SLIT("-- lvs: ["), interppSP (uniqSetToList lvs_whole),
712                              ptext SLIT("]; rhs lvs: ["), interppSP (uniqSetToList lvs_rhss),
713                              char ']']))))
714                 2 (ppr expr)]
715
716 pprStgExpr (StgSCC cc expr)
717   = sep [ hsep [ptext SLIT("_scc_"), ppr cc],
718           pprStgExpr expr ]
719
720 pprStgExpr (StgCase expr lvs_whole lvs_rhss bndr srt alt_type alts)
721   = sep [sep [ptext SLIT("case"),
722            nest 4 (hsep [pprStgExpr expr,
723              ifPprDebug (dcolon <+> ppr alt_type)]),
724            ptext SLIT("of"), ppr bndr, char '{'],
725            ifPprDebug (
726            nest 4 (
727              hcat [ptext  SLIT("-- lvs: ["), interppSP (uniqSetToList lvs_whole),
728                     ptext SLIT("]; rhs lvs: ["), interppSP (uniqSetToList lvs_rhss),
729                     ptext SLIT("]; "),
730                     pprMaybeSRT srt])),
731            nest 2 (vcat (map pprStgAlt alts)),
732            char '}']
733
734 pprStgAlt (con, params, use_mask, expr)
735   = hang (hsep [ppr con, interppSP params, ptext SLIT("->")])
736          4 (ppr expr <> semi)
737
738 pprStgOp (StgPrimOp  op)   = ppr op
739 pprStgOp (StgFCallOp op _) = ppr op
740
741 instance Outputable AltType where
742   ppr PolyAlt        = ptext SLIT("Polymorphic")
743   ppr (UbxTupAlt tc) = ptext SLIT("UbxTup") <+> ppr tc
744   ppr (AlgAlt tc)    = ptext SLIT("Alg")    <+> ppr tc
745   ppr (PrimAlt tc)   = ptext SLIT("Prim")   <+> ppr tc
746 \end{code}
747
748 \begin{code}
749 pprStgLVs :: Outputable occ => GenStgLiveVars occ -> SDoc
750 pprStgLVs lvs
751   = getPprStyle $ \ sty ->
752     if userStyle sty || isEmptyUniqSet lvs then
753         empty
754     else
755         hcat [text "{-lvs:", interpp'SP (uniqSetToList lvs), text "-}"]
756 \end{code}
757
758 \begin{code}
759 pprStgRhs :: (Outputable bndr, Outputable bdee, Ord bdee)
760           => GenStgRhs bndr bdee -> SDoc
761
762 -- special case
763 pprStgRhs (StgRhsClosure cc bi [free_var] upd_flag srt [{-no args-}] (StgApp func []))
764   = hcat [ ppr cc,
765            pp_binder_info bi,
766            brackets (ifPprDebug (ppr free_var)),
767            ptext SLIT(" \\"), ppr upd_flag, pprMaybeSRT srt, ptext SLIT(" [] "), ppr func ]
768
769 -- general case
770 pprStgRhs (StgRhsClosure cc bi free_vars upd_flag srt args body)
771   = hang (hsep [if opt_SccProfilingOn then ppr cc else empty,
772                 pp_binder_info bi,
773                 ifPprDebug (brackets (interppSP free_vars)),
774                 char '\\' <> ppr upd_flag, pprMaybeSRT srt, brackets (interppSP args)])
775          4 (ppr body)
776
777 pprStgRhs (StgRhsCon cc con args)
778   = hcat [ ppr cc,
779            space, ppr con, ptext SLIT("! "), brackets (interppSP args)]
780
781 pprMaybeSRT (NoSRT) = empty
782 pprMaybeSRT srt     = ptext SLIT("srt:") <> pprSRT srt
783 \end{code}