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,
33 SRT(..), noSRT, nonEmptySRT,
36 stgBindHasCafRefs, stgArgHasCafRefs, stgRhsArity, getArgPrimRep,
37 isDllConApp, isStgTypeArg,
38 stgArgType, stgBinders,
40 pprStgBinding, pprStgBindings, pprStgBindingsWithSRTs
47 #include "HsVersions.h"
49 import CostCentre ( CostCentreStack, CostCentre )
50 import VarSet ( IdSet, isEmptyVarSet )
52 import Id ( Id, idName, idPrimRep, idType, idCafInfo )
53 import IdInfo ( mayHaveCafRefs )
54 import Name ( isDllName )
55 import Literal ( Literal, literalType, literalPrimRep )
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 )
65 import UniqSet ( isEmptyUniqSet, uniqSetToList, UniqSet )
66 import Unique ( Unique )
68 import CmdLineOpts ( 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)]
89 stgBinders :: GenStgBinding bndr occ -> [bndr]
90 stgBinders (StgNonRec b _) = [b]
91 stgBinders (StgRec bs) = map fst bs
94 %************************************************************************
96 \subsection{@GenStgArg@}
98 %************************************************************************
104 | StgTypeArg Type -- For when we want to preserve all type info
108 getArgPrimRep (StgVarArg local) = idPrimRep local
109 getArgPrimRep (StgLitArg lit) = literalPrimRep lit
111 isStgTypeArg (StgTypeArg _) = True
112 isStgTypeArg other = False
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) = False
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
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"
133 %************************************************************************
135 \subsection{STG expressions}
137 %************************************************************************
139 The @GenStgExpr@ data type is parameterised on binder and occurrence
142 %************************************************************************
144 \subsubsection{@GenStgExpr@ application}
146 %************************************************************************
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.)
153 There is no constructor for a lone variable; it would appear as
156 type GenStgLiveVars occ = UniqSet occ
158 data GenStgExpr bndr occ
161 [GenStgArg occ] -- arguments; may be empty
164 %************************************************************************
166 \subsubsection{@StgConApp@ and @StgPrimApp@---saturated applications}
168 %************************************************************************
170 There are a specialised forms of application, for
171 constructors, primitives, and literals.
176 [GenStgArg occ] -- Saturated
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.
184 %************************************************************************
186 \subsubsection{@StgLam@}
188 %************************************************************************
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)
195 Type -- Type of whole lambda (useful when making a binder for it)
197 StgExpr -- Body of lambda
201 %************************************************************************
203 \subsubsection{@GenStgExpr@: case-expressions}
205 %************************************************************************
207 This has the same boxed/unboxed business as Core case expressions.
210 (GenStgExpr bndr occ)
211 -- the thing to examine
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
217 (GenStgLiveVars occ) -- Live vars of RHSs (plus what happens afterwards)
218 -- i.e., those which must be saved before eval.
220 -- note that an alt's constructor's
221 -- binder-variables are NOT counted in the
222 -- free vars for the alt's RHS
224 bndr -- binds the result of evaluating the scrutinee
226 SRT -- The SRT for the continuation
230 [GenStgAlt bndr occ] -- The DEFAULT case is always *first*
231 -- if it is there at all
234 %************************************************************************
236 \subsubsection{@GenStgExpr@: @let(rec)@-expressions}
238 %************************************************************************
240 The various forms of let(rec)-expression encode most of the
241 interesting things we want to do.
245 let-closure x = [free-vars] expr [args]
250 let x = (\free-vars -> \args -> expr) free-vars
252 \tr{args} may be empty (and is for most closures). It isn't under
253 circumstances like this:
259 let-closure x = [z] [y] (y+z)
261 The idea is that we compile code for @(y+z)@ in an environment in which
262 @z@ is bound to an offset from \tr{Node}, and @y@ is bound to an
263 offset from the stack pointer.
265 (A let-closure is an @StgLet@ with a @StgRhsClosure@ RHS.)
269 let-constructor x = Constructor [args]
273 (A let-constructor is an @StgLet@ with a @StgRhsCon@ RHS.)
276 Letrec-expressions are essentially the same deal as
277 let-closure/let-constructor, so we use a common structure and
278 distinguish between them with an @is_recursive@ boolean flag.
282 let-unboxed u = an arbitrary arithmetic expression in unboxed values
285 All the stuff on the RHS must be fully evaluated. No function calls either!
287 (We've backed away from this toward case-expressions with
288 suitably-magical alts ...)
291 ~[Advanced stuff here! Not to start with, but makes pattern matching
292 generate more efficient code.]
295 let-escapes-not fail = expr
298 Here the idea is that @e'@ guarantees not to put @fail@ in a data structure,
299 or pass it to another function. All @e'@ will ever do is tail-call @fail@.
300 Rather than build a closure for @fail@, all we need do is to record the stack
301 level at the moment of the @let-escapes-not@; then entering @fail@ is just
302 a matter of adjusting the stack pointer back down to that point and entering
307 f x y = let z = huge-expression in
313 (A let-escapes-not is an @StgLetNoEscape@.)
316 We may eventually want:
318 let-literal x = Literal
322 (ToDo: is this obsolete?)
325 And so the code for let(rec)-things:
328 (GenStgBinding bndr occ) -- right hand sides (see below)
329 (GenStgExpr bndr occ) -- body
331 | StgLetNoEscape -- remember: ``advanced stuff''
332 (GenStgLiveVars occ) -- Live in the whole let-expression
333 -- Mustn't overwrite these stack slots
334 -- *Doesn't* include binders of the let(rec).
336 (GenStgLiveVars occ) -- Live in the right hand sides (only)
337 -- These are the ones which must be saved on
338 -- the stack if they aren't there already
339 -- *Does* include binders of the let(rec) if recursive.
341 (GenStgBinding bndr occ) -- right hand sides (see below)
342 (GenStgExpr bndr occ) -- body
345 %************************************************************************
347 \subsubsection{@GenStgExpr@: @scc@ expressions}
349 %************************************************************************
351 Finally for @scc@ expressions we introduce a new STG construct.
355 CostCentre -- label of SCC expression
356 (GenStgExpr bndr occ) -- scc expression
360 %************************************************************************
362 \subsection{STG right-hand sides}
364 %************************************************************************
366 Here's the rest of the interesting stuff for @StgLet@s; the first
367 flavour is for closures:
369 data GenStgRhs bndr occ
371 CostCentreStack -- CCS to be attached (default is CurrentCCS)
372 StgBinderInfo -- Info about how this binder is used (see below)
373 [occ] -- non-global free vars; a list, rather than
374 -- a set, because order is important
375 !UpdateFlag -- ReEntrant | Updatable | SingleEntry
376 SRT -- The SRT reference
377 [bndr] -- arguments; if empty, then not a function;
378 -- as above, order is important.
379 (GenStgExpr bndr occ) -- body
381 An example may be in order. Consider:
383 let t = \x -> \y -> ... x ... y ... p ... q in e
385 Pulling out the free vars and stylising somewhat, we get the equivalent:
387 let t = (\[p,q] -> \[x,y] -> ... x ... y ... p ...q) p q
389 Stg-operationally, the @[x,y]@ are on the stack, the @[p,q]@ are
390 offsets from @Node@ into the closure, and the code ptr for the closure
391 will be exactly that in parentheses above.
393 The second flavour of right-hand-side is for constructors (simple but important):
396 CostCentreStack -- CCS to be attached (default is CurrentCCS).
397 -- Top-level (static) ones will end up with
398 -- DontCareCCS, because we don't count static
399 -- data in heap profiles, and we don't set CCCS
400 -- from static closure.
401 DataCon -- constructor
402 [GenStgArg occ] -- args
406 stgRhsArity :: StgRhs -> Int
407 stgRhsArity (StgRhsClosure _ _ _ _ _ bndrs _) = count isId bndrs
408 -- The arity never includes type parameters, so
409 -- when keeping type arguments and binders in the Stg syntax
410 -- (opt_RuntimeTypes) we have to fliter out the type binders.
411 stgRhsArity (StgRhsCon _ _ _) = 0
415 stgBindHasCafRefs :: GenStgBinding bndr Id -> Bool
416 stgBindHasCafRefs (StgNonRec _ rhs) = rhsHasCafRefs rhs
417 stgBindHasCafRefs (StgRec binds) = any rhsHasCafRefs (map snd binds)
419 rhsHasCafRefs (StgRhsClosure _ _ _ upd srt _ _)
420 = isUpdatable upd || nonEmptySRT srt
421 rhsHasCafRefs (StgRhsCon _ _ args)
422 = any stgArgHasCafRefs args
424 stgArgHasCafRefs (StgVarArg id) = mayHaveCafRefs (idCafInfo id)
425 stgArgHasCafRefs _ = False
428 Here's the @StgBinderInfo@ type, and its combining op:
432 | SatCallsOnly -- All occurrences are *saturated* *function* calls
433 -- This means we don't need to build an info table and
434 -- slow entry code for the thing
435 -- Thunks never get this value
437 noBinderInfo = NoStgBinderInfo
438 stgUnsatOcc = NoStgBinderInfo
439 stgSatOcc = SatCallsOnly
441 satCallsOnly :: StgBinderInfo -> Bool
442 satCallsOnly SatCallsOnly = True
443 satCallsOnly NoStgBinderInfo = False
445 combineStgBinderInfo :: StgBinderInfo -> StgBinderInfo -> StgBinderInfo
446 combineStgBinderInfo SatCallsOnly SatCallsOnly = SatCallsOnly
447 combineStgBinderInfo info1 info2 = NoStgBinderInfo
450 pp_binder_info NoStgBinderInfo = empty
451 pp_binder_info SatCallsOnly = ptext SLIT("sat-only")
454 %************************************************************************
456 \subsection[Stg-case-alternatives]{STG case alternatives}
458 %************************************************************************
460 Very like in @CoreSyntax@ (except no type-world stuff).
462 The type constructor is guaranteed not to be abstract; that is, we can
463 see its representation. This is important because the code generator
464 uses it to determine return conventions etc. But it's not trivial
465 where there's a moduule loop involved, because some versions of a type
466 constructor might not have all the constructors visible. So
467 mkStgAlgAlts (in CoreToStg) ensures that it gets the TyCon from the
468 constructors or literals (which are guaranteed to have the Real McCoy)
469 rather than from the scrutinee type.
472 type GenStgAlt bndr occ
473 = (AltCon, -- alts: data constructor,
474 [bndr], -- constructor's parameters,
475 [Bool], -- "use mask", same length as
476 -- parameters; a True in a
477 -- param's position if it is
479 GenStgExpr bndr occ) -- ...right-hand side.
482 = PolyAlt -- Polymorphic (a type variable)
483 | UbxTupAlt TyCon -- Unboxed tuple
484 | AlgAlt TyCon -- Algebraic data type; the AltCons will be DataAlts
485 | PrimAlt TyCon -- Primitive data type; the AltCons will be LitAlts
488 %************************************************************************
490 \subsection[Stg]{The Plain STG parameterisation}
492 %************************************************************************
494 This happens to be the only one we use at the moment.
497 type StgBinding = GenStgBinding Id Id
498 type StgArg = GenStgArg Id
499 type StgLiveVars = GenStgLiveVars Id
500 type StgExpr = GenStgExpr Id Id
501 type StgRhs = GenStgRhs Id Id
502 type StgAlt = GenStgAlt Id Id
505 %************************************************************************
507 \subsubsection[UpdateFlag-datatype]{@UpdateFlag@}
509 %************************************************************************
511 This is also used in @LambdaFormInfo@ in the @ClosureInfo@ module.
513 A @ReEntrant@ closure may be entered multiple times, but should not be
514 updated or blackholed. An @Updatable@ closure should be updated after
515 evaluation (and may be blackholed during evaluation). A @SingleEntry@
516 closure will only be entered once, and so need not be updated but may
517 safely be blackholed.
520 data UpdateFlag = ReEntrant | Updatable | SingleEntry
522 instance Outputable UpdateFlag where
524 = char (case u of { ReEntrant -> 'r'; Updatable -> 'u'; SingleEntry -> 's' })
526 isUpdatable ReEntrant = False
527 isUpdatable SingleEntry = False
528 isUpdatable Updatable = True
531 %************************************************************************
533 \subsubsection{StgOp}
535 %************************************************************************
537 An StgOp allows us to group together PrimOps and ForeignCalls.
538 It's quite useful to move these around together, notably
539 in StgOpApp and COpStmt.
542 data StgOp = StgPrimOp PrimOp
544 | StgFCallOp ForeignCall Unique
545 -- The Unique is occasionally needed by the C pretty-printer
546 -- (which lacks a unique supply), notably when generating a
547 -- typedef for foreign-export-dynamic
551 %************************************************************************
553 \subsubsection[Static Reference Tables]{@SRT@}
555 %************************************************************************
557 There is one SRT per top-level function group. Each local binding and
558 case expression within this binding group has a subrange of the whole
559 SRT, expressed as an offset and length.
561 In CoreToStg we collect the list of CafRefs at each SRT site, which is later
562 converted into the length and offset form by the SRT pass.
567 -- generated by CoreToStg
568 | SRT !Int{-offset-} !Int{-length-} !Bitmap{-bitmap-}
569 -- generated by computeSRTs
574 nonEmptySRT NoSRT = False
575 nonEmptySRT (SRTEntries vs) = not (isEmptyVarSet vs)
578 pprSRT (NoSRT) = ptext SLIT("_no_srt_")
579 pprSRT (SRTEntries ids) = text "SRT:" <> ppr ids
580 pprSRT (SRT off length bitmap) = parens (ppr off <> comma <> text "*bitmap*")
583 %************************************************************************
585 \subsection[Stg-pretty-printing]{Pretty-printing}
587 %************************************************************************
589 Robin Popplestone asked for semi-colon separators on STG binds; here's
590 hoping he likes terminators instead... Ditto for case alternatives.
593 pprGenStgBinding :: (Outputable bndr, Outputable bdee, Ord bdee)
594 => GenStgBinding bndr bdee -> SDoc
596 pprGenStgBinding (StgNonRec bndr rhs)
597 = hang (hsep [ppr bndr, equals])
598 4 ((<>) (ppr rhs) semi)
600 pprGenStgBinding (StgRec pairs)
601 = vcat ((ifPprDebug (ptext SLIT("{- StgRec (begin) -}"))) :
602 (map (ppr_bind) pairs) ++ [(ifPprDebug (ptext SLIT("{- StgRec (end) -}")))])
604 ppr_bind (bndr, expr)
605 = hang (hsep [ppr bndr, equals])
606 4 ((<>) (ppr expr) semi)
608 pprStgBinding :: StgBinding -> SDoc
609 pprStgBinding bind = pprGenStgBinding bind
611 pprStgBindings :: [StgBinding] -> SDoc
612 pprStgBindings binds = vcat (map pprGenStgBinding binds)
614 pprGenStgBindingWithSRT
615 :: (Outputable bndr, Outputable bdee, Ord bdee)
616 => (GenStgBinding bndr bdee,[(Id,[Id])]) -> SDoc
618 pprGenStgBindingWithSRT (bind,srts)
619 = vcat (pprGenStgBinding bind : map pprSRT srts)
620 where pprSRT (id,srt) =
621 ptext SLIT("SRT") <> parens (ppr id) <> ptext SLIT(": ") <> ppr srt
623 pprStgBindingsWithSRTs :: [(StgBinding,[(Id,[Id])])] -> SDoc
624 pprStgBindingsWithSRTs binds = vcat (map pprGenStgBindingWithSRT binds)
628 instance (Outputable bdee) => Outputable (GenStgArg bdee) where
631 instance (Outputable bndr, Outputable bdee, Ord bdee)
632 => Outputable (GenStgBinding bndr bdee) where
633 ppr = pprGenStgBinding
635 instance (Outputable bndr, Outputable bdee, Ord bdee)
636 => Outputable (GenStgExpr bndr bdee) where
639 instance (Outputable bndr, Outputable bdee, Ord bdee)
640 => Outputable (GenStgRhs bndr bdee) where
641 ppr rhs = pprStgRhs rhs
645 pprStgArg :: (Outputable bdee) => GenStgArg bdee -> SDoc
647 pprStgArg (StgVarArg var) = ppr var
648 pprStgArg (StgLitArg con) = ppr con
649 pprStgArg (StgTypeArg ty) = char '@' <+> ppr ty
653 pprStgExpr :: (Outputable bndr, Outputable bdee, Ord bdee)
654 => GenStgExpr bndr bdee -> SDoc
656 pprStgExpr (StgLit lit) = ppr lit
659 pprStgExpr (StgApp func args)
661 4 (sep (map (ppr) args))
665 pprStgExpr (StgConApp con args)
666 = hsep [ ppr con, brackets (interppSP args)]
668 pprStgExpr (StgOpApp op args _)
669 = hsep [ pprStgOp op, brackets (interppSP args)]
671 pprStgExpr (StgLam _ bndrs body)
672 =sep [ char '\\' <+> ppr bndrs <+> ptext SLIT("->"),
677 -- special case: let v = <very specific thing>
683 -- Very special! Suspicious! (SLPJ)
686 pprStgExpr (StgLet srt (StgNonRec bndr (StgRhsClosure cc bi free_vars upd_flag args rhs))
689 (hang (hcat [ptext SLIT("let { "), ppr bndr, ptext SLIT(" = "),
692 ptext SLIT(" ["), ifPprDebug (interppSP free_vars), ptext SLIT("] \\"),
693 ppr upd_flag, ptext SLIT(" ["),
694 interppSP args, char ']'])
695 8 (sep [hsep [ppr rhs, ptext SLIT("} in")]]))
699 -- special case: let ... in let ...
701 pprStgExpr (StgLet bind expr@(StgLet _ _))
703 (sep [hang (ptext SLIT("let {"))
704 2 (hsep [pprGenStgBinding bind, ptext SLIT("} in")])])
708 pprStgExpr (StgLet bind expr)
709 = sep [hang (ptext SLIT("let {")) 2 (pprGenStgBinding bind),
710 hang (ptext SLIT("} in ")) 2 (ppr expr)]
712 pprStgExpr (StgLetNoEscape lvs_whole lvs_rhss bind expr)
713 = sep [hang (ptext SLIT("let-no-escape {"))
714 2 (pprGenStgBinding bind),
715 hang ((<>) (ptext SLIT("} in "))
718 hcat [ptext SLIT("-- lvs: ["), interppSP (uniqSetToList lvs_whole),
719 ptext SLIT("]; rhs lvs: ["), interppSP (uniqSetToList lvs_rhss),
723 pprStgExpr (StgSCC cc expr)
724 = sep [ hsep [ptext SLIT("_scc_"), ppr cc],
727 pprStgExpr (StgCase expr lvs_whole lvs_rhss bndr srt alt_type alts)
728 = sep [sep [ptext SLIT("case"),
729 nest 4 (hsep [pprStgExpr expr,
730 ifPprDebug (dcolon <+> ppr alt_type)]),
731 ptext SLIT("of"), ppr bndr, char '{'],
734 hcat [ptext SLIT("-- lvs: ["), interppSP (uniqSetToList lvs_whole),
735 ptext SLIT("]; rhs lvs: ["), interppSP (uniqSetToList lvs_rhss),
738 nest 2 (vcat (map pprStgAlt alts)),
741 pprStgAlt (con, params, use_mask, expr)
742 = hang (hsep [ppr con, interppSP params, ptext SLIT("->")])
745 pprStgOp (StgPrimOp op) = ppr op
746 pprStgOp (StgFCallOp op _) = ppr op
748 instance Outputable AltType where
749 ppr PolyAlt = ptext SLIT("Polymorphic")
750 ppr (UbxTupAlt tc) = ptext SLIT("UbxTup") <+> ppr tc
751 ppr (AlgAlt tc) = ptext SLIT("Alg") <+> ppr tc
752 ppr (PrimAlt tc) = ptext SLIT("Prim") <+> ppr tc
756 pprStgLVs :: Outputable occ => GenStgLiveVars occ -> SDoc
758 = getPprStyle $ \ sty ->
759 if userStyle sty || isEmptyUniqSet lvs then
762 hcat [text "{-lvs:", interpp'SP (uniqSetToList lvs), text "-}"]
766 pprStgRhs :: (Outputable bndr, Outputable bdee, Ord bdee)
767 => GenStgRhs bndr bdee -> SDoc
770 pprStgRhs (StgRhsClosure cc bi [free_var] upd_flag srt [{-no args-}] (StgApp func []))
773 brackets (ifPprDebug (ppr free_var)),
774 ptext SLIT(" \\"), ppr upd_flag, pprMaybeSRT srt, ptext SLIT(" [] "), ppr func ]
777 pprStgRhs (StgRhsClosure cc bi free_vars upd_flag srt args body)
778 = hang (hsep [if opt_SccProfilingOn then ppr cc else empty,
780 ifPprDebug (brackets (interppSP free_vars)),
781 char '\\' <> ppr upd_flag, pprMaybeSRT srt, brackets (interppSP args)])
784 pprStgRhs (StgRhsCon cc con args)
786 space, ppr con, ptext SLIT("! "), brackets (interppSP args)]
788 pprMaybeSRT (NoSRT) = empty
789 pprMaybeSRT srt = ptext SLIT("srt:") <> pprSRT srt