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.
16 GenStgBinding(..), GenStgExpr(..), GenStgRhs(..),
17 GenStgAlt, AltType(..),
19 UpdateFlag(..), isUpdatable,
22 noBinderInfo, stgSatOcc, stgUnsatOcc, satCallsOnly,
25 -- a set of synonyms for the most common (only :-) parameterisation
27 StgBinding, StgExpr, StgRhs, StgAlt,
36 stgBindHasCafRefs, stgArgHasCafRefs, stgRhsArity,
37 isDllConApp, isStgTypeArg,
40 pprStgBinding, pprStgBindings, pprStgBindingsWithSRTs
47 #include "HsVersions.h"
49 import CostCentre ( CostCentreStack, CostCentre )
50 import VarSet ( IdSet, isEmptyVarSet )
51 import Id ( Id, idName, idType, idCafInfo, isId )
52 import IdInfo ( mayHaveCafRefs )
53 import Packages ( isDllName )
54 import Literal ( Literal, literalType )
55 import ForeignCall ( ForeignCall )
56 import DataCon ( DataCon, dataConName )
57 import CoreSyn ( AltCon )
58 import PprCore ( {- instances -} )
59 import PrimOp ( PrimOp )
62 import TyCon ( TyCon )
64 import Unique ( Unique )
66 import StaticFlags ( opt_SccProfilingOn )
71 %************************************************************************
73 \subsection{@GenStgBinding@}
75 %************************************************************************
77 As usual, expressions are interesting; other things are boring. Here
78 are the boring things [except note the @GenStgRhs@], parameterised
79 with respect to binder and occurrence information (just as in
82 There is one SRT for each group of bindings.
85 data GenStgBinding bndr occ
86 = StgNonRec bndr (GenStgRhs bndr occ)
87 | StgRec [(bndr, GenStgRhs bndr occ)]
90 %************************************************************************
92 \subsection{@GenStgArg@}
94 %************************************************************************
100 | StgTypeArg Type -- For when we want to preserve all type info
104 isStgTypeArg :: StgArg -> Bool
105 isStgTypeArg (StgTypeArg _) = True
106 isStgTypeArg _ = False
108 isDllArg :: PackageId -> StgArg -> Bool
109 -- Does this argument refer to something in a different DLL?
110 isDllArg this_pkg (StgVarArg v) = isDllName this_pkg (idName v)
113 isDllConApp :: PackageId -> DataCon -> [StgArg] -> Bool
114 -- Does this constructor application refer to
115 -- anything in a different DLL?
116 -- If so, we can't allocate it statically
117 isDllConApp this_pkg con args
118 = isDllName this_pkg (dataConName con) || any (isDllArg this_pkg) args
120 stgArgType :: StgArg -> Type
121 -- Very half baked becase we have lost the type arguments
122 stgArgType (StgVarArg v) = idType v
123 stgArgType (StgLitArg lit) = literalType lit
124 stgArgType (StgTypeArg _) = panic "stgArgType called on stgTypeArg"
127 %************************************************************************
129 \subsection{STG expressions}
131 %************************************************************************
133 The @GenStgExpr@ data type is parameterised on binder and occurrence
136 %************************************************************************
138 \subsubsection{@GenStgExpr@ application}
140 %************************************************************************
142 An application is of a function to a list of atoms [not expressions].
143 Operationally, we want to push the arguments on the stack and call the
144 function. (If the arguments were expressions, we would have to build
145 their closures first.)
147 There is no constructor for a lone variable; it would appear as
150 type GenStgLiveVars occ = UniqSet occ
152 data GenStgExpr bndr occ
155 [GenStgArg occ] -- arguments; may be empty
158 %************************************************************************
160 \subsubsection{@StgConApp@ and @StgPrimApp@---saturated applications}
162 %************************************************************************
164 There are a specialised forms of application, for
165 constructors, primitives, and literals.
169 -- StgConApp is vital for returning unboxed tuples
170 -- which can't be let-bound first
172 [GenStgArg occ] -- Saturated
174 | StgOpApp StgOp -- Primitive op or foreign call
175 [GenStgArg occ] -- Saturated
177 -- We need to know this so that we can
178 -- assign result registers
181 %************************************************************************
183 \subsubsection{@StgLam@}
185 %************************************************************************
187 StgLam is used *only* during CoreToStg's work. Before CoreToStg has finished
188 it encodes (\x -> e) as (let f = \x -> e in f)
192 Type -- Type of whole lambda (useful when making a binder for it)
194 StgExpr -- Body of lambda
198 %************************************************************************
200 \subsubsection{@GenStgExpr@: case-expressions}
202 %************************************************************************
204 This has the same boxed/unboxed business as Core case expressions.
207 (GenStgExpr bndr occ)
208 -- the thing to examine
210 (GenStgLiveVars occ) -- Live vars of whole case expression,
211 -- plus everything that happens after the case
212 -- i.e., those which mustn't be overwritten
214 (GenStgLiveVars occ) -- Live vars of RHSs (plus what happens afterwards)
215 -- i.e., those which must be saved before eval.
217 -- note that an alt's constructor's
218 -- binder-variables are NOT counted in the
219 -- free vars for the alt's RHS
221 bndr -- binds the result of evaluating the scrutinee
223 SRT -- The SRT for the continuation
227 [GenStgAlt bndr occ] -- The DEFAULT case is always *first*
228 -- if it is there at all
231 %************************************************************************
233 \subsubsection{@GenStgExpr@: @let(rec)@-expressions}
235 %************************************************************************
237 The various forms of let(rec)-expression encode most of the
238 interesting things we want to do.
242 let-closure x = [free-vars] expr [args]
247 let x = (\free-vars -> \args -> expr) free-vars
249 \tr{args} may be empty (and is for most closures). It isn't under
250 circumstances like this:
256 let-closure x = [z] [y] (y+z)
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.
262 (A let-closure is an @StgLet@ with a @StgRhsClosure@ RHS.)
266 let-constructor x = Constructor [args]
270 (A let-constructor is an @StgLet@ with a @StgRhsCon@ RHS.)
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.
279 let-unboxed u = an arbitrary arithmetic expression in unboxed values
282 All the stuff on the RHS must be fully evaluated. No function calls either!
284 (We've backed away from this toward case-expressions with
285 suitably-magical alts ...)
288 ~[Advanced stuff here! Not to start with, but makes pattern matching
289 generate more efficient code.]
292 let-escapes-not fail = expr
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
304 f x y = let z = huge-expression in
310 (A let-escapes-not is an @StgLetNoEscape@.)
313 We may eventually want:
315 let-literal x = Literal
319 (ToDo: is this obsolete?)
322 And so the code for let(rec)-things:
325 (GenStgBinding bndr occ) -- right hand sides (see below)
326 (GenStgExpr bndr occ) -- body
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).
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.
338 (GenStgBinding bndr occ) -- right hand sides (see below)
339 (GenStgExpr bndr occ) -- body
342 %************************************************************************
344 \subsubsection{@GenStgExpr@: @scc@ expressions}
346 %************************************************************************
348 Finally for @scc@ expressions we introduce a new STG construct.
352 CostCentre -- label of SCC expression
353 (GenStgExpr bndr occ) -- scc expression
356 %************************************************************************
358 \subsubsection{@GenStgExpr@: @hpc@ expressions}
360 %************************************************************************
362 Finally for @scc@ expressions we introduce a new STG construct.
366 Module -- the module of the source of this tick
368 (GenStgExpr bndr occ) -- sub expression
372 %************************************************************************
374 \subsection{STG right-hand sides}
376 %************************************************************************
378 Here's the rest of the interesting stuff for @StgLet@s; the first
379 flavour is for closures:
381 data GenStgRhs bndr occ
383 CostCentreStack -- CCS to be attached (default is CurrentCCS)
384 StgBinderInfo -- Info about how this binder is used (see below)
385 [occ] -- non-global free vars; a list, rather than
386 -- a set, because order is important
387 !UpdateFlag -- ReEntrant | Updatable | SingleEntry
388 SRT -- The SRT reference
389 [bndr] -- arguments; if empty, then not a function;
390 -- as above, order is important.
391 (GenStgExpr bndr occ) -- body
393 An example may be in order. Consider:
395 let t = \x -> \y -> ... x ... y ... p ... q in e
397 Pulling out the free vars and stylising somewhat, we get the equivalent:
399 let t = (\[p,q] -> \[x,y] -> ... x ... y ... p ...q) p q
401 Stg-operationally, the @[x,y]@ are on the stack, the @[p,q]@ are
402 offsets from @Node@ into the closure, and the code ptr for the closure
403 will be exactly that in parentheses above.
405 The second flavour of right-hand-side is for constructors (simple but important):
408 CostCentreStack -- CCS to be attached (default is CurrentCCS).
409 -- Top-level (static) ones will end up with
410 -- DontCareCCS, because we don't count static
411 -- data in heap profiles, and we don't set CCCS
412 -- from static closure.
413 DataCon -- constructor
414 [GenStgArg occ] -- args
418 stgRhsArity :: StgRhs -> Int
419 stgRhsArity (StgRhsClosure _ _ _ _ _ bndrs _)
420 = ASSERT( all isId bndrs ) length bndrs
421 -- The arity never includes type parameters, but they should have gone by now
422 stgRhsArity (StgRhsCon _ _ _) = 0
426 stgBindHasCafRefs :: GenStgBinding bndr Id -> Bool
427 stgBindHasCafRefs (StgNonRec _ rhs) = rhsHasCafRefs rhs
428 stgBindHasCafRefs (StgRec binds) = any rhsHasCafRefs (map snd binds)
430 rhsHasCafRefs :: GenStgRhs bndr Id -> Bool
431 rhsHasCafRefs (StgRhsClosure _ _ _ upd srt _ _)
432 = isUpdatable upd || nonEmptySRT srt
433 rhsHasCafRefs (StgRhsCon _ _ args)
434 = any stgArgHasCafRefs args
436 stgArgHasCafRefs :: GenStgArg Id -> Bool
437 stgArgHasCafRefs (StgVarArg id) = mayHaveCafRefs (idCafInfo id)
438 stgArgHasCafRefs _ = False
441 Here's the @StgBinderInfo@ type, and its combining op:
445 | SatCallsOnly -- All occurrences are *saturated* *function* calls
446 -- This means we don't need to build an info table and
447 -- slow entry code for the thing
448 -- Thunks never get this value
450 noBinderInfo, stgUnsatOcc, stgSatOcc :: StgBinderInfo
451 noBinderInfo = NoStgBinderInfo
452 stgUnsatOcc = NoStgBinderInfo
453 stgSatOcc = SatCallsOnly
455 satCallsOnly :: StgBinderInfo -> Bool
456 satCallsOnly SatCallsOnly = True
457 satCallsOnly NoStgBinderInfo = False
459 combineStgBinderInfo :: StgBinderInfo -> StgBinderInfo -> StgBinderInfo
460 combineStgBinderInfo SatCallsOnly SatCallsOnly = SatCallsOnly
461 combineStgBinderInfo _ _ = NoStgBinderInfo
464 pp_binder_info :: StgBinderInfo -> SDoc
465 pp_binder_info NoStgBinderInfo = empty
466 pp_binder_info SatCallsOnly = ptext (sLit "sat-only")
469 %************************************************************************
471 \subsection[Stg-case-alternatives]{STG case alternatives}
473 %************************************************************************
475 Very like in @CoreSyntax@ (except no type-world stuff).
477 The type constructor is guaranteed not to be abstract; that is, we can
478 see its representation. This is important because the code generator
479 uses it to determine return conventions etc. But it's not trivial
480 where there's a moduule loop involved, because some versions of a type
481 constructor might not have all the constructors visible. So
482 mkStgAlgAlts (in CoreToStg) ensures that it gets the TyCon from the
483 constructors or literals (which are guaranteed to have the Real McCoy)
484 rather than from the scrutinee type.
487 type GenStgAlt bndr occ
488 = (AltCon, -- alts: data constructor,
489 [bndr], -- constructor's parameters,
490 [Bool], -- "use mask", same length as
491 -- parameters; a True in a
492 -- param's position if it is
494 GenStgExpr bndr occ) -- ...right-hand side.
497 = PolyAlt -- Polymorphic (a type variable)
498 | UbxTupAlt TyCon -- Unboxed tuple
499 | AlgAlt TyCon -- Algebraic data type; the AltCons will be DataAlts
500 | PrimAlt TyCon -- Primitive data type; the AltCons will be LitAlts
503 %************************************************************************
505 \subsection[Stg]{The Plain STG parameterisation}
507 %************************************************************************
509 This happens to be the only one we use at the moment.
512 type StgBinding = GenStgBinding Id Id
513 type StgArg = GenStgArg Id
514 type StgLiveVars = GenStgLiveVars Id
515 type StgExpr = GenStgExpr Id Id
516 type StgRhs = GenStgRhs Id Id
517 type StgAlt = GenStgAlt Id Id
520 %************************************************************************
522 \subsubsection[UpdateFlag-datatype]{@UpdateFlag@}
524 %************************************************************************
526 This is also used in @LambdaFormInfo@ in the @ClosureInfo@ module.
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.
535 data UpdateFlag = ReEntrant | Updatable | SingleEntry
537 instance Outputable UpdateFlag where
539 = char (case u of { ReEntrant -> 'r'; Updatable -> 'u'; SingleEntry -> 's' })
541 isUpdatable :: UpdateFlag -> Bool
542 isUpdatable ReEntrant = False
543 isUpdatable SingleEntry = False
544 isUpdatable Updatable = True
547 %************************************************************************
549 \subsubsection{StgOp}
551 %************************************************************************
553 An StgOp allows us to group together PrimOps and ForeignCalls.
554 It's quite useful to move these around together, notably
555 in StgOpApp and COpStmt.
558 data StgOp = StgPrimOp PrimOp
560 | StgFCallOp ForeignCall Unique
561 -- The Unique is occasionally needed by the C pretty-printer
562 -- (which lacks a unique supply), notably when generating a
563 -- typedef for foreign-export-dynamic
567 %************************************************************************
569 \subsubsection[Static Reference Tables]{@SRT@}
571 %************************************************************************
573 There is one SRT per top-level function group. Each local binding and
574 case expression within this binding group has a subrange of the whole
575 SRT, expressed as an offset and length.
577 In CoreToStg we collect the list of CafRefs at each SRT site, which is later
578 converted into the length and offset form by the SRT pass.
583 -- generated by CoreToStg
584 | SRT !Int{-offset-} !Int{-length-} !Bitmap{-bitmap-}
585 -- generated by computeSRTs
587 nonEmptySRT :: SRT -> Bool
588 nonEmptySRT NoSRT = False
589 nonEmptySRT (SRTEntries vs) = not (isEmptyVarSet vs)
592 pprSRT :: SRT -> SDoc
593 pprSRT (NoSRT) = ptext (sLit "_no_srt_")
594 pprSRT (SRTEntries ids) = text "SRT:" <> ppr ids
595 pprSRT (SRT off _ _) = parens (ppr off <> comma <> text "*bitmap*")
598 %************************************************************************
600 \subsection[Stg-pretty-printing]{Pretty-printing}
602 %************************************************************************
604 Robin Popplestone asked for semi-colon separators on STG binds; here's
605 hoping he likes terminators instead... Ditto for case alternatives.
608 pprGenStgBinding :: (Outputable bndr, Outputable bdee, Ord bdee)
609 => GenStgBinding bndr bdee -> SDoc
611 pprGenStgBinding (StgNonRec bndr rhs)
612 = hang (hsep [ppr bndr, equals])
613 4 ((<>) (ppr rhs) semi)
615 pprGenStgBinding (StgRec pairs)
616 = vcat ((ifPprDebug (ptext (sLit "{- StgRec (begin) -}"))) :
617 (map (ppr_bind) pairs) ++ [(ifPprDebug (ptext (sLit "{- StgRec (end) -}")))])
619 ppr_bind (bndr, expr)
620 = hang (hsep [ppr bndr, equals])
621 4 ((<>) (ppr expr) semi)
623 pprStgBinding :: StgBinding -> SDoc
624 pprStgBinding bind = pprGenStgBinding bind
626 pprStgBindings :: [StgBinding] -> SDoc
627 pprStgBindings binds = vcat (map pprGenStgBinding binds)
629 pprGenStgBindingWithSRT
630 :: (Outputable bndr, Outputable bdee, Ord bdee)
631 => (GenStgBinding bndr bdee,[(Id,[Id])]) -> SDoc
633 pprGenStgBindingWithSRT (bind,srts)
634 = vcat (pprGenStgBinding bind : map pprSRT srts)
635 where pprSRT (id,srt) =
636 ptext (sLit "SRT") <> parens (ppr id) <> ptext (sLit ": ") <> ppr srt
638 pprStgBindingsWithSRTs :: [(StgBinding,[(Id,[Id])])] -> SDoc
639 pprStgBindingsWithSRTs binds = vcat (map pprGenStgBindingWithSRT binds)
643 instance (Outputable bdee) => Outputable (GenStgArg bdee) where
646 instance (Outputable bndr, Outputable bdee, Ord bdee)
647 => Outputable (GenStgBinding bndr bdee) where
648 ppr = pprGenStgBinding
650 instance (Outputable bndr, Outputable bdee, Ord bdee)
651 => Outputable (GenStgExpr bndr bdee) where
654 instance (Outputable bndr, Outputable bdee, Ord bdee)
655 => Outputable (GenStgRhs bndr bdee) where
656 ppr rhs = pprStgRhs rhs
660 pprStgArg :: (Outputable bdee) => GenStgArg bdee -> SDoc
662 pprStgArg (StgVarArg var) = ppr var
663 pprStgArg (StgLitArg con) = ppr con
664 pprStgArg (StgTypeArg ty) = char '@' <+> ppr ty
668 pprStgExpr :: (Outputable bndr, Outputable bdee, Ord bdee)
669 => GenStgExpr bndr bdee -> SDoc
671 pprStgExpr (StgLit lit) = ppr lit
674 pprStgExpr (StgApp func args)
676 4 (sep (map (ppr) args))
680 pprStgExpr (StgConApp con args)
681 = hsep [ ppr con, brackets (interppSP args)]
683 pprStgExpr (StgOpApp op args _)
684 = hsep [ pprStgOp op, brackets (interppSP args)]
686 pprStgExpr (StgLam _ bndrs body)
687 =sep [ char '\\' <+> ppr bndrs <+> ptext (sLit "->"),
692 -- special case: let v = <very specific thing>
698 -- Very special! Suspicious! (SLPJ)
701 pprStgExpr (StgLet srt (StgNonRec bndr (StgRhsClosure cc bi free_vars upd_flag args rhs))
704 (hang (hcat [ptext (sLit "let { "), ppr bndr, ptext (sLit " = "),
707 ptext (sLit " ["), ifPprDebug (interppSP free_vars), ptext (sLit "] \\"),
708 ppr upd_flag, ptext (sLit " ["),
709 interppSP args, char ']'])
710 8 (sep [hsep [ppr rhs, ptext (sLit "} in")]]))
714 -- special case: let ... in let ...
716 pprStgExpr (StgLet bind expr@(StgLet _ _))
718 (sep [hang (ptext (sLit "let {"))
719 2 (hsep [pprGenStgBinding bind, ptext (sLit "} in")])])
723 pprStgExpr (StgLet bind expr)
724 = sep [hang (ptext (sLit "let {")) 2 (pprGenStgBinding bind),
725 hang (ptext (sLit "} in ")) 2 (ppr expr)]
727 pprStgExpr (StgLetNoEscape lvs_whole lvs_rhss bind expr)
728 = sep [hang (ptext (sLit "let-no-escape {"))
729 2 (pprGenStgBinding bind),
730 hang ((<>) (ptext (sLit "} in "))
733 hcat [ptext (sLit "-- lvs: ["), interppSP (uniqSetToList lvs_whole),
734 ptext (sLit "]; rhs lvs: ["), interppSP (uniqSetToList lvs_rhss),
738 pprStgExpr (StgSCC cc expr)
739 = sep [ hsep [ptext (sLit "_scc_"), ppr cc],
742 pprStgExpr (StgTick m n expr)
743 = sep [ hsep [ptext (sLit "_tick_"), pprModule m,text (show n)],
746 pprStgExpr (StgCase expr lvs_whole lvs_rhss bndr srt alt_type alts)
747 = sep [sep [ptext (sLit "case"),
748 nest 4 (hsep [pprStgExpr expr,
749 ifPprDebug (dcolon <+> ppr alt_type)]),
750 ptext (sLit "of"), ppr bndr, char '{'],
753 hcat [ptext (sLit "-- lvs: ["), interppSP (uniqSetToList lvs_whole),
754 ptext (sLit "]; rhs lvs: ["), interppSP (uniqSetToList lvs_rhss),
757 nest 2 (vcat (map pprStgAlt alts)),
760 pprStgAlt :: (Outputable bndr, Outputable occ, Ord occ)
761 => GenStgAlt bndr occ -> SDoc
762 pprStgAlt (con, params, _use_mask, expr)
763 = hang (hsep [ppr con, interppSP params, ptext (sLit "->")])
766 pprStgOp :: StgOp -> SDoc
767 pprStgOp (StgPrimOp op) = ppr op
768 pprStgOp (StgFCallOp op _) = ppr op
770 instance Outputable AltType where
771 ppr PolyAlt = ptext (sLit "Polymorphic")
772 ppr (UbxTupAlt tc) = ptext (sLit "UbxTup") <+> ppr tc
773 ppr (AlgAlt tc) = ptext (sLit "Alg") <+> ppr tc
774 ppr (PrimAlt tc) = ptext (sLit "Prim") <+> ppr tc
779 pprStgLVs :: Outputable occ => GenStgLiveVars occ -> SDoc
781 = getPprStyle $ \ sty ->
782 if userStyle sty || isEmptyUniqSet lvs then
785 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 :: SRT -> SDoc
813 pprMaybeSRT (NoSRT) = empty
814 pprMaybeSRT srt = ptext (sLit "srt:") <> pprSRT srt