40fbef753a756e2c6dd718a4bb06f643c3fde0f5
[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         stgArgHasCafRefs,
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, idPrimRep, idType, idCafInfo )
53 import IdInfo           ( mayHaveCafRefs )
54 import Name             ( isDllName )
55 import Literal          ( Literal, literalType, literalPrimRep )
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 rhsHasCafRefs (StgRhsClosure _ _ _ upd srt _ _) 
400   = isUpdatable upd || nonEmptySRT srt
401 rhsHasCafRefs (StgRhsCon _ _ args)
402   = any stgArgHasCafRefs args
403
404 stgArgHasCafRefs (StgVarArg id) = mayHaveCafRefs (idCafInfo id)
405 stgArgHasCafRefs _ = False
406 \end{code}
407
408 Here's the @StgBinderInfo@ type, and its combining op:
409 \begin{code}
410 data StgBinderInfo
411   = NoStgBinderInfo
412   | SatCallsOnly        -- All occurrences are *saturated* *function* calls
413                         -- This means we don't need to build an info table and 
414                         -- slow entry code for the thing
415                         -- Thunks never get this value
416
417 noBinderInfo = NoStgBinderInfo
418 stgUnsatOcc  = NoStgBinderInfo
419 stgSatOcc    = SatCallsOnly
420
421 satCallsOnly :: StgBinderInfo -> Bool
422 satCallsOnly SatCallsOnly    = True
423 satCallsOnly NoStgBinderInfo = False
424
425 combineStgBinderInfo :: StgBinderInfo -> StgBinderInfo -> StgBinderInfo
426 combineStgBinderInfo SatCallsOnly SatCallsOnly = SatCallsOnly
427 combineStgBinderInfo info1 info2               = NoStgBinderInfo
428
429 --------------
430 pp_binder_info NoStgBinderInfo = empty
431 pp_binder_info SatCallsOnly    = ptext SLIT("sat-only")
432 \end{code}
433
434 %************************************************************************
435 %*                                                                      *
436 \subsection[Stg-case-alternatives]{STG case alternatives}
437 %*                                                                      *
438 %************************************************************************
439
440 Very like in @CoreSyntax@ (except no type-world stuff).
441
442 The type constructor is guaranteed not to be abstract; that is, we can
443 see its representation.  This is important because the code generator
444 uses it to determine return conventions etc.  But it's not trivial
445 where there's a moduule loop involved, because some versions of a type
446 constructor might not have all the constructors visible.  So
447 mkStgAlgAlts (in CoreToStg) ensures that it gets the TyCon from the
448 constructors or literals (which are guaranteed to have the Real McCoy)
449 rather than from the scrutinee type.
450
451 \begin{code}
452 type GenStgAlt bndr occ
453   = (AltCon,            -- alts: data constructor,
454      [bndr],            -- constructor's parameters,
455      [Bool],            -- "use mask", same length as
456                         -- parameters; a True in a
457                         -- param's position if it is
458                         -- used in the ...
459      GenStgExpr bndr occ)       -- ...right-hand side.
460
461 data AltType
462   = PolyAlt             -- Polymorphic (a type variable)
463   | UbxTupAlt TyCon     -- Unboxed tuple
464   | AlgAlt    TyCon     -- Algebraic data type; the AltCons will be DataAlts
465   | PrimAlt   TyCon     -- Primitive data type; the AltCons will be LitAlts
466 \end{code}
467
468 %************************************************************************
469 %*                                                                      *
470 \subsection[Stg]{The Plain STG parameterisation}
471 %*                                                                      *
472 %************************************************************************
473
474 This happens to be the only one we use at the moment.
475
476 \begin{code}
477 type StgBinding     = GenStgBinding     Id Id
478 type StgArg         = GenStgArg         Id
479 type StgLiveVars    = GenStgLiveVars    Id
480 type StgExpr        = GenStgExpr        Id Id
481 type StgRhs         = GenStgRhs         Id Id
482 type StgAlt         = GenStgAlt         Id Id
483 \end{code}
484
485 %************************************************************************
486 %*                                                                      *
487 \subsubsection[UpdateFlag-datatype]{@UpdateFlag@}
488 %*                                                                      *
489 %************************************************************************
490
491 This is also used in @LambdaFormInfo@ in the @ClosureInfo@ module.
492
493 A @ReEntrant@ closure may be entered multiple times, but should not be
494 updated or blackholed.  An @Updatable@ closure should be updated after
495 evaluation (and may be blackholed during evaluation).  A @SingleEntry@
496 closure will only be entered once, and so need not be updated but may
497 safely be blackholed.
498
499 \begin{code}
500 data UpdateFlag = ReEntrant | Updatable | SingleEntry
501
502 instance Outputable UpdateFlag where
503     ppr u
504       = char (case u of { ReEntrant -> 'r';  Updatable -> 'u';  SingleEntry -> 's' })
505
506 isUpdatable ReEntrant   = False
507 isUpdatable SingleEntry = False
508 isUpdatable Updatable   = True
509 \end{code}
510
511 %************************************************************************
512 %*                                                                      *
513 \subsubsection{StgOp}
514 %*                                                                      *
515 %************************************************************************
516
517 An StgOp allows us to group together PrimOps and ForeignCalls.
518 It's quite useful to move these around together, notably
519 in StgOpApp and COpStmt.
520
521 \begin{code}
522 data StgOp = StgPrimOp  PrimOp
523
524            | StgFCallOp ForeignCall Unique
525                 -- The Unique is occasionally needed by the C pretty-printer
526                 -- (which lacks a unique supply), notably when generating a
527                 -- typedef for foreign-export-dynamic
528 \end{code}
529
530
531 %************************************************************************
532 %*                                                                      *
533 \subsubsection[Static Reference Tables]{@SRT@}
534 %*                                                                      *
535 %************************************************************************
536
537 There is one SRT per top-level function group.  Each local binding and
538 case expression within this binding group has a subrange of the whole
539 SRT, expressed as an offset and length.
540
541 In CoreToStg we collect the list of CafRefs at each SRT site, which is later 
542 converted into the length and offset form by the SRT pass.
543
544 \begin{code}
545 data SRT = NoSRT
546          | SRTEntries IdSet
547                 -- generated by CoreToStg
548          | SRT !Int{-offset-} !Int{-length-} !Bitmap{-bitmap-}
549                 -- generated by computeSRTs
550
551 noSRT :: SRT
552 noSRT = NoSRT
553
554 nonEmptySRT NoSRT           = False
555 nonEmptySRT (SRTEntries vs) = not (isEmptyVarSet vs)
556 nonEmptySRT _               = True
557
558 pprSRT (NoSRT) = ptext SLIT("_no_srt_")
559 pprSRT (SRTEntries ids) = text "SRT:" <> ppr ids
560 pprSRT (SRT off length bitmap) = parens (ppr off <> comma <> text "*bitmap*")
561 \end{code}
562
563 %************************************************************************
564 %*                                                                      *
565 \subsection[Stg-pretty-printing]{Pretty-printing}
566 %*                                                                      *
567 %************************************************************************
568
569 Robin Popplestone asked for semi-colon separators on STG binds; here's
570 hoping he likes terminators instead...  Ditto for case alternatives.
571
572 \begin{code}
573 pprGenStgBinding :: (Outputable bndr, Outputable bdee, Ord bdee)
574                  => GenStgBinding bndr bdee -> SDoc
575
576 pprGenStgBinding (StgNonRec bndr rhs)
577   = hang (hsep [ppr bndr, equals])
578         4 ((<>) (ppr rhs) semi)
579
580 pprGenStgBinding (StgRec pairs)
581   = vcat ((ifPprDebug (ptext SLIT("{- StgRec (begin) -}"))) :
582            (map (ppr_bind) pairs) ++ [(ifPprDebug (ptext SLIT("{- StgRec (end) -}")))])
583   where
584     ppr_bind (bndr, expr)
585       = hang (hsep [ppr bndr, equals])
586              4 ((<>) (ppr expr) semi)
587
588 pprStgBinding  :: StgBinding -> SDoc
589 pprStgBinding  bind  = pprGenStgBinding bind
590
591 pprStgBindings :: [StgBinding] -> SDoc
592 pprStgBindings binds = vcat (map pprGenStgBinding binds)
593
594 pprGenStgBindingWithSRT  
595         :: (Outputable bndr, Outputable bdee, Ord bdee) 
596         => (GenStgBinding bndr bdee,[(Id,[Id])]) -> SDoc
597
598 pprGenStgBindingWithSRT (bind,srts)
599   = vcat (pprGenStgBinding bind : map pprSRT srts)
600   where pprSRT (id,srt) = 
601            ptext SLIT("SRT") <> parens (ppr id) <> ptext SLIT(": ") <> ppr srt
602
603 pprStgBindingsWithSRTs :: [(StgBinding,[(Id,[Id])])] -> SDoc
604 pprStgBindingsWithSRTs binds = vcat (map pprGenStgBindingWithSRT binds)
605 \end{code}
606
607 \begin{code}
608 instance (Outputable bdee) => Outputable (GenStgArg bdee) where
609     ppr = pprStgArg
610
611 instance (Outputable bndr, Outputable bdee, Ord bdee)
612                 => Outputable (GenStgBinding bndr bdee) where
613     ppr = pprGenStgBinding
614
615 instance (Outputable bndr, Outputable bdee, Ord bdee)
616                 => Outputable (GenStgExpr bndr bdee) where
617     ppr = pprStgExpr
618
619 instance (Outputable bndr, Outputable bdee, Ord bdee)
620                 => Outputable (GenStgRhs bndr bdee) where
621     ppr rhs = pprStgRhs rhs
622 \end{code}
623
624 \begin{code}
625 pprStgArg :: (Outputable bdee) => GenStgArg bdee -> SDoc
626
627 pprStgArg (StgVarArg var) = ppr var
628 pprStgArg (StgLitArg con) = ppr con
629 pprStgArg (StgTypeArg ty) = char '@' <+> ppr ty
630 \end{code}
631
632 \begin{code}
633 pprStgExpr :: (Outputable bndr, Outputable bdee, Ord bdee)
634            => GenStgExpr bndr bdee -> SDoc
635 -- special case
636 pprStgExpr (StgLit lit)     = ppr lit
637
638 -- general case
639 pprStgExpr (StgApp func args)
640   = hang (ppr func)
641          4 (sep (map (ppr) args))
642 \end{code}
643
644 \begin{code}
645 pprStgExpr (StgConApp con args)
646   = hsep [ ppr con, brackets (interppSP args)]
647
648 pprStgExpr (StgOpApp op args _)
649   = hsep [ pprStgOp op, brackets (interppSP args)]
650
651 pprStgExpr (StgLam _ bndrs body)
652   =sep [ char '\\' <+> ppr bndrs <+> ptext SLIT("->"),
653          pprStgExpr body ]
654 \end{code}
655
656 \begin{code}
657 -- special case: let v = <very specific thing>
658 --               in
659 --               let ...
660 --               in
661 --               ...
662 --
663 -- Very special!  Suspicious! (SLPJ)
664
665 {-
666 pprStgExpr (StgLet srt (StgNonRec bndr (StgRhsClosure cc bi free_vars upd_flag args rhs))
667                         expr@(StgLet _ _))
668   = ($$)
669       (hang (hcat [ptext SLIT("let { "), ppr bndr, ptext SLIT(" = "),
670                           ppr cc,
671                           pp_binder_info bi,
672                           ptext SLIT(" ["), ifPprDebug (interppSP free_vars), ptext SLIT("] \\"),
673                           ppr upd_flag, ptext SLIT(" ["),
674                           interppSP args, char ']'])
675             8 (sep [hsep [ppr rhs, ptext SLIT("} in")]]))
676       (ppr expr)
677 -}
678
679 -- special case: let ... in let ...
680
681 pprStgExpr (StgLet bind expr@(StgLet _ _))
682   = ($$)
683       (sep [hang (ptext SLIT("let {"))
684                 2 (hsep [pprGenStgBinding bind, ptext SLIT("} in")])])
685       (ppr expr)
686
687 -- general case
688 pprStgExpr (StgLet bind expr)
689   = sep [hang (ptext SLIT("let {")) 2 (pprGenStgBinding bind),
690            hang (ptext SLIT("} in ")) 2 (ppr expr)]
691
692 pprStgExpr (StgLetNoEscape lvs_whole lvs_rhss bind expr)
693   = sep [hang (ptext SLIT("let-no-escape {"))
694                 2 (pprGenStgBinding bind),
695            hang ((<>) (ptext SLIT("} in "))
696                    (ifPprDebug (
697                     nest 4 (
698                       hcat [ptext  SLIT("-- lvs: ["), interppSP (uniqSetToList lvs_whole),
699                              ptext SLIT("]; rhs lvs: ["), interppSP (uniqSetToList lvs_rhss),
700                              char ']']))))
701                 2 (ppr expr)]
702
703 pprStgExpr (StgSCC cc expr)
704   = sep [ hsep [ptext SLIT("_scc_"), ppr cc],
705           pprStgExpr expr ]
706
707 pprStgExpr (StgCase expr lvs_whole lvs_rhss bndr srt alt_type alts)
708   = sep [sep [ptext SLIT("case"),
709            nest 4 (hsep [pprStgExpr expr,
710              ifPprDebug (dcolon <+> ppr alt_type)]),
711            ptext SLIT("of"), ppr bndr, char '{'],
712            ifPprDebug (
713            nest 4 (
714              hcat [ptext  SLIT("-- lvs: ["), interppSP (uniqSetToList lvs_whole),
715                     ptext SLIT("]; rhs lvs: ["), interppSP (uniqSetToList lvs_rhss),
716                     ptext SLIT("]; "),
717                     pprMaybeSRT srt])),
718            nest 2 (vcat (map pprStgAlt alts)),
719            char '}']
720
721 pprStgAlt (con, params, use_mask, expr)
722   = hang (hsep [ppr con, interppSP params, ptext SLIT("->")])
723          4 (ppr expr <> semi)
724
725 pprStgOp (StgPrimOp  op)   = ppr op
726 pprStgOp (StgFCallOp op _) = ppr op
727
728 instance Outputable AltType where
729   ppr PolyAlt        = ptext SLIT("Polymorphic")
730   ppr (UbxTupAlt tc) = ptext SLIT("UbxTup") <+> ppr tc
731   ppr (AlgAlt tc)    = ptext SLIT("Alg")    <+> ppr tc
732   ppr (PrimAlt tc)   = ptext SLIT("Prim")   <+> ppr tc
733 \end{code}
734
735 \begin{code}
736 pprStgLVs :: Outputable occ => GenStgLiveVars occ -> SDoc
737 pprStgLVs lvs
738   = getPprStyle $ \ sty ->
739     if userStyle sty || isEmptyUniqSet lvs then
740         empty
741     else
742         hcat [text "{-lvs:", interpp'SP (uniqSetToList lvs), text "-}"]
743 \end{code}
744
745 \begin{code}
746 pprStgRhs :: (Outputable bndr, Outputable bdee, Ord bdee)
747           => GenStgRhs bndr bdee -> SDoc
748
749 -- special case
750 pprStgRhs (StgRhsClosure cc bi [free_var] upd_flag srt [{-no args-}] (StgApp func []))
751   = hcat [ ppr cc,
752            pp_binder_info bi,
753            brackets (ifPprDebug (ppr free_var)),
754            ptext SLIT(" \\"), ppr upd_flag, pprMaybeSRT srt, ptext SLIT(" [] "), ppr func ]
755
756 -- general case
757 pprStgRhs (StgRhsClosure cc bi free_vars upd_flag srt args body)
758   = hang (hsep [if opt_SccProfilingOn then ppr cc else empty,
759                 pp_binder_info bi,
760                 ifPprDebug (brackets (interppSP free_vars)),
761                 char '\\' <> ppr upd_flag, pprMaybeSRT srt, brackets (interppSP args)])
762          4 (ppr body)
763
764 pprStgRhs (StgRhsCon cc con args)
765   = hcat [ ppr cc,
766            space, ppr con, ptext SLIT("! "), brackets (interppSP args)]
767
768 pprMaybeSRT (NoSRT) = empty
769 pprMaybeSRT srt     = ptext SLIT("srt:") <> pprSRT srt
770 \end{code}