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