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