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