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, 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)]
90 %************************************************************************
92 \subsection{@GenStgArg@}
94 %************************************************************************
100 | StgTypeArg Type -- For when we want to preserve all type info
104 isStgTypeArg (StgTypeArg _) = True
105 isStgTypeArg other = False
107 isDllArg :: StgArg -> Bool
108 -- Does this argument refer to something in a different DLL?
109 isDllArg (StgTypeArg v) = False
110 isDllArg (StgVarArg v) = isDllName (idName v)
111 isDllArg (StgLitArg lit) = False
113 isDllConApp :: 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 con args = isDllName (dataConName con) || any isDllArg args
119 stgArgType :: StgArg -> Type
120 -- Very half baked becase we have lost the type arguments
121 stgArgType (StgVarArg v) = idType v
122 stgArgType (StgLitArg lit) = literalType lit
123 stgArgType (StgTypeArg lit) = panic "stgArgType called on stgTypeArg"
126 %************************************************************************
128 \subsection{STG expressions}
130 %************************************************************************
132 The @GenStgExpr@ data type is parameterised on binder and occurrence
135 %************************************************************************
137 \subsubsection{@GenStgExpr@ application}
139 %************************************************************************
141 An application is of a function to a list of atoms [not expressions].
142 Operationally, we want to push the arguments on the stack and call the
143 function. (If the arguments were expressions, we would have to build
144 their closures first.)
146 There is no constructor for a lone variable; it would appear as
149 type GenStgLiveVars occ = UniqSet occ
151 data GenStgExpr bndr occ
154 [GenStgArg occ] -- arguments; may be empty
157 %************************************************************************
159 \subsubsection{@StgConApp@ and @StgPrimApp@---saturated applications}
161 %************************************************************************
163 There are a specialised forms of application, for
164 constructors, primitives, and literals.
169 [GenStgArg occ] -- Saturated
171 | StgOpApp StgOp -- Primitive op or foreign call
172 [GenStgArg occ] -- Saturated
173 Type -- Result type; we need to know the result type
174 -- so that we can assign result registers.
177 %************************************************************************
179 \subsubsection{@StgLam@}
181 %************************************************************************
183 StgLam is used *only* during CoreToStg's work. Before CoreToStg has finished
184 it encodes (\x -> e) as (let f = \x -> e in f)
188 Type -- Type of whole lambda (useful when making a binder for it)
190 StgExpr -- Body of lambda
194 %************************************************************************
196 \subsubsection{@GenStgExpr@: case-expressions}
198 %************************************************************************
200 This has the same boxed/unboxed business as Core case expressions.
203 (GenStgExpr bndr occ)
204 -- the thing to examine
206 (GenStgLiveVars occ) -- Live vars of whole case expression,
207 -- plus everything that happens after the case
208 -- i.e., those which mustn't be overwritten
210 (GenStgLiveVars occ) -- Live vars of RHSs (plus what happens afterwards)
211 -- i.e., those which must be saved before eval.
213 -- note that an alt's constructor's
214 -- binder-variables are NOT counted in the
215 -- free vars for the alt's RHS
217 bndr -- binds the result of evaluating the scrutinee
219 SRT -- The SRT for the continuation
223 [GenStgAlt bndr occ] -- The DEFAULT case is always *first*
224 -- if it is there at all
227 %************************************************************************
229 \subsubsection{@GenStgExpr@: @let(rec)@-expressions}
231 %************************************************************************
233 The various forms of let(rec)-expression encode most of the
234 interesting things we want to do.
238 let-closure x = [free-vars] expr [args]
243 let x = (\free-vars -> \args -> expr) free-vars
245 \tr{args} may be empty (and is for most closures). It isn't under
246 circumstances like this:
252 let-closure x = [z] [y] (y+z)
254 The idea is that we compile code for @(y+z)@ in an environment in which
255 @z@ is bound to an offset from \tr{Node}, and @y@ is bound to an
256 offset from the stack pointer.
258 (A let-closure is an @StgLet@ with a @StgRhsClosure@ RHS.)
262 let-constructor x = Constructor [args]
266 (A let-constructor is an @StgLet@ with a @StgRhsCon@ RHS.)
269 Letrec-expressions are essentially the same deal as
270 let-closure/let-constructor, so we use a common structure and
271 distinguish between them with an @is_recursive@ boolean flag.
275 let-unboxed u = an arbitrary arithmetic expression in unboxed values
278 All the stuff on the RHS must be fully evaluated. No function calls either!
280 (We've backed away from this toward case-expressions with
281 suitably-magical alts ...)
284 ~[Advanced stuff here! Not to start with, but makes pattern matching
285 generate more efficient code.]
288 let-escapes-not fail = expr
291 Here the idea is that @e'@ guarantees not to put @fail@ in a data structure,
292 or pass it to another function. All @e'@ will ever do is tail-call @fail@.
293 Rather than build a closure for @fail@, all we need do is to record the stack
294 level at the moment of the @let-escapes-not@; then entering @fail@ is just
295 a matter of adjusting the stack pointer back down to that point and entering
300 f x y = let z = huge-expression in
306 (A let-escapes-not is an @StgLetNoEscape@.)
309 We may eventually want:
311 let-literal x = Literal
315 (ToDo: is this obsolete?)
318 And so the code for let(rec)-things:
321 (GenStgBinding bndr occ) -- right hand sides (see below)
322 (GenStgExpr bndr occ) -- body
324 | StgLetNoEscape -- remember: ``advanced stuff''
325 (GenStgLiveVars occ) -- Live in the whole let-expression
326 -- Mustn't overwrite these stack slots
327 -- *Doesn't* include binders of the let(rec).
329 (GenStgLiveVars occ) -- Live in the right hand sides (only)
330 -- These are the ones which must be saved on
331 -- the stack if they aren't there already
332 -- *Does* include binders of the let(rec) if recursive.
334 (GenStgBinding bndr occ) -- right hand sides (see below)
335 (GenStgExpr bndr occ) -- body
338 %************************************************************************
340 \subsubsection{@GenStgExpr@: @scc@ expressions}
342 %************************************************************************
344 Finally for @scc@ expressions we introduce a new STG construct.
348 CostCentre -- label of SCC expression
349 (GenStgExpr bndr occ) -- scc expression
353 %************************************************************************
355 \subsection{STG right-hand sides}
357 %************************************************************************
359 Here's the rest of the interesting stuff for @StgLet@s; the first
360 flavour is for closures:
362 data GenStgRhs bndr occ
364 CostCentreStack -- CCS to be attached (default is CurrentCCS)
365 StgBinderInfo -- Info about how this binder is used (see below)
366 [occ] -- non-global free vars; a list, rather than
367 -- a set, because order is important
368 !UpdateFlag -- ReEntrant | Updatable | SingleEntry
369 SRT -- The SRT reference
370 [bndr] -- arguments; if empty, then not a function;
371 -- as above, order is important.
372 (GenStgExpr bndr occ) -- body
374 An example may be in order. Consider:
376 let t = \x -> \y -> ... x ... y ... p ... q in e
378 Pulling out the free vars and stylising somewhat, we get the equivalent:
380 let t = (\[p,q] -> \[x,y] -> ... x ... y ... p ...q) p q
382 Stg-operationally, the @[x,y]@ are on the stack, the @[p,q]@ are
383 offsets from @Node@ into the closure, and the code ptr for the closure
384 will be exactly that in parentheses above.
386 The second flavour of right-hand-side is for constructors (simple but important):
389 CostCentreStack -- CCS to be attached (default is CurrentCCS).
390 -- Top-level (static) ones will end up with
391 -- DontCareCCS, because we don't count static
392 -- data in heap profiles, and we don't set CCCS
393 -- from static closure.
394 DataCon -- constructor
395 [GenStgArg occ] -- args
399 stgRhsArity :: StgRhs -> Int
400 stgRhsArity (StgRhsClosure _ _ _ _ _ bndrs _) = count isId bndrs
401 -- The arity never includes type parameters, so
402 -- when keeping type arguments and binders in the Stg syntax
403 -- (opt_RuntimeTypes) we have to fliter out the type binders.
404 stgRhsArity (StgRhsCon _ _ _) = 0
408 stgBindHasCafRefs :: GenStgBinding bndr Id -> Bool
409 stgBindHasCafRefs (StgNonRec _ rhs) = rhsHasCafRefs rhs
410 stgBindHasCafRefs (StgRec binds) = any rhsHasCafRefs (map snd binds)
412 rhsHasCafRefs (StgRhsClosure _ _ _ upd srt _ _)
413 = isUpdatable upd || nonEmptySRT srt
414 rhsHasCafRefs (StgRhsCon _ _ args)
415 = any stgArgHasCafRefs args
417 stgArgHasCafRefs (StgVarArg id) = mayHaveCafRefs (idCafInfo id)
418 stgArgHasCafRefs _ = False
421 Here's the @StgBinderInfo@ type, and its combining op:
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
430 noBinderInfo = NoStgBinderInfo
431 stgUnsatOcc = NoStgBinderInfo
432 stgSatOcc = SatCallsOnly
434 satCallsOnly :: StgBinderInfo -> Bool
435 satCallsOnly SatCallsOnly = True
436 satCallsOnly NoStgBinderInfo = False
438 combineStgBinderInfo :: StgBinderInfo -> StgBinderInfo -> StgBinderInfo
439 combineStgBinderInfo SatCallsOnly SatCallsOnly = SatCallsOnly
440 combineStgBinderInfo info1 info2 = NoStgBinderInfo
443 pp_binder_info NoStgBinderInfo = empty
444 pp_binder_info SatCallsOnly = ptext SLIT("sat-only")
447 %************************************************************************
449 \subsection[Stg-case-alternatives]{STG case alternatives}
451 %************************************************************************
453 Very like in @CoreSyntax@ (except no type-world stuff).
455 The type constructor is guaranteed not to be abstract; that is, we can
456 see its representation. This is important because the code generator
457 uses it to determine return conventions etc. But it's not trivial
458 where there's a moduule loop involved, because some versions of a type
459 constructor might not have all the constructors visible. So
460 mkStgAlgAlts (in CoreToStg) ensures that it gets the TyCon from the
461 constructors or literals (which are guaranteed to have the Real McCoy)
462 rather than from the scrutinee type.
465 type GenStgAlt bndr occ
466 = (AltCon, -- alts: data constructor,
467 [bndr], -- constructor's parameters,
468 [Bool], -- "use mask", same length as
469 -- parameters; a True in a
470 -- param's position if it is
472 GenStgExpr bndr occ) -- ...right-hand side.
475 = PolyAlt -- Polymorphic (a type variable)
476 | UbxTupAlt TyCon -- Unboxed tuple
477 | AlgAlt TyCon -- Algebraic data type; the AltCons will be DataAlts
478 | PrimAlt TyCon -- Primitive data type; the AltCons will be LitAlts
481 %************************************************************************
483 \subsection[Stg]{The Plain STG parameterisation}
485 %************************************************************************
487 This happens to be the only one we use at the moment.
490 type StgBinding = GenStgBinding Id Id
491 type StgArg = GenStgArg Id
492 type StgLiveVars = GenStgLiveVars Id
493 type StgExpr = GenStgExpr Id Id
494 type StgRhs = GenStgRhs Id Id
495 type StgAlt = GenStgAlt Id Id
498 %************************************************************************
500 \subsubsection[UpdateFlag-datatype]{@UpdateFlag@}
502 %************************************************************************
504 This is also used in @LambdaFormInfo@ in the @ClosureInfo@ module.
506 A @ReEntrant@ closure may be entered multiple times, but should not be
507 updated or blackholed. An @Updatable@ closure should be updated after
508 evaluation (and may be blackholed during evaluation). A @SingleEntry@
509 closure will only be entered once, and so need not be updated but may
510 safely be blackholed.
513 data UpdateFlag = ReEntrant | Updatable | SingleEntry
515 instance Outputable UpdateFlag where
517 = char (case u of { ReEntrant -> 'r'; Updatable -> 'u'; SingleEntry -> 's' })
519 isUpdatable ReEntrant = False
520 isUpdatable SingleEntry = False
521 isUpdatable Updatable = True
524 %************************************************************************
526 \subsubsection{StgOp}
528 %************************************************************************
530 An StgOp allows us to group together PrimOps and ForeignCalls.
531 It's quite useful to move these around together, notably
532 in StgOpApp and COpStmt.
535 data StgOp = StgPrimOp PrimOp
537 | StgFCallOp ForeignCall Unique
538 -- The Unique is occasionally needed by the C pretty-printer
539 -- (which lacks a unique supply), notably when generating a
540 -- typedef for foreign-export-dynamic
544 %************************************************************************
546 \subsubsection[Static Reference Tables]{@SRT@}
548 %************************************************************************
550 There is one SRT per top-level function group. Each local binding and
551 case expression within this binding group has a subrange of the whole
552 SRT, expressed as an offset and length.
554 In CoreToStg we collect the list of CafRefs at each SRT site, which is later
555 converted into the length and offset form by the SRT pass.
560 -- generated by CoreToStg
561 | SRT !Int{-offset-} !Int{-length-} !Bitmap{-bitmap-}
562 -- generated by computeSRTs
567 nonEmptySRT NoSRT = False
568 nonEmptySRT (SRTEntries vs) = not (isEmptyVarSet vs)
571 pprSRT (NoSRT) = ptext SLIT("_no_srt_")
572 pprSRT (SRTEntries ids) = text "SRT:" <> ppr ids
573 pprSRT (SRT off length bitmap) = parens (ppr off <> comma <> text "*bitmap*")
576 %************************************************************************
578 \subsection[Stg-pretty-printing]{Pretty-printing}
580 %************************************************************************
582 Robin Popplestone asked for semi-colon separators on STG binds; here's
583 hoping he likes terminators instead... Ditto for case alternatives.
586 pprGenStgBinding :: (Outputable bndr, Outputable bdee, Ord bdee)
587 => GenStgBinding bndr bdee -> SDoc
589 pprGenStgBinding (StgNonRec bndr rhs)
590 = hang (hsep [ppr bndr, equals])
591 4 ((<>) (ppr rhs) semi)
593 pprGenStgBinding (StgRec pairs)
594 = vcat ((ifPprDebug (ptext SLIT("{- StgRec (begin) -}"))) :
595 (map (ppr_bind) pairs) ++ [(ifPprDebug (ptext SLIT("{- StgRec (end) -}")))])
597 ppr_bind (bndr, expr)
598 = hang (hsep [ppr bndr, equals])
599 4 ((<>) (ppr expr) semi)
601 pprStgBinding :: StgBinding -> SDoc
602 pprStgBinding bind = pprGenStgBinding bind
604 pprStgBindings :: [StgBinding] -> SDoc
605 pprStgBindings binds = vcat (map pprGenStgBinding binds)
607 pprGenStgBindingWithSRT
608 :: (Outputable bndr, Outputable bdee, Ord bdee)
609 => (GenStgBinding bndr bdee,[(Id,[Id])]) -> SDoc
611 pprGenStgBindingWithSRT (bind,srts)
612 = vcat (pprGenStgBinding bind : map pprSRT srts)
613 where pprSRT (id,srt) =
614 ptext SLIT("SRT") <> parens (ppr id) <> ptext SLIT(": ") <> ppr srt
616 pprStgBindingsWithSRTs :: [(StgBinding,[(Id,[Id])])] -> SDoc
617 pprStgBindingsWithSRTs binds = vcat (map pprGenStgBindingWithSRT binds)
621 instance (Outputable bdee) => Outputable (GenStgArg bdee) where
624 instance (Outputable bndr, Outputable bdee, Ord bdee)
625 => Outputable (GenStgBinding bndr bdee) where
626 ppr = pprGenStgBinding
628 instance (Outputable bndr, Outputable bdee, Ord bdee)
629 => Outputable (GenStgExpr bndr bdee) where
632 instance (Outputable bndr, Outputable bdee, Ord bdee)
633 => Outputable (GenStgRhs bndr bdee) where
634 ppr rhs = pprStgRhs rhs
638 pprStgArg :: (Outputable bdee) => GenStgArg bdee -> SDoc
640 pprStgArg (StgVarArg var) = ppr var
641 pprStgArg (StgLitArg con) = ppr con
642 pprStgArg (StgTypeArg ty) = char '@' <+> ppr ty
646 pprStgExpr :: (Outputable bndr, Outputable bdee, Ord bdee)
647 => GenStgExpr bndr bdee -> SDoc
649 pprStgExpr (StgLit lit) = ppr lit
652 pprStgExpr (StgApp func args)
654 4 (sep (map (ppr) args))
658 pprStgExpr (StgConApp con args)
659 = hsep [ ppr con, brackets (interppSP args)]
661 pprStgExpr (StgOpApp op args _)
662 = hsep [ pprStgOp op, brackets (interppSP args)]
664 pprStgExpr (StgLam _ bndrs body)
665 =sep [ char '\\' <+> ppr bndrs <+> ptext SLIT("->"),
670 -- special case: let v = <very specific thing>
676 -- Very special! Suspicious! (SLPJ)
679 pprStgExpr (StgLet srt (StgNonRec bndr (StgRhsClosure cc bi free_vars upd_flag args rhs))
682 (hang (hcat [ptext SLIT("let { "), ppr bndr, ptext SLIT(" = "),
685 ptext SLIT(" ["), ifPprDebug (interppSP free_vars), ptext SLIT("] \\"),
686 ppr upd_flag, ptext SLIT(" ["),
687 interppSP args, char ']'])
688 8 (sep [hsep [ppr rhs, ptext SLIT("} in")]]))
692 -- special case: let ... in let ...
694 pprStgExpr (StgLet bind expr@(StgLet _ _))
696 (sep [hang (ptext SLIT("let {"))
697 2 (hsep [pprGenStgBinding bind, ptext SLIT("} in")])])
701 pprStgExpr (StgLet bind expr)
702 = sep [hang (ptext SLIT("let {")) 2 (pprGenStgBinding bind),
703 hang (ptext SLIT("} in ")) 2 (ppr expr)]
705 pprStgExpr (StgLetNoEscape lvs_whole lvs_rhss bind expr)
706 = sep [hang (ptext SLIT("let-no-escape {"))
707 2 (pprGenStgBinding bind),
708 hang ((<>) (ptext SLIT("} in "))
711 hcat [ptext SLIT("-- lvs: ["), interppSP (uniqSetToList lvs_whole),
712 ptext SLIT("]; rhs lvs: ["), interppSP (uniqSetToList lvs_rhss),
716 pprStgExpr (StgSCC cc expr)
717 = sep [ hsep [ptext SLIT("_scc_"), ppr cc],
720 pprStgExpr (StgCase expr lvs_whole lvs_rhss bndr srt alt_type alts)
721 = sep [sep [ptext SLIT("case"),
722 nest 4 (hsep [pprStgExpr expr,
723 ifPprDebug (dcolon <+> ppr alt_type)]),
724 ptext SLIT("of"), ppr bndr, char '{'],
727 hcat [ptext SLIT("-- lvs: ["), interppSP (uniqSetToList lvs_whole),
728 ptext SLIT("]; rhs lvs: ["), interppSP (uniqSetToList lvs_rhss),
731 nest 2 (vcat (map pprStgAlt alts)),
734 pprStgAlt (con, params, use_mask, expr)
735 = hang (hsep [ppr con, interppSP params, ptext SLIT("->")])
738 pprStgOp (StgPrimOp op) = ppr op
739 pprStgOp (StgFCallOp op _) = ppr op
741 instance Outputable AltType where
742 ppr PolyAlt = ptext SLIT("Polymorphic")
743 ppr (UbxTupAlt tc) = ptext SLIT("UbxTup") <+> ppr tc
744 ppr (AlgAlt tc) = ptext SLIT("Alg") <+> ppr tc
745 ppr (PrimAlt tc) = ptext SLIT("Prim") <+> ppr tc
749 pprStgLVs :: Outputable occ => GenStgLiveVars occ -> SDoc
751 = getPprStyle $ \ sty ->
752 if userStyle sty || isEmptyUniqSet lvs then
755 hcat [text "{-lvs:", interpp'SP (uniqSetToList lvs), text "-}"]
759 pprStgRhs :: (Outputable bndr, Outputable bdee, Ord bdee)
760 => GenStgRhs bndr bdee -> SDoc
763 pprStgRhs (StgRhsClosure cc bi [free_var] upd_flag srt [{-no args-}] (StgApp func []))
766 brackets (ifPprDebug (ppr free_var)),
767 ptext SLIT(" \\"), ppr upd_flag, pprMaybeSRT srt, ptext SLIT(" [] "), ppr func ]
770 pprStgRhs (StgRhsClosure cc bi free_vars upd_flag srt args body)
771 = hang (hsep [if opt_SccProfilingOn then ppr cc else empty,
773 ifPprDebug (brackets (interppSP free_vars)),
774 char '\\' <> ppr upd_flag, pprMaybeSRT srt, brackets (interppSP args)])
777 pprStgRhs (StgRhsCon cc con args)
779 space, ppr con, ptext SLIT("! "), brackets (interppSP args)]
781 pprMaybeSRT (NoSRT) = empty
782 pprMaybeSRT srt = ptext SLIT("srt:") <> pprSRT srt