2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[StgSyn]{Shared term graph (STG) syntax for spineless-tagless code generation}
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.
13 -- The above warning supression flag is a temporary kludge.
14 -- While working on this module you are encouraged to remove it and fix
15 -- any warnings in the module. See
16 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
23 GenStgBinding(..), GenStgExpr(..), GenStgRhs(..),
24 GenStgAlt, AltType(..),
26 UpdateFlag(..), isUpdatable,
29 noBinderInfo, stgSatOcc, stgUnsatOcc, satCallsOnly,
32 -- a set of synonyms for the most common (only :-) parameterisation
34 StgBinding, StgExpr, StgRhs, StgAlt,
43 stgBindHasCafRefs, stgArgHasCafRefs, stgRhsArity,
44 isDllConApp, isStgTypeArg,
47 pprStgBinding, pprStgBindings, pprStgBindingsWithSRTs
54 #include "HsVersions.h"
56 import CostCentre ( CostCentreStack, CostCentre )
57 import VarSet ( IdSet, isEmptyVarSet )
59 import Id ( Id, idName, idType, idCafInfo )
60 import IdInfo ( mayHaveCafRefs )
61 import Packages ( isDllName )
62 import Literal ( Literal, literalType )
63 import ForeignCall ( ForeignCall )
64 import DataCon ( DataCon, dataConName )
65 import CoreSyn ( AltCon )
66 import PprCore ( {- instances -} )
67 import PrimOp ( PrimOp )
71 import TyCon ( TyCon )
72 import UniqSet ( isEmptyUniqSet, uniqSetToList, UniqSet )
73 import Unique ( Unique )
75 import StaticFlags ( opt_SccProfilingOn )
79 %************************************************************************
81 \subsection{@GenStgBinding@}
83 %************************************************************************
85 As usual, expressions are interesting; other things are boring. Here
86 are the boring things [except note the @GenStgRhs@], parameterised
87 with respect to binder and occurrence information (just as in
90 There is one SRT for each group of bindings.
93 data GenStgBinding bndr occ
94 = StgNonRec bndr (GenStgRhs bndr occ)
95 | StgRec [(bndr, GenStgRhs bndr occ)]
98 %************************************************************************
100 \subsection{@GenStgArg@}
102 %************************************************************************
108 | StgTypeArg Type -- For when we want to preserve all type info
112 isStgTypeArg (StgTypeArg _) = True
113 isStgTypeArg other = False
115 isDllArg :: PackageId -> StgArg -> Bool
116 -- Does this argument refer to something in a different DLL?
117 isDllArg this_pkg (StgTypeArg v) = False
118 isDllArg this_pkg (StgVarArg v) = isDllName this_pkg (idName v)
119 isDllArg this_pkg (StgLitArg lit) = False
121 isDllConApp :: PackageId -> DataCon -> [StgArg] -> Bool
122 -- Does this constructor application refer to
123 -- anything in a different DLL?
124 -- If so, we can't allocate it statically
125 isDllConApp this_pkg con args
126 = isDllName this_pkg (dataConName con) || any (isDllArg this_pkg) args
128 stgArgType :: StgArg -> Type
129 -- Very half baked becase we have lost the type arguments
130 stgArgType (StgVarArg v) = idType v
131 stgArgType (StgLitArg lit) = literalType lit
132 stgArgType (StgTypeArg lit) = panic "stgArgType called on stgTypeArg"
135 %************************************************************************
137 \subsection{STG expressions}
139 %************************************************************************
141 The @GenStgExpr@ data type is parameterised on binder and occurrence
144 %************************************************************************
146 \subsubsection{@GenStgExpr@ application}
148 %************************************************************************
150 An application is of a function to a list of atoms [not expressions].
151 Operationally, we want to push the arguments on the stack and call the
152 function. (If the arguments were expressions, we would have to build
153 their closures first.)
155 There is no constructor for a lone variable; it would appear as
158 type GenStgLiveVars occ = UniqSet occ
160 data GenStgExpr bndr occ
163 [GenStgArg occ] -- arguments; may be empty
166 %************************************************************************
168 \subsubsection{@StgConApp@ and @StgPrimApp@---saturated applications}
170 %************************************************************************
172 There are a specialised forms of application, for
173 constructors, primitives, and literals.
177 -- StgConApp is vital for returning unboxed tuples
178 -- which can't be let-bound first
180 [GenStgArg occ] -- Saturated
182 | StgOpApp StgOp -- Primitive op or foreign call
183 [GenStgArg occ] -- Saturated
185 -- We need to know this so that we can
186 -- assign result registers
189 %************************************************************************
191 \subsubsection{@StgLam@}
193 %************************************************************************
195 StgLam is used *only* during CoreToStg's work. Before CoreToStg has finished
196 it encodes (\x -> e) as (let f = \x -> e in f)
200 Type -- Type of whole lambda (useful when making a binder for it)
202 StgExpr -- Body of lambda
206 %************************************************************************
208 \subsubsection{@GenStgExpr@: case-expressions}
210 %************************************************************************
212 This has the same boxed/unboxed business as Core case expressions.
215 (GenStgExpr bndr occ)
216 -- the thing to examine
218 (GenStgLiveVars occ) -- Live vars of whole case expression,
219 -- plus everything that happens after the case
220 -- i.e., those which mustn't be overwritten
222 (GenStgLiveVars occ) -- Live vars of RHSs (plus what happens afterwards)
223 -- i.e., those which must be saved before eval.
225 -- note that an alt's constructor's
226 -- binder-variables are NOT counted in the
227 -- free vars for the alt's RHS
229 bndr -- binds the result of evaluating the scrutinee
231 SRT -- The SRT for the continuation
235 [GenStgAlt bndr occ] -- The DEFAULT case is always *first*
236 -- if it is there at all
239 %************************************************************************
241 \subsubsection{@GenStgExpr@: @let(rec)@-expressions}
243 %************************************************************************
245 The various forms of let(rec)-expression encode most of the
246 interesting things we want to do.
250 let-closure x = [free-vars] expr [args]
255 let x = (\free-vars -> \args -> expr) free-vars
257 \tr{args} may be empty (and is for most closures). It isn't under
258 circumstances like this:
264 let-closure x = [z] [y] (y+z)
266 The idea is that we compile code for @(y+z)@ in an environment in which
267 @z@ is bound to an offset from \tr{Node}, and @y@ is bound to an
268 offset from the stack pointer.
270 (A let-closure is an @StgLet@ with a @StgRhsClosure@ RHS.)
274 let-constructor x = Constructor [args]
278 (A let-constructor is an @StgLet@ with a @StgRhsCon@ RHS.)
281 Letrec-expressions are essentially the same deal as
282 let-closure/let-constructor, so we use a common structure and
283 distinguish between them with an @is_recursive@ boolean flag.
287 let-unboxed u = an arbitrary arithmetic expression in unboxed values
290 All the stuff on the RHS must be fully evaluated. No function calls either!
292 (We've backed away from this toward case-expressions with
293 suitably-magical alts ...)
296 ~[Advanced stuff here! Not to start with, but makes pattern matching
297 generate more efficient code.]
300 let-escapes-not fail = expr
303 Here the idea is that @e'@ guarantees not to put @fail@ in a data structure,
304 or pass it to another function. All @e'@ will ever do is tail-call @fail@.
305 Rather than build a closure for @fail@, all we need do is to record the stack
306 level at the moment of the @let-escapes-not@; then entering @fail@ is just
307 a matter of adjusting the stack pointer back down to that point and entering
312 f x y = let z = huge-expression in
318 (A let-escapes-not is an @StgLetNoEscape@.)
321 We may eventually want:
323 let-literal x = Literal
327 (ToDo: is this obsolete?)
330 And so the code for let(rec)-things:
333 (GenStgBinding bndr occ) -- right hand sides (see below)
334 (GenStgExpr bndr occ) -- body
336 | StgLetNoEscape -- remember: ``advanced stuff''
337 (GenStgLiveVars occ) -- Live in the whole let-expression
338 -- Mustn't overwrite these stack slots
339 -- *Doesn't* include binders of the let(rec).
341 (GenStgLiveVars occ) -- Live in the right hand sides (only)
342 -- These are the ones which must be saved on
343 -- the stack if they aren't there already
344 -- *Does* include binders of the let(rec) if recursive.
346 (GenStgBinding bndr occ) -- right hand sides (see below)
347 (GenStgExpr bndr occ) -- body
350 %************************************************************************
352 \subsubsection{@GenStgExpr@: @scc@ expressions}
354 %************************************************************************
356 Finally for @scc@ expressions we introduce a new STG construct.
360 CostCentre -- label of SCC expression
361 (GenStgExpr bndr occ) -- scc expression
364 %************************************************************************
366 \subsubsection{@GenStgExpr@: @hpc@ expressions}
368 %************************************************************************
370 Finally for @scc@ expressions we introduce a new STG construct.
374 Module -- the module of the source of this tick
376 (GenStgExpr bndr occ) -- sub expression
380 %************************************************************************
382 \subsection{STG right-hand sides}
384 %************************************************************************
386 Here's the rest of the interesting stuff for @StgLet@s; the first
387 flavour is for closures:
389 data GenStgRhs bndr occ
391 CostCentreStack -- CCS to be attached (default is CurrentCCS)
392 StgBinderInfo -- Info about how this binder is used (see below)
393 [occ] -- non-global free vars; a list, rather than
394 -- a set, because order is important
395 !UpdateFlag -- ReEntrant | Updatable | SingleEntry
396 SRT -- The SRT reference
397 [bndr] -- arguments; if empty, then not a function;
398 -- as above, order is important.
399 (GenStgExpr bndr occ) -- body
401 An example may be in order. Consider:
403 let t = \x -> \y -> ... x ... y ... p ... q in e
405 Pulling out the free vars and stylising somewhat, we get the equivalent:
407 let t = (\[p,q] -> \[x,y] -> ... x ... y ... p ...q) p q
409 Stg-operationally, the @[x,y]@ are on the stack, the @[p,q]@ are
410 offsets from @Node@ into the closure, and the code ptr for the closure
411 will be exactly that in parentheses above.
413 The second flavour of right-hand-side is for constructors (simple but important):
416 CostCentreStack -- CCS to be attached (default is CurrentCCS).
417 -- Top-level (static) ones will end up with
418 -- DontCareCCS, because we don't count static
419 -- data in heap profiles, and we don't set CCCS
420 -- from static closure.
421 DataCon -- constructor
422 [GenStgArg occ] -- args
426 stgRhsArity :: StgRhs -> Int
427 stgRhsArity (StgRhsClosure _ _ _ _ _ bndrs _) = count isId bndrs
428 -- The arity never includes type parameters, so
429 -- when keeping type arguments and binders in the Stg syntax
430 -- (opt_RuntimeTypes) we have to fliter out the type binders.
431 stgRhsArity (StgRhsCon _ _ _) = 0
435 stgBindHasCafRefs :: GenStgBinding bndr Id -> Bool
436 stgBindHasCafRefs (StgNonRec _ rhs) = rhsHasCafRefs rhs
437 stgBindHasCafRefs (StgRec binds) = any rhsHasCafRefs (map snd binds)
439 rhsHasCafRefs (StgRhsClosure _ _ _ upd srt _ _)
440 = isUpdatable upd || nonEmptySRT srt
441 rhsHasCafRefs (StgRhsCon _ _ args)
442 = any stgArgHasCafRefs args
444 stgArgHasCafRefs (StgVarArg id) = mayHaveCafRefs (idCafInfo id)
445 stgArgHasCafRefs _ = False
448 Here's the @StgBinderInfo@ type, and its combining op:
452 | SatCallsOnly -- All occurrences are *saturated* *function* calls
453 -- This means we don't need to build an info table and
454 -- slow entry code for the thing
455 -- Thunks never get this value
457 noBinderInfo = NoStgBinderInfo
458 stgUnsatOcc = NoStgBinderInfo
459 stgSatOcc = SatCallsOnly
461 satCallsOnly :: StgBinderInfo -> Bool
462 satCallsOnly SatCallsOnly = True
463 satCallsOnly NoStgBinderInfo = False
465 combineStgBinderInfo :: StgBinderInfo -> StgBinderInfo -> StgBinderInfo
466 combineStgBinderInfo SatCallsOnly SatCallsOnly = SatCallsOnly
467 combineStgBinderInfo info1 info2 = NoStgBinderInfo
470 pp_binder_info NoStgBinderInfo = empty
471 pp_binder_info SatCallsOnly = ptext SLIT("sat-only")
474 %************************************************************************
476 \subsection[Stg-case-alternatives]{STG case alternatives}
478 %************************************************************************
480 Very like in @CoreSyntax@ (except no type-world stuff).
482 The type constructor is guaranteed not to be abstract; that is, we can
483 see its representation. This is important because the code generator
484 uses it to determine return conventions etc. But it's not trivial
485 where there's a moduule loop involved, because some versions of a type
486 constructor might not have all the constructors visible. So
487 mkStgAlgAlts (in CoreToStg) ensures that it gets the TyCon from the
488 constructors or literals (which are guaranteed to have the Real McCoy)
489 rather than from the scrutinee type.
492 type GenStgAlt bndr occ
493 = (AltCon, -- alts: data constructor,
494 [bndr], -- constructor's parameters,
495 [Bool], -- "use mask", same length as
496 -- parameters; a True in a
497 -- param's position if it is
499 GenStgExpr bndr occ) -- ...right-hand side.
502 = PolyAlt -- Polymorphic (a type variable)
503 | UbxTupAlt TyCon -- Unboxed tuple
504 | AlgAlt TyCon -- Algebraic data type; the AltCons will be DataAlts
505 | PrimAlt TyCon -- Primitive data type; the AltCons will be LitAlts
508 %************************************************************************
510 \subsection[Stg]{The Plain STG parameterisation}
512 %************************************************************************
514 This happens to be the only one we use at the moment.
517 type StgBinding = GenStgBinding Id Id
518 type StgArg = GenStgArg Id
519 type StgLiveVars = GenStgLiveVars Id
520 type StgExpr = GenStgExpr Id Id
521 type StgRhs = GenStgRhs Id Id
522 type StgAlt = GenStgAlt Id Id
525 %************************************************************************
527 \subsubsection[UpdateFlag-datatype]{@UpdateFlag@}
529 %************************************************************************
531 This is also used in @LambdaFormInfo@ in the @ClosureInfo@ module.
533 A @ReEntrant@ closure may be entered multiple times, but should not be
534 updated or blackholed. An @Updatable@ closure should be updated after
535 evaluation (and may be blackholed during evaluation). A @SingleEntry@
536 closure will only be entered once, and so need not be updated but may
537 safely be blackholed.
540 data UpdateFlag = ReEntrant | Updatable | SingleEntry
542 instance Outputable UpdateFlag where
544 = char (case u of { ReEntrant -> 'r'; Updatable -> 'u'; SingleEntry -> 's' })
546 isUpdatable ReEntrant = False
547 isUpdatable SingleEntry = False
548 isUpdatable Updatable = True
551 %************************************************************************
553 \subsubsection{StgOp}
555 %************************************************************************
557 An StgOp allows us to group together PrimOps and ForeignCalls.
558 It's quite useful to move these around together, notably
559 in StgOpApp and COpStmt.
562 data StgOp = StgPrimOp PrimOp
564 | StgFCallOp ForeignCall Unique
565 -- The Unique is occasionally needed by the C pretty-printer
566 -- (which lacks a unique supply), notably when generating a
567 -- typedef for foreign-export-dynamic
571 %************************************************************************
573 \subsubsection[Static Reference Tables]{@SRT@}
575 %************************************************************************
577 There is one SRT per top-level function group. Each local binding and
578 case expression within this binding group has a subrange of the whole
579 SRT, expressed as an offset and length.
581 In CoreToStg we collect the list of CafRefs at each SRT site, which is later
582 converted into the length and offset form by the SRT pass.
587 -- generated by CoreToStg
588 | SRT !Int{-offset-} !Int{-length-} !Bitmap{-bitmap-}
589 -- generated by computeSRTs
594 nonEmptySRT NoSRT = False
595 nonEmptySRT (SRTEntries vs) = not (isEmptyVarSet vs)
598 pprSRT (NoSRT) = ptext SLIT("_no_srt_")
599 pprSRT (SRTEntries ids) = text "SRT:" <> ppr ids
600 pprSRT (SRT off length bitmap) = parens (ppr off <> comma <> text "*bitmap*")
603 %************************************************************************
605 \subsection[Stg-pretty-printing]{Pretty-printing}
607 %************************************************************************
609 Robin Popplestone asked for semi-colon separators on STG binds; here's
610 hoping he likes terminators instead... Ditto for case alternatives.
613 pprGenStgBinding :: (Outputable bndr, Outputable bdee, Ord bdee)
614 => GenStgBinding bndr bdee -> SDoc
616 pprGenStgBinding (StgNonRec bndr rhs)
617 = hang (hsep [ppr bndr, equals])
618 4 ((<>) (ppr rhs) semi)
620 pprGenStgBinding (StgRec pairs)
621 = vcat ((ifPprDebug (ptext SLIT("{- StgRec (begin) -}"))) :
622 (map (ppr_bind) pairs) ++ [(ifPprDebug (ptext SLIT("{- StgRec (end) -}")))])
624 ppr_bind (bndr, expr)
625 = hang (hsep [ppr bndr, equals])
626 4 ((<>) (ppr expr) semi)
628 pprStgBinding :: StgBinding -> SDoc
629 pprStgBinding bind = pprGenStgBinding bind
631 pprStgBindings :: [StgBinding] -> SDoc
632 pprStgBindings binds = vcat (map pprGenStgBinding binds)
634 pprGenStgBindingWithSRT
635 :: (Outputable bndr, Outputable bdee, Ord bdee)
636 => (GenStgBinding bndr bdee,[(Id,[Id])]) -> SDoc
638 pprGenStgBindingWithSRT (bind,srts)
639 = vcat (pprGenStgBinding bind : map pprSRT srts)
640 where pprSRT (id,srt) =
641 ptext SLIT("SRT") <> parens (ppr id) <> ptext SLIT(": ") <> ppr srt
643 pprStgBindingsWithSRTs :: [(StgBinding,[(Id,[Id])])] -> SDoc
644 pprStgBindingsWithSRTs binds = vcat (map pprGenStgBindingWithSRT binds)
648 instance (Outputable bdee) => Outputable (GenStgArg bdee) where
651 instance (Outputable bndr, Outputable bdee, Ord bdee)
652 => Outputable (GenStgBinding bndr bdee) where
653 ppr = pprGenStgBinding
655 instance (Outputable bndr, Outputable bdee, Ord bdee)
656 => Outputable (GenStgExpr bndr bdee) where
659 instance (Outputable bndr, Outputable bdee, Ord bdee)
660 => Outputable (GenStgRhs bndr bdee) where
661 ppr rhs = pprStgRhs rhs
665 pprStgArg :: (Outputable bdee) => GenStgArg bdee -> SDoc
667 pprStgArg (StgVarArg var) = ppr var
668 pprStgArg (StgLitArg con) = ppr con
669 pprStgArg (StgTypeArg ty) = char '@' <+> ppr ty
673 pprStgExpr :: (Outputable bndr, Outputable bdee, Ord bdee)
674 => GenStgExpr bndr bdee -> SDoc
676 pprStgExpr (StgLit lit) = ppr lit
679 pprStgExpr (StgApp func args)
681 4 (sep (map (ppr) args))
685 pprStgExpr (StgConApp con args)
686 = hsep [ ppr con, brackets (interppSP args)]
688 pprStgExpr (StgOpApp op args _)
689 = hsep [ pprStgOp op, brackets (interppSP args)]
691 pprStgExpr (StgLam _ bndrs body)
692 =sep [ char '\\' <+> ppr bndrs <+> ptext SLIT("->"),
697 -- special case: let v = <very specific thing>
703 -- Very special! Suspicious! (SLPJ)
706 pprStgExpr (StgLet srt (StgNonRec bndr (StgRhsClosure cc bi free_vars upd_flag args rhs))
709 (hang (hcat [ptext SLIT("let { "), ppr bndr, ptext SLIT(" = "),
712 ptext SLIT(" ["), ifPprDebug (interppSP free_vars), ptext SLIT("] \\"),
713 ppr upd_flag, ptext SLIT(" ["),
714 interppSP args, char ']'])
715 8 (sep [hsep [ppr rhs, ptext SLIT("} in")]]))
719 -- special case: let ... in let ...
721 pprStgExpr (StgLet bind expr@(StgLet _ _))
723 (sep [hang (ptext SLIT("let {"))
724 2 (hsep [pprGenStgBinding bind, ptext SLIT("} in")])])
728 pprStgExpr (StgLet bind expr)
729 = sep [hang (ptext SLIT("let {")) 2 (pprGenStgBinding bind),
730 hang (ptext SLIT("} in ")) 2 (ppr expr)]
732 pprStgExpr (StgLetNoEscape lvs_whole lvs_rhss bind expr)
733 = sep [hang (ptext SLIT("let-no-escape {"))
734 2 (pprGenStgBinding bind),
735 hang ((<>) (ptext SLIT("} in "))
738 hcat [ptext SLIT("-- lvs: ["), interppSP (uniqSetToList lvs_whole),
739 ptext SLIT("]; rhs lvs: ["), interppSP (uniqSetToList lvs_rhss),
743 pprStgExpr (StgSCC cc expr)
744 = sep [ hsep [ptext SLIT("_scc_"), ppr cc],
747 pprStgExpr (StgTick m n expr)
748 = sep [ hsep [ptext SLIT("_tick_"), pprModule m,text (show n)],
751 pprStgExpr (StgCase expr lvs_whole lvs_rhss bndr srt alt_type alts)
752 = sep [sep [ptext SLIT("case"),
753 nest 4 (hsep [pprStgExpr expr,
754 ifPprDebug (dcolon <+> ppr alt_type)]),
755 ptext SLIT("of"), ppr bndr, char '{'],
758 hcat [ptext SLIT("-- lvs: ["), interppSP (uniqSetToList lvs_whole),
759 ptext SLIT("]; rhs lvs: ["), interppSP (uniqSetToList lvs_rhss),
762 nest 2 (vcat (map pprStgAlt alts)),
765 pprStgAlt (con, params, use_mask, expr)
766 = hang (hsep [ppr con, interppSP params, ptext SLIT("->")])
769 pprStgOp (StgPrimOp op) = ppr op
770 pprStgOp (StgFCallOp op _) = ppr op
772 instance Outputable AltType where
773 ppr PolyAlt = ptext SLIT("Polymorphic")
774 ppr (UbxTupAlt tc) = ptext SLIT("UbxTup") <+> ppr tc
775 ppr (AlgAlt tc) = ptext SLIT("Alg") <+> ppr tc
776 ppr (PrimAlt tc) = ptext SLIT("Prim") <+> ppr tc
780 pprStgLVs :: Outputable occ => GenStgLiveVars occ -> SDoc
782 = getPprStyle $ \ sty ->
783 if userStyle sty || isEmptyUniqSet lvs then
786 hcat [text "{-lvs:", interpp'SP (uniqSetToList lvs), text "-}"]
790 pprStgRhs :: (Outputable bndr, Outputable bdee, Ord bdee)
791 => GenStgRhs bndr bdee -> SDoc
794 pprStgRhs (StgRhsClosure cc bi [free_var] upd_flag srt [{-no args-}] (StgApp func []))
797 brackets (ifPprDebug (ppr free_var)),
798 ptext SLIT(" \\"), ppr upd_flag, pprMaybeSRT srt, ptext SLIT(" [] "), ppr func ]
801 pprStgRhs (StgRhsClosure cc bi free_vars upd_flag srt args body)
802 = hang (hsep [if opt_SccProfilingOn then ppr cc else empty,
804 ifPprDebug (brackets (interppSP free_vars)),
805 char '\\' <> ppr upd_flag, pprMaybeSRT srt, brackets (interppSP args)])
808 pprStgRhs (StgRhsCon cc con args)
810 space, ppr con, ptext SLIT("! "), brackets (interppSP args)]
812 pprMaybeSRT (NoSRT) = empty
813 pprMaybeSRT srt = ptext SLIT("srt:") <> pprSRT srt