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 PrimOp ( PrimOp )
63 import TyCon ( TyCon )
64 import UniqSet ( isEmptyUniqSet, uniqSetToList, UniqSet )
65 import Unique ( Unique )
67 import CmdLineOpts ( opt_SccProfilingOn )
70 %************************************************************************
72 \subsection{@GenStgBinding@}
74 %************************************************************************
76 As usual, expressions are interesting; other things are boring. Here
77 are the boring things [except note the @GenStgRhs@], parameterised
78 with respect to binder and occurrence information (just as in
81 There is one SRT for each group of bindings.
84 data GenStgBinding bndr occ
85 = StgNonRec bndr (GenStgRhs bndr occ)
86 | StgRec [(bndr, GenStgRhs bndr occ)]
88 stgBinders :: GenStgBinding bndr occ -> [bndr]
89 stgBinders (StgNonRec b _) = [b]
90 stgBinders (StgRec bs) = map fst bs
93 %************************************************************************
95 \subsection{@GenStgArg@}
97 %************************************************************************
103 | StgTypeArg Type -- For when we want to preserve all type info
107 getArgPrimRep (StgVarArg local) = idPrimRep local
108 getArgPrimRep (StgLitArg lit) = literalPrimRep lit
110 isStgTypeArg (StgTypeArg _) = True
111 isStgTypeArg other = False
113 isDllArg :: StgArg -> Bool
114 -- Does this argument refer to something in a different DLL?
115 isDllArg (StgTypeArg v) = False
116 isDllArg (StgVarArg v) = isDllName (idName v)
117 isDllArg (StgLitArg lit) = False
119 isDllConApp :: DataCon -> [StgArg] -> Bool
120 -- Does this constructor application refer to
121 -- anything in a different DLL?
122 -- If so, we can't allocate it statically
123 isDllConApp con args = isDllName (dataConName con) || any isDllArg args
125 stgArgType :: StgArg -> Type
126 -- Very half baked becase we have lost the type arguments
127 stgArgType (StgVarArg v) = idType v
128 stgArgType (StgLitArg lit) = literalType lit
129 stgArgType (StgTypeArg lit) = panic "stgArgType called on stgTypeArg"
132 %************************************************************************
134 \subsection{STG expressions}
136 %************************************************************************
138 The @GenStgExpr@ data type is parameterised on binder and occurrence
141 %************************************************************************
143 \subsubsection{@GenStgExpr@ application}
145 %************************************************************************
147 An application is of a function to a list of atoms [not expressions].
148 Operationally, we want to push the arguments on the stack and call the
149 function. (If the arguments were expressions, we would have to build
150 their closures first.)
152 There is no constructor for a lone variable; it would appear as
155 type GenStgLiveVars occ = UniqSet occ
157 data GenStgExpr bndr occ
160 [GenStgArg occ] -- arguments; may be empty
163 %************************************************************************
165 \subsubsection{@StgConApp@ and @StgPrimApp@---saturated applications}
167 %************************************************************************
169 There are a specialised forms of application, for
170 constructors, primitives, and literals.
175 [GenStgArg occ] -- Saturated
177 | StgOpApp StgOp -- Primitive op or foreign call
178 [GenStgArg occ] -- Saturated
179 Type -- Result type; we need to know the result type
180 -- so that we can assign result registers.
183 %************************************************************************
185 \subsubsection{@StgLam@}
187 %************************************************************************
189 StgLam is used *only* during CoreToStg's work. Before CoreToStg has finished
190 it encodes (\x -> e) as (let f = \x -> e in f)
194 Type -- Type of whole lambda (useful when making a binder for it)
196 StgExpr -- Body of lambda
200 %************************************************************************
202 \subsubsection{@GenStgExpr@: case-expressions}
204 %************************************************************************
206 This has the same boxed/unboxed business as Core case expressions.
209 (GenStgExpr bndr occ)
210 -- the thing to examine
212 (GenStgLiveVars occ) -- Live vars of whole case expression,
213 -- plus everything that happens after the case
214 -- i.e., those which mustn't be overwritten
216 (GenStgLiveVars occ) -- Live vars of RHSs (plus what happens afterwards)
217 -- i.e., those which must be saved before eval.
219 -- note that an alt's constructor's
220 -- binder-variables are NOT counted in the
221 -- free vars for the alt's RHS
223 bndr -- binds the result of evaluating the scrutinee
225 SRT -- The SRT for the continuation
229 [GenStgAlt bndr occ] -- The DEFAULT case is always *first*
230 -- if it is there at all
233 %************************************************************************
235 \subsubsection{@GenStgExpr@: @let(rec)@-expressions}
237 %************************************************************************
239 The various forms of let(rec)-expression encode most of the
240 interesting things we want to do.
244 let-closure x = [free-vars] expr [args]
249 let x = (\free-vars -> \args -> expr) free-vars
251 \tr{args} may be empty (and is for most closures). It isn't under
252 circumstances like this:
258 let-closure x = [z] [y] (y+z)
260 The idea is that we compile code for @(y+z)@ in an environment in which
261 @z@ is bound to an offset from \tr{Node}, and @y@ is bound to an
262 offset from the stack pointer.
264 (A let-closure is an @StgLet@ with a @StgRhsClosure@ RHS.)
268 let-constructor x = Constructor [args]
272 (A let-constructor is an @StgLet@ with a @StgRhsCon@ RHS.)
275 Letrec-expressions are essentially the same deal as
276 let-closure/let-constructor, so we use a common structure and
277 distinguish between them with an @is_recursive@ boolean flag.
281 let-unboxed u = an arbitrary arithmetic expression in unboxed values
284 All the stuff on the RHS must be fully evaluated. No function calls either!
286 (We've backed away from this toward case-expressions with
287 suitably-magical alts ...)
290 ~[Advanced stuff here! Not to start with, but makes pattern matching
291 generate more efficient code.]
294 let-escapes-not fail = expr
297 Here the idea is that @e'@ guarantees not to put @fail@ in a data structure,
298 or pass it to another function. All @e'@ will ever do is tail-call @fail@.
299 Rather than build a closure for @fail@, all we need do is to record the stack
300 level at the moment of the @let-escapes-not@; then entering @fail@ is just
301 a matter of adjusting the stack pointer back down to that point and entering
306 f x y = let z = huge-expression in
312 (A let-escapes-not is an @StgLetNoEscape@.)
315 We may eventually want:
317 let-literal x = Literal
321 (ToDo: is this obsolete?)
324 And so the code for let(rec)-things:
327 (GenStgBinding bndr occ) -- right hand sides (see below)
328 (GenStgExpr bndr occ) -- body
330 | StgLetNoEscape -- remember: ``advanced stuff''
331 (GenStgLiveVars occ) -- Live in the whole let-expression
332 -- Mustn't overwrite these stack slots
333 -- *Doesn't* include binders of the let(rec).
335 (GenStgLiveVars occ) -- Live in the right hand sides (only)
336 -- These are the ones which must be saved on
337 -- the stack if they aren't there already
338 -- *Does* include binders of the let(rec) if recursive.
340 (GenStgBinding bndr occ) -- right hand sides (see below)
341 (GenStgExpr bndr occ) -- body
344 %************************************************************************
346 \subsubsection{@GenStgExpr@: @scc@ expressions}
348 %************************************************************************
350 Finally for @scc@ expressions we introduce a new STG construct.
354 CostCentre -- label of SCC expression
355 (GenStgExpr bndr occ) -- scc expression
359 %************************************************************************
361 \subsection{STG right-hand sides}
363 %************************************************************************
365 Here's the rest of the interesting stuff for @StgLet@s; the first
366 flavour is for closures:
368 data GenStgRhs bndr occ
370 CostCentreStack -- CCS to be attached (default is CurrentCCS)
371 StgBinderInfo -- Info about how this binder is used (see below)
372 [occ] -- non-global free vars; a list, rather than
373 -- a set, because order is important
374 !UpdateFlag -- ReEntrant | Updatable | SingleEntry
375 SRT -- The SRT reference
376 [bndr] -- arguments; if empty, then not a function;
377 -- as above, order is important.
378 (GenStgExpr bndr occ) -- body
380 An example may be in order. Consider:
382 let t = \x -> \y -> ... x ... y ... p ... q in e
384 Pulling out the free vars and stylising somewhat, we get the equivalent:
386 let t = (\[p,q] -> \[x,y] -> ... x ... y ... p ...q) p q
388 Stg-operationally, the @[x,y]@ are on the stack, the @[p,q]@ are
389 offsets from @Node@ into the closure, and the code ptr for the closure
390 will be exactly that in parentheses above.
392 The second flavour of right-hand-side is for constructors (simple but important):
395 CostCentreStack -- CCS to be attached (default is CurrentCCS).
396 -- Top-level (static) ones will end up with
397 -- DontCareCCS, because we don't count static
398 -- data in heap profiles, and we don't set CCCS
399 -- from static closure.
400 DataCon -- constructor
401 [GenStgArg occ] -- args
405 stgRhsArity :: StgRhs -> Int
406 stgRhsArity (StgRhsClosure _ _ _ _ _ bndrs _) = count isId bndrs
407 -- The arity never includes type parameters, so
408 -- when keeping type arguments and binders in the Stg syntax
409 -- (opt_RuntimeTypes) we have to fliter out the type binders.
410 stgRhsArity (StgRhsCon _ _ _) = 0
414 stgBindHasCafRefs :: GenStgBinding bndr Id -> Bool
415 stgBindHasCafRefs (StgNonRec _ rhs) = rhsHasCafRefs rhs
416 stgBindHasCafRefs (StgRec binds) = any rhsHasCafRefs (map snd binds)
418 rhsHasCafRefs (StgRhsClosure _ _ _ upd srt _ _)
419 = isUpdatable upd || nonEmptySRT srt
420 rhsHasCafRefs (StgRhsCon _ _ args)
421 = any stgArgHasCafRefs args
423 stgArgHasCafRefs (StgVarArg id) = mayHaveCafRefs (idCafInfo id)
424 stgArgHasCafRefs _ = False
427 Here's the @StgBinderInfo@ type, and its combining op:
431 | SatCallsOnly -- All occurrences are *saturated* *function* calls
432 -- This means we don't need to build an info table and
433 -- slow entry code for the thing
434 -- Thunks never get this value
436 noBinderInfo = NoStgBinderInfo
437 stgUnsatOcc = NoStgBinderInfo
438 stgSatOcc = SatCallsOnly
440 satCallsOnly :: StgBinderInfo -> Bool
441 satCallsOnly SatCallsOnly = True
442 satCallsOnly NoStgBinderInfo = False
444 combineStgBinderInfo :: StgBinderInfo -> StgBinderInfo -> StgBinderInfo
445 combineStgBinderInfo SatCallsOnly SatCallsOnly = SatCallsOnly
446 combineStgBinderInfo info1 info2 = NoStgBinderInfo
449 pp_binder_info NoStgBinderInfo = empty
450 pp_binder_info SatCallsOnly = ptext SLIT("sat-only")
453 %************************************************************************
455 \subsection[Stg-case-alternatives]{STG case alternatives}
457 %************************************************************************
459 Very like in @CoreSyntax@ (except no type-world stuff).
461 The type constructor is guaranteed not to be abstract; that is, we can
462 see its representation. This is important because the code generator
463 uses it to determine return conventions etc. But it's not trivial
464 where there's a moduule loop involved, because some versions of a type
465 constructor might not have all the constructors visible. So
466 mkStgAlgAlts (in CoreToStg) ensures that it gets the TyCon from the
467 constructors or literals (which are guaranteed to have the Real McCoy)
468 rather than from the scrutinee type.
471 type GenStgAlt bndr occ
472 = (AltCon, -- alts: data constructor,
473 [bndr], -- constructor's parameters,
474 [Bool], -- "use mask", same length as
475 -- parameters; a True in a
476 -- param's position if it is
478 GenStgExpr bndr occ) -- ...right-hand side.
481 = PolyAlt -- Polymorphic (a type variable)
482 | UbxTupAlt TyCon -- Unboxed tuple
483 | AlgAlt TyCon -- Algebraic data type; the AltCons will be DataAlts
484 | PrimAlt TyCon -- Primitive data type; the AltCons will be LitAlts
487 %************************************************************************
489 \subsection[Stg]{The Plain STG parameterisation}
491 %************************************************************************
493 This happens to be the only one we use at the moment.
496 type StgBinding = GenStgBinding Id Id
497 type StgArg = GenStgArg Id
498 type StgLiveVars = GenStgLiveVars Id
499 type StgExpr = GenStgExpr Id Id
500 type StgRhs = GenStgRhs Id Id
501 type StgAlt = GenStgAlt Id Id
504 %************************************************************************
506 \subsubsection[UpdateFlag-datatype]{@UpdateFlag@}
508 %************************************************************************
510 This is also used in @LambdaFormInfo@ in the @ClosureInfo@ module.
512 A @ReEntrant@ closure may be entered multiple times, but should not be
513 updated or blackholed. An @Updatable@ closure should be updated after
514 evaluation (and may be blackholed during evaluation). A @SingleEntry@
515 closure will only be entered once, and so need not be updated but may
516 safely be blackholed.
519 data UpdateFlag = ReEntrant | Updatable | SingleEntry
521 instance Outputable UpdateFlag where
523 = char (case u of { ReEntrant -> 'r'; Updatable -> 'u'; SingleEntry -> 's' })
525 isUpdatable ReEntrant = False
526 isUpdatable SingleEntry = False
527 isUpdatable Updatable = True
530 %************************************************************************
532 \subsubsection{StgOp}
534 %************************************************************************
536 An StgOp allows us to group together PrimOps and ForeignCalls.
537 It's quite useful to move these around together, notably
538 in StgOpApp and COpStmt.
541 data StgOp = StgPrimOp PrimOp
543 | StgFCallOp ForeignCall Unique
544 -- The Unique is occasionally needed by the C pretty-printer
545 -- (which lacks a unique supply), notably when generating a
546 -- typedef for foreign-export-dynamic
550 %************************************************************************
552 \subsubsection[Static Reference Tables]{@SRT@}
554 %************************************************************************
556 There is one SRT per top-level function group. Each local binding and
557 case expression within this binding group has a subrange of the whole
558 SRT, expressed as an offset and length.
560 In CoreToStg we collect the list of CafRefs at each SRT site, which is later
561 converted into the length and offset form by the SRT pass.
566 -- generated by CoreToStg
567 | SRT !Int{-offset-} !Int{-length-} !Bitmap{-bitmap-}
568 -- generated by computeSRTs
573 nonEmptySRT NoSRT = False
574 nonEmptySRT (SRTEntries vs) = not (isEmptyVarSet vs)
577 pprSRT (NoSRT) = ptext SLIT("_no_srt_")
578 pprSRT (SRTEntries ids) = text "SRT:" <> ppr ids
579 pprSRT (SRT off length bitmap) = parens (ppr off <> comma <> text "*bitmap*")
582 %************************************************************************
584 \subsection[Stg-pretty-printing]{Pretty-printing}
586 %************************************************************************
588 Robin Popplestone asked for semi-colon separators on STG binds; here's
589 hoping he likes terminators instead... Ditto for case alternatives.
592 pprGenStgBinding :: (Outputable bndr, Outputable bdee, Ord bdee)
593 => GenStgBinding bndr bdee -> SDoc
595 pprGenStgBinding (StgNonRec bndr rhs)
596 = hang (hsep [ppr bndr, equals])
597 4 ((<>) (ppr rhs) semi)
599 pprGenStgBinding (StgRec pairs)
600 = vcat ((ifPprDebug (ptext SLIT("{- StgRec (begin) -}"))) :
601 (map (ppr_bind) pairs) ++ [(ifPprDebug (ptext SLIT("{- StgRec (end) -}")))])
603 ppr_bind (bndr, expr)
604 = hang (hsep [ppr bndr, equals])
605 4 ((<>) (ppr expr) semi)
607 pprStgBinding :: StgBinding -> SDoc
608 pprStgBinding bind = pprGenStgBinding bind
610 pprStgBindings :: [StgBinding] -> SDoc
611 pprStgBindings binds = vcat (map pprGenStgBinding binds)
613 pprGenStgBindingWithSRT
614 :: (Outputable bndr, Outputable bdee, Ord bdee)
615 => (GenStgBinding bndr bdee,[(Id,[Id])]) -> SDoc
617 pprGenStgBindingWithSRT (bind,srts)
618 = vcat (pprGenStgBinding bind : map pprSRT srts)
619 where pprSRT (id,srt) =
620 ptext SLIT("SRT") <> parens (ppr id) <> ptext SLIT(": ") <> ppr srt
622 pprStgBindingsWithSRTs :: [(StgBinding,[(Id,[Id])])] -> SDoc
623 pprStgBindingsWithSRTs binds = vcat (map pprGenStgBindingWithSRT binds)
627 instance (Outputable bdee) => Outputable (GenStgArg bdee) where
630 instance (Outputable bndr, Outputable bdee, Ord bdee)
631 => Outputable (GenStgBinding bndr bdee) where
632 ppr = pprGenStgBinding
634 instance (Outputable bndr, Outputable bdee, Ord bdee)
635 => Outputable (GenStgExpr bndr bdee) where
638 instance (Outputable bndr, Outputable bdee, Ord bdee)
639 => Outputable (GenStgRhs bndr bdee) where
640 ppr rhs = pprStgRhs rhs
644 pprStgArg :: (Outputable bdee) => GenStgArg bdee -> SDoc
646 pprStgArg (StgVarArg var) = ppr var
647 pprStgArg (StgLitArg con) = ppr con
648 pprStgArg (StgTypeArg ty) = char '@' <+> ppr ty
652 pprStgExpr :: (Outputable bndr, Outputable bdee, Ord bdee)
653 => GenStgExpr bndr bdee -> SDoc
655 pprStgExpr (StgLit lit) = ppr lit
658 pprStgExpr (StgApp func args)
660 4 (sep (map (ppr) args))
664 pprStgExpr (StgConApp con args)
665 = hsep [ ppr con, brackets (interppSP args)]
667 pprStgExpr (StgOpApp op args _)
668 = hsep [ pprStgOp op, brackets (interppSP args)]
670 pprStgExpr (StgLam _ bndrs body)
671 =sep [ char '\\' <+> ppr bndrs <+> ptext SLIT("->"),
676 -- special case: let v = <very specific thing>
682 -- Very special! Suspicious! (SLPJ)
685 pprStgExpr (StgLet srt (StgNonRec bndr (StgRhsClosure cc bi free_vars upd_flag args rhs))
688 (hang (hcat [ptext SLIT("let { "), ppr bndr, ptext SLIT(" = "),
691 ptext SLIT(" ["), ifPprDebug (interppSP free_vars), ptext SLIT("] \\"),
692 ppr upd_flag, ptext SLIT(" ["),
693 interppSP args, char ']'])
694 8 (sep [hsep [ppr rhs, ptext SLIT("} in")]]))
698 -- special case: let ... in let ...
700 pprStgExpr (StgLet bind expr@(StgLet _ _))
702 (sep [hang (ptext SLIT("let {"))
703 2 (hsep [pprGenStgBinding bind, ptext SLIT("} in")])])
707 pprStgExpr (StgLet bind expr)
708 = sep [hang (ptext SLIT("let {")) 2 (pprGenStgBinding bind),
709 hang (ptext SLIT("} in ")) 2 (ppr expr)]
711 pprStgExpr (StgLetNoEscape lvs_whole lvs_rhss bind expr)
712 = sep [hang (ptext SLIT("let-no-escape {"))
713 2 (pprGenStgBinding bind),
714 hang ((<>) (ptext SLIT("} in "))
717 hcat [ptext SLIT("-- lvs: ["), interppSP (uniqSetToList lvs_whole),
718 ptext SLIT("]; rhs lvs: ["), interppSP (uniqSetToList lvs_rhss),
722 pprStgExpr (StgSCC cc expr)
723 = sep [ hsep [ptext SLIT("_scc_"), ppr cc],
726 pprStgExpr (StgCase expr lvs_whole lvs_rhss bndr srt alt_type alts)
727 = sep [sep [ptext SLIT("case"),
728 nest 4 (hsep [pprStgExpr expr,
729 ifPprDebug (dcolon <+> ppr alt_type)]),
730 ptext SLIT("of"), ppr bndr, char '{'],
733 hcat [ptext SLIT("-- lvs: ["), interppSP (uniqSetToList lvs_whole),
734 ptext SLIT("]; rhs lvs: ["), interppSP (uniqSetToList lvs_rhss),
737 nest 2 (vcat (map pprStgAlt alts)),
740 pprStgAlt (con, params, use_mask, expr)
741 = hang (hsep [ppr con, interppSP params, ptext SLIT("->")])
744 pprStgOp (StgPrimOp op) = ppr op
745 pprStgOp (StgFCallOp op _) = ppr op
747 instance Outputable AltType where
748 ppr PolyAlt = ptext SLIT("Polymorphic")
749 ppr (UbxTupAlt tc) = ptext SLIT("UbxTup") <+> ppr tc
750 ppr (AlgAlt tc) = ptext SLIT("Alg") <+> ppr tc
751 ppr (PrimAlt tc) = ptext SLIT("Prim") <+> ppr tc
755 pprStgLVs :: Outputable occ => GenStgLiveVars occ -> SDoc
757 = getPprStyle $ \ sty ->
758 if userStyle sty || isEmptyUniqSet lvs then
761 hcat [text "{-lvs:", interpp'SP (uniqSetToList lvs), text "-}"]
765 pprStgRhs :: (Outputable bndr, Outputable bdee, Ord bdee)
766 => GenStgRhs bndr bdee -> SDoc
769 pprStgRhs (StgRhsClosure cc bi [free_var] upd_flag srt [{-no args-}] (StgApp func []))
772 brackets (ifPprDebug (ppr free_var)),
773 ptext SLIT(" \\"), ppr upd_flag, pprMaybeSRT srt, ptext SLIT(" [] "), ppr func ]
776 pprStgRhs (StgRhsClosure cc bi free_vars upd_flag srt args body)
777 = hang (hsep [if opt_SccProfilingOn then ppr cc else empty,
779 ifPprDebug (brackets (interppSP free_vars)),
780 char '\\' <> ppr upd_flag, pprMaybeSRT srt, brackets (interppSP args)])
783 pprStgRhs (StgRhsCon cc con args)
785 space, ppr con, ptext SLIT("! "), brackets (interppSP args)]
787 pprMaybeSRT (NoSRT) = empty
788 pprMaybeSRT srt = ptext SLIT("srt:") <> pprSRT srt