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