Haskell Program Coverage
[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 #include "HsVersions.h"
48
49 import CostCentre       ( CostCentreStack, CostCentre )
50 import VarSet           ( IdSet, isEmptyVarSet )
51 import Var              ( isId )
52 import Id               ( Id, idName, idType, idCafInfo )
53 import IdInfo           ( mayHaveCafRefs )
54 import Packages         ( isDllName )
55 import PackageConfig    ( PackageId )
56 import Literal          ( Literal, literalType )
57 import ForeignCall      ( ForeignCall )
58 import DataCon          ( DataCon, dataConName )
59 import CoreSyn          ( AltCon )
60 import PprCore          ( {- instances -} )
61 import PrimOp           ( PrimOp )
62 import Outputable
63 import Util             ( count )
64 import Type             ( Type )
65 import TyCon            ( TyCon )
66 import UniqSet          ( isEmptyUniqSet, uniqSetToList, UniqSet )
67 import Unique           ( Unique )
68 import Bitmap
69 import StaticFlags      ( opt_SccProfilingOn )
70 import Module           ( Module, pprModule )
71 \end{code}
72
73 %************************************************************************
74 %*                                                                      *
75 \subsection{@GenStgBinding@}
76 %*                                                                      *
77 %************************************************************************
78
79 As usual, expressions are interesting; other things are boring.  Here
80 are the boring things [except note the @GenStgRhs@], parameterised
81 with respect to binder and occurrence information (just as in
82 @CoreSyn@):
83
84 There is one SRT for each group of bindings.
85
86 \begin{code}
87 data GenStgBinding bndr occ
88   = StgNonRec   bndr (GenStgRhs bndr occ)
89   | StgRec      [(bndr, GenStgRhs bndr occ)]
90 \end{code}
91
92 %************************************************************************
93 %*                                                                      *
94 \subsection{@GenStgArg@}
95 %*                                                                      *
96 %************************************************************************
97
98 \begin{code}
99 data GenStgArg occ
100   = StgVarArg   occ
101   | StgLitArg   Literal
102   | StgTypeArg  Type            -- For when we want to preserve all type info
103 \end{code}
104
105 \begin{code}
106 isStgTypeArg (StgTypeArg _) = True
107 isStgTypeArg other          = False
108
109 isDllArg :: PackageId -> StgArg -> Bool
110         -- Does this argument refer to something in a different DLL?
111 isDllArg this_pkg (StgTypeArg v)  = False
112 isDllArg this_pkg (StgVarArg v)   = isDllName this_pkg (idName v)
113 isDllArg this_pkg (StgLitArg lit) = False
114
115 isDllConApp :: PackageId -> DataCon -> [StgArg] -> Bool
116         -- Does this constructor application refer to 
117         -- anything in a different DLL?
118         -- If so, we can't allocate it statically
119 isDllConApp this_pkg con args
120    = isDllName this_pkg (dataConName con) || any (isDllArg this_pkg) args
121
122 stgArgType :: StgArg -> Type
123         -- Very half baked becase we have lost the type arguments
124 stgArgType (StgVarArg v)   = idType v
125 stgArgType (StgLitArg lit) = literalType lit
126 stgArgType (StgTypeArg lit) = panic "stgArgType called on stgTypeArg"
127 \end{code}
128
129 %************************************************************************
130 %*                                                                      *
131 \subsection{STG expressions}
132 %*                                                                      *
133 %************************************************************************
134
135 The @GenStgExpr@ data type is parameterised on binder and occurrence
136 info, as before.
137
138 %************************************************************************
139 %*                                                                      *
140 \subsubsection{@GenStgExpr@ application}
141 %*                                                                      *
142 %************************************************************************
143
144 An application is of a function to a list of atoms [not expressions].
145 Operationally, we want to push the arguments on the stack and call the
146 function.  (If the arguments were expressions, we would have to build
147 their closures first.)
148
149 There is no constructor for a lone variable; it would appear as
150 @StgApp var [] _@.
151 \begin{code}
152 type GenStgLiveVars occ = UniqSet occ
153
154 data GenStgExpr bndr occ
155   = StgApp
156         occ             -- function
157         [GenStgArg occ] -- arguments; may be empty
158 \end{code}
159
160 %************************************************************************
161 %*                                                                      *
162 \subsubsection{@StgConApp@ and @StgPrimApp@---saturated applications}
163 %*                                                                      *
164 %************************************************************************
165
166 There are a specialised forms of application, for
167 constructors, primitives, and literals.
168 \begin{code}
169   | StgLit      Literal
170   
171   | StgConApp   DataCon
172                 [GenStgArg occ] -- Saturated
173
174   | StgOpApp    StgOp           -- Primitive op or foreign call
175                 [GenStgArg occ] -- Saturated
176                 Type            -- Result type; we need to know the result type
177                                 -- so that we can 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 _) = count isId bndrs
419   -- The arity never includes type parameters, so
420   -- when keeping type arguments and binders in the Stg syntax 
421   -- (opt_RuntimeTypes) we have to fliter out the type binders.
422 stgRhsArity (StgRhsCon _ _ _) = 0
423 \end{code}
424
425 \begin{code}
426 stgBindHasCafRefs :: GenStgBinding bndr Id -> Bool
427 stgBindHasCafRefs (StgNonRec _ rhs) = rhsHasCafRefs rhs
428 stgBindHasCafRefs (StgRec binds)    = any rhsHasCafRefs (map snd binds)
429
430 rhsHasCafRefs (StgRhsClosure _ _ _ upd srt _ _) 
431   = isUpdatable upd || nonEmptySRT srt
432 rhsHasCafRefs (StgRhsCon _ _ args)
433   = any stgArgHasCafRefs args
434
435 stgArgHasCafRefs (StgVarArg id) = mayHaveCafRefs (idCafInfo id)
436 stgArgHasCafRefs _ = False
437 \end{code}
438
439 Here's the @StgBinderInfo@ type, and its combining op:
440 \begin{code}
441 data StgBinderInfo
442   = NoStgBinderInfo
443   | SatCallsOnly        -- All occurrences are *saturated* *function* calls
444                         -- This means we don't need to build an info table and 
445                         -- slow entry code for the thing
446                         -- Thunks never get this value
447
448 noBinderInfo = NoStgBinderInfo
449 stgUnsatOcc  = NoStgBinderInfo
450 stgSatOcc    = SatCallsOnly
451
452 satCallsOnly :: StgBinderInfo -> Bool
453 satCallsOnly SatCallsOnly    = True
454 satCallsOnly NoStgBinderInfo = False
455
456 combineStgBinderInfo :: StgBinderInfo -> StgBinderInfo -> StgBinderInfo
457 combineStgBinderInfo SatCallsOnly SatCallsOnly = SatCallsOnly
458 combineStgBinderInfo info1 info2               = NoStgBinderInfo
459
460 --------------
461 pp_binder_info NoStgBinderInfo = empty
462 pp_binder_info SatCallsOnly    = ptext SLIT("sat-only")
463 \end{code}
464
465 %************************************************************************
466 %*                                                                      *
467 \subsection[Stg-case-alternatives]{STG case alternatives}
468 %*                                                                      *
469 %************************************************************************
470
471 Very like in @CoreSyntax@ (except no type-world stuff).
472
473 The type constructor is guaranteed not to be abstract; that is, we can
474 see its representation.  This is important because the code generator
475 uses it to determine return conventions etc.  But it's not trivial
476 where there's a moduule loop involved, because some versions of a type
477 constructor might not have all the constructors visible.  So
478 mkStgAlgAlts (in CoreToStg) ensures that it gets the TyCon from the
479 constructors or literals (which are guaranteed to have the Real McCoy)
480 rather than from the scrutinee type.
481
482 \begin{code}
483 type GenStgAlt bndr occ
484   = (AltCon,            -- alts: data constructor,
485      [bndr],            -- constructor's parameters,
486      [Bool],            -- "use mask", same length as
487                         -- parameters; a True in a
488                         -- param's position if it is
489                         -- used in the ...
490      GenStgExpr bndr occ)       -- ...right-hand side.
491
492 data AltType
493   = PolyAlt             -- Polymorphic (a type variable)
494   | UbxTupAlt TyCon     -- Unboxed tuple
495   | AlgAlt    TyCon     -- Algebraic data type; the AltCons will be DataAlts
496   | PrimAlt   TyCon     -- Primitive data type; the AltCons will be LitAlts
497 \end{code}
498
499 %************************************************************************
500 %*                                                                      *
501 \subsection[Stg]{The Plain STG parameterisation}
502 %*                                                                      *
503 %************************************************************************
504
505 This happens to be the only one we use at the moment.
506
507 \begin{code}
508 type StgBinding     = GenStgBinding     Id Id
509 type StgArg         = GenStgArg         Id
510 type StgLiveVars    = GenStgLiveVars    Id
511 type StgExpr        = GenStgExpr        Id Id
512 type StgRhs         = GenStgRhs         Id Id
513 type StgAlt         = GenStgAlt         Id Id
514 \end{code}
515
516 %************************************************************************
517 %*                                                                      *
518 \subsubsection[UpdateFlag-datatype]{@UpdateFlag@}
519 %*                                                                      *
520 %************************************************************************
521
522 This is also used in @LambdaFormInfo@ in the @ClosureInfo@ module.
523
524 A @ReEntrant@ closure may be entered multiple times, but should not be
525 updated or blackholed.  An @Updatable@ closure should be updated after
526 evaluation (and may be blackholed during evaluation).  A @SingleEntry@
527 closure will only be entered once, and so need not be updated but may
528 safely be blackholed.
529
530 \begin{code}
531 data UpdateFlag = ReEntrant | Updatable | SingleEntry
532
533 instance Outputable UpdateFlag where
534     ppr u
535       = char (case u of { ReEntrant -> 'r';  Updatable -> 'u';  SingleEntry -> 's' })
536
537 isUpdatable ReEntrant   = False
538 isUpdatable SingleEntry = False
539 isUpdatable Updatable   = True
540 \end{code}
541
542 %************************************************************************
543 %*                                                                      *
544 \subsubsection{StgOp}
545 %*                                                                      *
546 %************************************************************************
547
548 An StgOp allows us to group together PrimOps and ForeignCalls.
549 It's quite useful to move these around together, notably
550 in StgOpApp and COpStmt.
551
552 \begin{code}
553 data StgOp = StgPrimOp  PrimOp
554
555            | StgFCallOp ForeignCall Unique
556                 -- The Unique is occasionally needed by the C pretty-printer
557                 -- (which lacks a unique supply), notably when generating a
558                 -- typedef for foreign-export-dynamic
559 \end{code}
560
561
562 %************************************************************************
563 %*                                                                      *
564 \subsubsection[Static Reference Tables]{@SRT@}
565 %*                                                                      *
566 %************************************************************************
567
568 There is one SRT per top-level function group.  Each local binding and
569 case expression within this binding group has a subrange of the whole
570 SRT, expressed as an offset and length.
571
572 In CoreToStg we collect the list of CafRefs at each SRT site, which is later 
573 converted into the length and offset form by the SRT pass.
574
575 \begin{code}
576 data SRT = NoSRT
577          | SRTEntries IdSet
578                 -- generated by CoreToStg
579          | SRT !Int{-offset-} !Int{-length-} !Bitmap{-bitmap-}
580                 -- generated by computeSRTs
581
582 noSRT :: SRT
583 noSRT = NoSRT
584
585 nonEmptySRT NoSRT           = False
586 nonEmptySRT (SRTEntries vs) = not (isEmptyVarSet vs)
587 nonEmptySRT _               = True
588
589 pprSRT (NoSRT) = ptext SLIT("_no_srt_")
590 pprSRT (SRTEntries ids) = text "SRT:" <> ppr ids
591 pprSRT (SRT off length bitmap) = parens (ppr off <> comma <> text "*bitmap*")
592 \end{code}
593
594 %************************************************************************
595 %*                                                                      *
596 \subsection[Stg-pretty-printing]{Pretty-printing}
597 %*                                                                      *
598 %************************************************************************
599
600 Robin Popplestone asked for semi-colon separators on STG binds; here's
601 hoping he likes terminators instead...  Ditto for case alternatives.
602
603 \begin{code}
604 pprGenStgBinding :: (Outputable bndr, Outputable bdee, Ord bdee)
605                  => GenStgBinding bndr bdee -> SDoc
606
607 pprGenStgBinding (StgNonRec bndr rhs)
608   = hang (hsep [ppr bndr, equals])
609         4 ((<>) (ppr rhs) semi)
610
611 pprGenStgBinding (StgRec pairs)
612   = vcat ((ifPprDebug (ptext SLIT("{- StgRec (begin) -}"))) :
613            (map (ppr_bind) pairs) ++ [(ifPprDebug (ptext SLIT("{- StgRec (end) -}")))])
614   where
615     ppr_bind (bndr, expr)
616       = hang (hsep [ppr bndr, equals])
617              4 ((<>) (ppr expr) semi)
618
619 pprStgBinding  :: StgBinding -> SDoc
620 pprStgBinding  bind  = pprGenStgBinding bind
621
622 pprStgBindings :: [StgBinding] -> SDoc
623 pprStgBindings binds = vcat (map pprGenStgBinding binds)
624
625 pprGenStgBindingWithSRT  
626         :: (Outputable bndr, Outputable bdee, Ord bdee) 
627         => (GenStgBinding bndr bdee,[(Id,[Id])]) -> SDoc
628
629 pprGenStgBindingWithSRT (bind,srts)
630   = vcat (pprGenStgBinding bind : map pprSRT srts)
631   where pprSRT (id,srt) = 
632            ptext SLIT("SRT") <> parens (ppr id) <> ptext SLIT(": ") <> ppr srt
633
634 pprStgBindingsWithSRTs :: [(StgBinding,[(Id,[Id])])] -> SDoc
635 pprStgBindingsWithSRTs binds = vcat (map pprGenStgBindingWithSRT binds)
636 \end{code}
637
638 \begin{code}
639 instance (Outputable bdee) => Outputable (GenStgArg bdee) where
640     ppr = pprStgArg
641
642 instance (Outputable bndr, Outputable bdee, Ord bdee)
643                 => Outputable (GenStgBinding bndr bdee) where
644     ppr = pprGenStgBinding
645
646 instance (Outputable bndr, Outputable bdee, Ord bdee)
647                 => Outputable (GenStgExpr bndr bdee) where
648     ppr = pprStgExpr
649
650 instance (Outputable bndr, Outputable bdee, Ord bdee)
651                 => Outputable (GenStgRhs bndr bdee) where
652     ppr rhs = pprStgRhs rhs
653 \end{code}
654
655 \begin{code}
656 pprStgArg :: (Outputable bdee) => GenStgArg bdee -> SDoc
657
658 pprStgArg (StgVarArg var) = ppr var
659 pprStgArg (StgLitArg con) = ppr con
660 pprStgArg (StgTypeArg ty) = char '@' <+> ppr ty
661 \end{code}
662
663 \begin{code}
664 pprStgExpr :: (Outputable bndr, Outputable bdee, Ord bdee)
665            => GenStgExpr bndr bdee -> SDoc
666 -- special case
667 pprStgExpr (StgLit lit)     = ppr lit
668
669 -- general case
670 pprStgExpr (StgApp func args)
671   = hang (ppr func)
672          4 (sep (map (ppr) args))
673 \end{code}
674
675 \begin{code}
676 pprStgExpr (StgConApp con args)
677   = hsep [ ppr con, brackets (interppSP args)]
678
679 pprStgExpr (StgOpApp op args _)
680   = hsep [ pprStgOp op, brackets (interppSP args)]
681
682 pprStgExpr (StgLam _ bndrs body)
683   =sep [ char '\\' <+> ppr bndrs <+> ptext SLIT("->"),
684          pprStgExpr body ]
685 \end{code}
686
687 \begin{code}
688 -- special case: let v = <very specific thing>
689 --               in
690 --               let ...
691 --               in
692 --               ...
693 --
694 -- Very special!  Suspicious! (SLPJ)
695
696 {-
697 pprStgExpr (StgLet srt (StgNonRec bndr (StgRhsClosure cc bi free_vars upd_flag args rhs))
698                         expr@(StgLet _ _))
699   = ($$)
700       (hang (hcat [ptext SLIT("let { "), ppr bndr, ptext SLIT(" = "),
701                           ppr cc,
702                           pp_binder_info bi,
703                           ptext SLIT(" ["), ifPprDebug (interppSP free_vars), ptext SLIT("] \\"),
704                           ppr upd_flag, ptext SLIT(" ["),
705                           interppSP args, char ']'])
706             8 (sep [hsep [ppr rhs, ptext SLIT("} in")]]))
707       (ppr expr)
708 -}
709
710 -- special case: let ... in let ...
711
712 pprStgExpr (StgLet bind expr@(StgLet _ _))
713   = ($$)
714       (sep [hang (ptext SLIT("let {"))
715                 2 (hsep [pprGenStgBinding bind, ptext SLIT("} in")])])
716       (ppr expr)
717
718 -- general case
719 pprStgExpr (StgLet bind expr)
720   = sep [hang (ptext SLIT("let {")) 2 (pprGenStgBinding bind),
721            hang (ptext SLIT("} in ")) 2 (ppr expr)]
722
723 pprStgExpr (StgLetNoEscape lvs_whole lvs_rhss bind expr)
724   = sep [hang (ptext SLIT("let-no-escape {"))
725                 2 (pprGenStgBinding bind),
726            hang ((<>) (ptext SLIT("} in "))
727                    (ifPprDebug (
728                     nest 4 (
729                       hcat [ptext  SLIT("-- lvs: ["), interppSP (uniqSetToList lvs_whole),
730                              ptext SLIT("]; rhs lvs: ["), interppSP (uniqSetToList lvs_rhss),
731                              char ']']))))
732                 2 (ppr expr)]
733
734 pprStgExpr (StgSCC cc expr)
735   = sep [ hsep [ptext SLIT("_scc_"), ppr cc],
736           pprStgExpr expr ]
737
738 pprStgExpr (StgTick m n expr)
739   = sep [ hsep [ptext SLIT("_tick_"),  pprModule m,text (show n)],
740           pprStgExpr expr ]
741
742 pprStgExpr (StgCase expr lvs_whole lvs_rhss bndr srt alt_type alts)
743   = sep [sep [ptext SLIT("case"),
744            nest 4 (hsep [pprStgExpr expr,
745              ifPprDebug (dcolon <+> ppr alt_type)]),
746            ptext SLIT("of"), ppr bndr, char '{'],
747            ifPprDebug (
748            nest 4 (
749              hcat [ptext  SLIT("-- lvs: ["), interppSP (uniqSetToList lvs_whole),
750                     ptext SLIT("]; rhs lvs: ["), interppSP (uniqSetToList lvs_rhss),
751                     ptext SLIT("]; "),
752                     pprMaybeSRT srt])),
753            nest 2 (vcat (map pprStgAlt alts)),
754            char '}']
755
756 pprStgAlt (con, params, use_mask, expr)
757   = hang (hsep [ppr con, interppSP params, ptext SLIT("->")])
758          4 (ppr expr <> semi)
759
760 pprStgOp (StgPrimOp  op)   = ppr op
761 pprStgOp (StgFCallOp op _) = ppr op
762
763 instance Outputable AltType where
764   ppr PolyAlt        = ptext SLIT("Polymorphic")
765   ppr (UbxTupAlt tc) = ptext SLIT("UbxTup") <+> ppr tc
766   ppr (AlgAlt tc)    = ptext SLIT("Alg")    <+> ppr tc
767   ppr (PrimAlt tc)   = ptext SLIT("Prim")   <+> ppr tc
768 \end{code}
769
770 \begin{code}
771 pprStgLVs :: Outputable occ => GenStgLiveVars occ -> SDoc
772 pprStgLVs lvs
773   = getPprStyle $ \ sty ->
774     if userStyle sty || isEmptyUniqSet lvs then
775         empty
776     else
777         hcat [text "{-lvs:", interpp'SP (uniqSetToList lvs), text "-}"]
778 \end{code}
779
780 \begin{code}
781 pprStgRhs :: (Outputable bndr, Outputable bdee, Ord bdee)
782           => GenStgRhs bndr bdee -> SDoc
783
784 -- special case
785 pprStgRhs (StgRhsClosure cc bi [free_var] upd_flag srt [{-no args-}] (StgApp func []))
786   = hcat [ ppr cc,
787            pp_binder_info bi,
788            brackets (ifPprDebug (ppr free_var)),
789            ptext SLIT(" \\"), ppr upd_flag, pprMaybeSRT srt, ptext SLIT(" [] "), ppr func ]
790
791 -- general case
792 pprStgRhs (StgRhsClosure cc bi free_vars upd_flag srt args body)
793   = hang (hsep [if opt_SccProfilingOn then ppr cc else empty,
794                 pp_binder_info bi,
795                 ifPprDebug (brackets (interppSP free_vars)),
796                 char '\\' <> ppr upd_flag, pprMaybeSRT srt, brackets (interppSP args)])
797          4 (ppr body)
798
799 pprStgRhs (StgRhsCon cc con args)
800   = hcat [ ppr cc,
801            space, ppr con, ptext SLIT("! "), brackets (interppSP args)]
802
803 pprMaybeSRT (NoSRT) = empty
804 pprMaybeSRT srt     = ptext SLIT("srt:") <> pprSRT srt
805 \end{code}