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 )
52 import Id ( Id, idName, idType, idCafInfo )
53 import IdInfo ( mayHaveCafRefs )
54 import Packages ( isDllName )
55 import Literal ( Literal, literalType )
56 import ForeignCall ( ForeignCall )
57 import DataCon ( DataCon, dataConName )
58 import CoreSyn ( AltCon )
59 import PprCore ( {- instances -} )
60 import PrimOp ( PrimOp )
64 import TyCon ( TyCon )
66 import Unique ( Unique )
68 import StaticFlags ( opt_SccProfilingOn )
72 %************************************************************************
74 \subsection{@GenStgBinding@}
76 %************************************************************************
78 As usual, expressions are interesting; other things are boring. Here
79 are the boring things [except note the @GenStgRhs@], parameterised
80 with respect to binder and occurrence information (just as in
83 There is one SRT for each group of bindings.
86 data GenStgBinding bndr occ
87 = StgNonRec bndr (GenStgRhs bndr occ)
88 | StgRec [(bndr, GenStgRhs bndr occ)]
91 %************************************************************************
93 \subsection{@GenStgArg@}
95 %************************************************************************
101 | StgTypeArg Type -- For when we want to preserve all type info
105 isStgTypeArg :: StgArg -> Bool
106 isStgTypeArg (StgTypeArg _) = True
107 isStgTypeArg _ = False
109 isDllArg :: PackageId -> StgArg -> Bool
110 -- Does this argument refer to something in a different DLL?
111 isDllArg this_pkg (StgVarArg v) = isDllName this_pkg (idName v)
114 isDllConApp :: PackageId -> DataCon -> [StgArg] -> Bool
115 -- Does this constructor application refer to
116 -- anything in a different DLL?
117 -- If so, we can't allocate it statically
118 isDllConApp this_pkg con args
119 = isDllName this_pkg (dataConName con) || any (isDllArg this_pkg) args
121 stgArgType :: StgArg -> Type
122 -- Very half baked becase we have lost the type arguments
123 stgArgType (StgVarArg v) = idType v
124 stgArgType (StgLitArg lit) = literalType lit
125 stgArgType (StgTypeArg _) = panic "stgArgType called on stgTypeArg"
128 %************************************************************************
130 \subsection{STG expressions}
132 %************************************************************************
134 The @GenStgExpr@ data type is parameterised on binder and occurrence
137 %************************************************************************
139 \subsubsection{@GenStgExpr@ application}
141 %************************************************************************
143 An application is of a function to a list of atoms [not expressions].
144 Operationally, we want to push the arguments on the stack and call the
145 function. (If the arguments were expressions, we would have to build
146 their closures first.)
148 There is no constructor for a lone variable; it would appear as
151 type GenStgLiveVars occ = UniqSet occ
153 data GenStgExpr bndr occ
156 [GenStgArg occ] -- arguments; may be empty
159 %************************************************************************
161 \subsubsection{@StgConApp@ and @StgPrimApp@---saturated applications}
163 %************************************************************************
165 There are a specialised forms of application, for
166 constructors, primitives, and literals.
170 -- StgConApp is vital for returning unboxed tuples
171 -- which can't be let-bound first
173 [GenStgArg occ] -- Saturated
175 | StgOpApp StgOp -- Primitive op or foreign call
176 [GenStgArg occ] -- Saturated
178 -- We need to know this so that we can
179 -- assign result registers
182 %************************************************************************
184 \subsubsection{@StgLam@}
186 %************************************************************************
188 StgLam is used *only* during CoreToStg's work. Before CoreToStg has finished
189 it encodes (\x -> e) as (let f = \x -> e in f)
193 Type -- Type of whole lambda (useful when making a binder for it)
195 StgExpr -- Body of lambda
199 %************************************************************************
201 \subsubsection{@GenStgExpr@: case-expressions}
203 %************************************************************************
205 This has the same boxed/unboxed business as Core case expressions.
208 (GenStgExpr bndr occ)
209 -- the thing to examine
211 (GenStgLiveVars occ) -- Live vars of whole case expression,
212 -- plus everything that happens after the case
213 -- i.e., those which mustn't be overwritten
215 (GenStgLiveVars occ) -- Live vars of RHSs (plus what happens afterwards)
216 -- i.e., those which must be saved before eval.
218 -- note that an alt's constructor's
219 -- binder-variables are NOT counted in the
220 -- free vars for the alt's RHS
222 bndr -- binds the result of evaluating the scrutinee
224 SRT -- The SRT for the continuation
228 [GenStgAlt bndr occ] -- The DEFAULT case is always *first*
229 -- if it is there at all
232 %************************************************************************
234 \subsubsection{@GenStgExpr@: @let(rec)@-expressions}
236 %************************************************************************
238 The various forms of let(rec)-expression encode most of the
239 interesting things we want to do.
243 let-closure x = [free-vars] expr [args]
248 let x = (\free-vars -> \args -> expr) free-vars
250 \tr{args} may be empty (and is for most closures). It isn't under
251 circumstances like this:
257 let-closure x = [z] [y] (y+z)
259 The idea is that we compile code for @(y+z)@ in an environment in which
260 @z@ is bound to an offset from \tr{Node}, and @y@ is bound to an
261 offset from the stack pointer.
263 (A let-closure is an @StgLet@ with a @StgRhsClosure@ RHS.)
267 let-constructor x = Constructor [args]
271 (A let-constructor is an @StgLet@ with a @StgRhsCon@ RHS.)
274 Letrec-expressions are essentially the same deal as
275 let-closure/let-constructor, so we use a common structure and
276 distinguish between them with an @is_recursive@ boolean flag.
280 let-unboxed u = an arbitrary arithmetic expression in unboxed values
283 All the stuff on the RHS must be fully evaluated. No function calls either!
285 (We've backed away from this toward case-expressions with
286 suitably-magical alts ...)
289 ~[Advanced stuff here! Not to start with, but makes pattern matching
290 generate more efficient code.]
293 let-escapes-not fail = expr
296 Here the idea is that @e'@ guarantees not to put @fail@ in a data structure,
297 or pass it to another function. All @e'@ will ever do is tail-call @fail@.
298 Rather than build a closure for @fail@, all we need do is to record the stack
299 level at the moment of the @let-escapes-not@; then entering @fail@ is just
300 a matter of adjusting the stack pointer back down to that point and entering
305 f x y = let z = huge-expression in
311 (A let-escapes-not is an @StgLetNoEscape@.)
314 We may eventually want:
316 let-literal x = Literal
320 (ToDo: is this obsolete?)
323 And so the code for let(rec)-things:
326 (GenStgBinding bndr occ) -- right hand sides (see below)
327 (GenStgExpr bndr occ) -- body
329 | StgLetNoEscape -- remember: ``advanced stuff''
330 (GenStgLiveVars occ) -- Live in the whole let-expression
331 -- Mustn't overwrite these stack slots
332 -- *Doesn't* include binders of the let(rec).
334 (GenStgLiveVars occ) -- Live in the right hand sides (only)
335 -- These are the ones which must be saved on
336 -- the stack if they aren't there already
337 -- *Does* include binders of the let(rec) if recursive.
339 (GenStgBinding bndr occ) -- right hand sides (see below)
340 (GenStgExpr bndr occ) -- body
343 %************************************************************************
345 \subsubsection{@GenStgExpr@: @scc@ expressions}
347 %************************************************************************
349 Finally for @scc@ expressions we introduce a new STG construct.
353 CostCentre -- label of SCC expression
354 (GenStgExpr bndr occ) -- scc expression
357 %************************************************************************
359 \subsubsection{@GenStgExpr@: @hpc@ expressions}
361 %************************************************************************
363 Finally for @scc@ expressions we introduce a new STG construct.
367 Module -- the module of the source of this tick
369 (GenStgExpr bndr occ) -- sub expression
373 %************************************************************************
375 \subsection{STG right-hand sides}
377 %************************************************************************
379 Here's the rest of the interesting stuff for @StgLet@s; the first
380 flavour is for closures:
382 data GenStgRhs bndr occ
384 CostCentreStack -- CCS to be attached (default is CurrentCCS)
385 StgBinderInfo -- Info about how this binder is used (see below)
386 [occ] -- non-global free vars; a list, rather than
387 -- a set, because order is important
388 !UpdateFlag -- ReEntrant | Updatable | SingleEntry
389 SRT -- The SRT reference
390 [bndr] -- arguments; if empty, then not a function;
391 -- as above, order is important.
392 (GenStgExpr bndr occ) -- body
394 An example may be in order. Consider:
396 let t = \x -> \y -> ... x ... y ... p ... q in e
398 Pulling out the free vars and stylising somewhat, we get the equivalent:
400 let t = (\[p,q] -> \[x,y] -> ... x ... y ... p ...q) p q
402 Stg-operationally, the @[x,y]@ are on the stack, the @[p,q]@ are
403 offsets from @Node@ into the closure, and the code ptr for the closure
404 will be exactly that in parentheses above.
406 The second flavour of right-hand-side is for constructors (simple but important):
409 CostCentreStack -- CCS to be attached (default is CurrentCCS).
410 -- Top-level (static) ones will end up with
411 -- DontCareCCS, because we don't count static
412 -- data in heap profiles, and we don't set CCCS
413 -- from static closure.
414 DataCon -- constructor
415 [GenStgArg occ] -- args
419 stgRhsArity :: StgRhs -> Int
420 stgRhsArity (StgRhsClosure _ _ _ _ _ bndrs _) = count isId bndrs
421 -- The arity never includes type parameters, so
422 -- when keeping type arguments and binders in the Stg syntax
423 -- (opt_RuntimeTypes) we have to fliter out the type binders.
424 stgRhsArity (StgRhsCon _ _ _) = 0
428 stgBindHasCafRefs :: GenStgBinding bndr Id -> Bool
429 stgBindHasCafRefs (StgNonRec _ rhs) = rhsHasCafRefs rhs
430 stgBindHasCafRefs (StgRec binds) = any rhsHasCafRefs (map snd binds)
432 rhsHasCafRefs :: GenStgRhs bndr Id -> Bool
433 rhsHasCafRefs (StgRhsClosure _ _ _ upd srt _ _)
434 = isUpdatable upd || nonEmptySRT srt
435 rhsHasCafRefs (StgRhsCon _ _ args)
436 = any stgArgHasCafRefs args
438 stgArgHasCafRefs :: GenStgArg Id -> Bool
439 stgArgHasCafRefs (StgVarArg id) = mayHaveCafRefs (idCafInfo id)
440 stgArgHasCafRefs _ = False
443 Here's the @StgBinderInfo@ type, and its combining op:
447 | SatCallsOnly -- All occurrences are *saturated* *function* calls
448 -- This means we don't need to build an info table and
449 -- slow entry code for the thing
450 -- Thunks never get this value
452 noBinderInfo, stgUnsatOcc, stgSatOcc :: StgBinderInfo
453 noBinderInfo = NoStgBinderInfo
454 stgUnsatOcc = NoStgBinderInfo
455 stgSatOcc = SatCallsOnly
457 satCallsOnly :: StgBinderInfo -> Bool
458 satCallsOnly SatCallsOnly = True
459 satCallsOnly NoStgBinderInfo = False
461 combineStgBinderInfo :: StgBinderInfo -> StgBinderInfo -> StgBinderInfo
462 combineStgBinderInfo SatCallsOnly SatCallsOnly = SatCallsOnly
463 combineStgBinderInfo _ _ = NoStgBinderInfo
466 pp_binder_info :: StgBinderInfo -> SDoc
467 pp_binder_info NoStgBinderInfo = empty
468 pp_binder_info SatCallsOnly = ptext SLIT("sat-only")
471 %************************************************************************
473 \subsection[Stg-case-alternatives]{STG case alternatives}
475 %************************************************************************
477 Very like in @CoreSyntax@ (except no type-world stuff).
479 The type constructor is guaranteed not to be abstract; that is, we can
480 see its representation. This is important because the code generator
481 uses it to determine return conventions etc. But it's not trivial
482 where there's a moduule loop involved, because some versions of a type
483 constructor might not have all the constructors visible. So
484 mkStgAlgAlts (in CoreToStg) ensures that it gets the TyCon from the
485 constructors or literals (which are guaranteed to have the Real McCoy)
486 rather than from the scrutinee type.
489 type GenStgAlt bndr occ
490 = (AltCon, -- alts: data constructor,
491 [bndr], -- constructor's parameters,
492 [Bool], -- "use mask", same length as
493 -- parameters; a True in a
494 -- param's position if it is
496 GenStgExpr bndr occ) -- ...right-hand side.
499 = PolyAlt -- Polymorphic (a type variable)
500 | UbxTupAlt TyCon -- Unboxed tuple
501 | AlgAlt TyCon -- Algebraic data type; the AltCons will be DataAlts
502 | PrimAlt TyCon -- Primitive data type; the AltCons will be LitAlts
505 %************************************************************************
507 \subsection[Stg]{The Plain STG parameterisation}
509 %************************************************************************
511 This happens to be the only one we use at the moment.
514 type StgBinding = GenStgBinding Id Id
515 type StgArg = GenStgArg Id
516 type StgLiveVars = GenStgLiveVars Id
517 type StgExpr = GenStgExpr Id Id
518 type StgRhs = GenStgRhs Id Id
519 type StgAlt = GenStgAlt Id Id
522 %************************************************************************
524 \subsubsection[UpdateFlag-datatype]{@UpdateFlag@}
526 %************************************************************************
528 This is also used in @LambdaFormInfo@ in the @ClosureInfo@ module.
530 A @ReEntrant@ closure may be entered multiple times, but should not be
531 updated or blackholed. An @Updatable@ closure should be updated after
532 evaluation (and may be blackholed during evaluation). A @SingleEntry@
533 closure will only be entered once, and so need not be updated but may
534 safely be blackholed.
537 data UpdateFlag = ReEntrant | Updatable | SingleEntry
539 instance Outputable UpdateFlag where
541 = char (case u of { ReEntrant -> 'r'; Updatable -> 'u'; SingleEntry -> 's' })
543 isUpdatable :: UpdateFlag -> Bool
544 isUpdatable ReEntrant = False
545 isUpdatable SingleEntry = False
546 isUpdatable Updatable = True
549 %************************************************************************
551 \subsubsection{StgOp}
553 %************************************************************************
555 An StgOp allows us to group together PrimOps and ForeignCalls.
556 It's quite useful to move these around together, notably
557 in StgOpApp and COpStmt.
560 data StgOp = StgPrimOp PrimOp
562 | StgFCallOp ForeignCall Unique
563 -- The Unique is occasionally needed by the C pretty-printer
564 -- (which lacks a unique supply), notably when generating a
565 -- typedef for foreign-export-dynamic
569 %************************************************************************
571 \subsubsection[Static Reference Tables]{@SRT@}
573 %************************************************************************
575 There is one SRT per top-level function group. Each local binding and
576 case expression within this binding group has a subrange of the whole
577 SRT, expressed as an offset and length.
579 In CoreToStg we collect the list of CafRefs at each SRT site, which is later
580 converted into the length and offset form by the SRT pass.
585 -- generated by CoreToStg
586 | SRT !Int{-offset-} !Int{-length-} !Bitmap{-bitmap-}
587 -- generated by computeSRTs
589 nonEmptySRT :: SRT -> Bool
590 nonEmptySRT NoSRT = False
591 nonEmptySRT (SRTEntries vs) = not (isEmptyVarSet vs)
594 pprSRT :: SRT -> SDoc
595 pprSRT (NoSRT) = ptext SLIT("_no_srt_")
596 pprSRT (SRTEntries ids) = text "SRT:" <> ppr ids
597 pprSRT (SRT off _ _) = parens (ppr off <> comma <> text "*bitmap*")
600 %************************************************************************
602 \subsection[Stg-pretty-printing]{Pretty-printing}
604 %************************************************************************
606 Robin Popplestone asked for semi-colon separators on STG binds; here's
607 hoping he likes terminators instead... Ditto for case alternatives.
610 pprGenStgBinding :: (Outputable bndr, Outputable bdee, Ord bdee)
611 => GenStgBinding bndr bdee -> SDoc
613 pprGenStgBinding (StgNonRec bndr rhs)
614 = hang (hsep [ppr bndr, equals])
615 4 ((<>) (ppr rhs) semi)
617 pprGenStgBinding (StgRec pairs)
618 = vcat ((ifPprDebug (ptext SLIT("{- StgRec (begin) -}"))) :
619 (map (ppr_bind) pairs) ++ [(ifPprDebug (ptext SLIT("{- StgRec (end) -}")))])
621 ppr_bind (bndr, expr)
622 = hang (hsep [ppr bndr, equals])
623 4 ((<>) (ppr expr) semi)
625 pprStgBinding :: StgBinding -> SDoc
626 pprStgBinding bind = pprGenStgBinding bind
628 pprStgBindings :: [StgBinding] -> SDoc
629 pprStgBindings binds = vcat (map pprGenStgBinding binds)
631 pprGenStgBindingWithSRT
632 :: (Outputable bndr, Outputable bdee, Ord bdee)
633 => (GenStgBinding bndr bdee,[(Id,[Id])]) -> SDoc
635 pprGenStgBindingWithSRT (bind,srts)
636 = vcat (pprGenStgBinding bind : map pprSRT srts)
637 where pprSRT (id,srt) =
638 ptext SLIT("SRT") <> parens (ppr id) <> ptext SLIT(": ") <> ppr srt
640 pprStgBindingsWithSRTs :: [(StgBinding,[(Id,[Id])])] -> SDoc
641 pprStgBindingsWithSRTs binds = vcat (map pprGenStgBindingWithSRT binds)
645 instance (Outputable bdee) => Outputable (GenStgArg bdee) where
648 instance (Outputable bndr, Outputable bdee, Ord bdee)
649 => Outputable (GenStgBinding bndr bdee) where
650 ppr = pprGenStgBinding
652 instance (Outputable bndr, Outputable bdee, Ord bdee)
653 => Outputable (GenStgExpr bndr bdee) where
656 instance (Outputable bndr, Outputable bdee, Ord bdee)
657 => Outputable (GenStgRhs bndr bdee) where
658 ppr rhs = pprStgRhs rhs
662 pprStgArg :: (Outputable bdee) => GenStgArg bdee -> SDoc
664 pprStgArg (StgVarArg var) = ppr var
665 pprStgArg (StgLitArg con) = ppr con
666 pprStgArg (StgTypeArg ty) = char '@' <+> ppr ty
670 pprStgExpr :: (Outputable bndr, Outputable bdee, Ord bdee)
671 => GenStgExpr bndr bdee -> SDoc
673 pprStgExpr (StgLit lit) = ppr lit
676 pprStgExpr (StgApp func args)
678 4 (sep (map (ppr) args))
682 pprStgExpr (StgConApp con args)
683 = hsep [ ppr con, brackets (interppSP args)]
685 pprStgExpr (StgOpApp op args _)
686 = hsep [ pprStgOp op, brackets (interppSP args)]
688 pprStgExpr (StgLam _ bndrs body)
689 =sep [ char '\\' <+> ppr bndrs <+> ptext SLIT("->"),
694 -- special case: let v = <very specific thing>
700 -- Very special! Suspicious! (SLPJ)
703 pprStgExpr (StgLet srt (StgNonRec bndr (StgRhsClosure cc bi free_vars upd_flag args rhs))
706 (hang (hcat [ptext SLIT("let { "), ppr bndr, ptext SLIT(" = "),
709 ptext SLIT(" ["), ifPprDebug (interppSP free_vars), ptext SLIT("] \\"),
710 ppr upd_flag, ptext SLIT(" ["),
711 interppSP args, char ']'])
712 8 (sep [hsep [ppr rhs, ptext SLIT("} in")]]))
716 -- special case: let ... in let ...
718 pprStgExpr (StgLet bind expr@(StgLet _ _))
720 (sep [hang (ptext SLIT("let {"))
721 2 (hsep [pprGenStgBinding bind, ptext SLIT("} in")])])
725 pprStgExpr (StgLet bind expr)
726 = sep [hang (ptext SLIT("let {")) 2 (pprGenStgBinding bind),
727 hang (ptext SLIT("} in ")) 2 (ppr expr)]
729 pprStgExpr (StgLetNoEscape lvs_whole lvs_rhss bind expr)
730 = sep [hang (ptext SLIT("let-no-escape {"))
731 2 (pprGenStgBinding bind),
732 hang ((<>) (ptext SLIT("} in "))
735 hcat [ptext SLIT("-- lvs: ["), interppSP (uniqSetToList lvs_whole),
736 ptext SLIT("]; rhs lvs: ["), interppSP (uniqSetToList lvs_rhss),
740 pprStgExpr (StgSCC cc expr)
741 = sep [ hsep [ptext SLIT("_scc_"), ppr cc],
744 pprStgExpr (StgTick m n expr)
745 = sep [ hsep [ptext SLIT("_tick_"), pprModule m,text (show n)],
748 pprStgExpr (StgCase expr lvs_whole lvs_rhss bndr srt alt_type alts)
749 = sep [sep [ptext SLIT("case"),
750 nest 4 (hsep [pprStgExpr expr,
751 ifPprDebug (dcolon <+> ppr alt_type)]),
752 ptext SLIT("of"), ppr bndr, char '{'],
755 hcat [ptext SLIT("-- lvs: ["), interppSP (uniqSetToList lvs_whole),
756 ptext SLIT("]; rhs lvs: ["), interppSP (uniqSetToList lvs_rhss),
759 nest 2 (vcat (map pprStgAlt alts)),
762 pprStgAlt :: (Outputable bndr, Outputable occ, Ord occ)
763 => GenStgAlt bndr occ -> SDoc
764 pprStgAlt (con, params, _use_mask, expr)
765 = hang (hsep [ppr con, interppSP params, ptext SLIT("->")])
768 pprStgOp :: StgOp -> SDoc
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
781 pprStgLVs :: Outputable occ => GenStgLiveVars occ -> SDoc
783 = getPprStyle $ \ sty ->
784 if userStyle sty || isEmptyUniqSet lvs then
787 hcat [text "{-lvs:", interpp'SP (uniqSetToList lvs), text "-}"]
792 pprStgRhs :: (Outputable bndr, Outputable bdee, Ord bdee)
793 => GenStgRhs bndr bdee -> SDoc
796 pprStgRhs (StgRhsClosure cc bi [free_var] upd_flag srt [{-no args-}] (StgApp func []))
799 brackets (ifPprDebug (ppr free_var)),
800 ptext SLIT(" \\"), ppr upd_flag, pprMaybeSRT srt, ptext SLIT(" [] "), ppr func ]
803 pprStgRhs (StgRhsClosure cc bi free_vars upd_flag srt args body)
804 = hang (hsep [if opt_SccProfilingOn then ppr cc else empty,
806 ifPprDebug (brackets (interppSP free_vars)),
807 char '\\' <> ppr upd_flag, pprMaybeSRT srt, brackets (interppSP args)])
810 pprStgRhs (StgRhsCon cc con args)
812 space, ppr con, ptext SLIT("! "), brackets (interppSP args)]
814 pprMaybeSRT :: SRT -> SDoc
815 pprMaybeSRT (NoSRT) = empty
816 pprMaybeSRT srt = ptext SLIT("srt:") <> pprSRT srt