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 )
65 import UniqSet ( isEmptyUniqSet, uniqSetToList, UniqSet )
66 import Unique ( Unique )
68 import DynFlags ( DynFlags )
69 import Packages ( HomeModules )
70 import StaticFlags ( opt_SccProfilingOn )
73 %************************************************************************
75 \subsection{@GenStgBinding@}
77 %************************************************************************
79 As usual, expressions are interesting; other things are boring. Here
80 are the boring things [except note the @GenStgRhs@], parameterised
81 with respect to binder and occurrence information (just as in
84 There is one SRT for each group of bindings.
87 data GenStgBinding bndr occ
88 = StgNonRec bndr (GenStgRhs bndr occ)
89 | StgRec [(bndr, GenStgRhs bndr occ)]
92 %************************************************************************
94 \subsection{@GenStgArg@}
96 %************************************************************************
102 | StgTypeArg Type -- For when we want to preserve all type info
106 isStgTypeArg (StgTypeArg _) = True
107 isStgTypeArg other = False
109 isDllArg :: HomeModules -> StgArg -> Bool
110 -- Does this argument refer to something in a different DLL?
111 isDllArg hmods (StgTypeArg v) = False
112 isDllArg hmods (StgVarArg v) = isDllName hmods (idName v)
113 isDllArg hmods (StgLitArg lit) = False
115 isDllConApp :: HomeModules -> DataCon -> [StgArg] -> Bool
116 -- Does this constructor application refer to
117 -- anything in a different DLL?
118 -- If so, we can't allocate it statically
119 isDllConApp hmods con args
120 = isDllName hmods (dataConName con) || any (isDllArg hmods) args
122 stgArgType :: StgArg -> Type
123 -- Very half baked becase we have lost the type arguments
124 stgArgType (StgVarArg v) = idType v
125 stgArgType (StgLitArg lit) = literalType lit
126 stgArgType (StgTypeArg lit) = panic "stgArgType called on stgTypeArg"
129 %************************************************************************
131 \subsection{STG expressions}
133 %************************************************************************
135 The @GenStgExpr@ data type is parameterised on binder and occurrence
138 %************************************************************************
140 \subsubsection{@GenStgExpr@ application}
142 %************************************************************************
144 An application is of a function to a list of atoms [not expressions].
145 Operationally, we want to push the arguments on the stack and call the
146 function. (If the arguments were expressions, we would have to build
147 their closures first.)
149 There is no constructor for a lone variable; it would appear as
152 type GenStgLiveVars occ = UniqSet occ
154 data GenStgExpr bndr occ
157 [GenStgArg occ] -- arguments; may be empty
160 %************************************************************************
162 \subsubsection{@StgConApp@ and @StgPrimApp@---saturated applications}
164 %************************************************************************
166 There are a specialised forms of application, for
167 constructors, primitives, and literals.
172 [GenStgArg occ] -- Saturated
174 | StgOpApp StgOp -- Primitive op or foreign call
175 [GenStgArg occ] -- Saturated
176 Type -- Result type; we need to know the result type
177 -- so that we can assign result registers.
180 %************************************************************************
182 \subsubsection{@StgLam@}
184 %************************************************************************
186 StgLam is used *only* during CoreToStg's work. Before CoreToStg has finished
187 it encodes (\x -> e) as (let f = \x -> e in f)
191 Type -- Type of whole lambda (useful when making a binder for it)
193 StgExpr -- Body of lambda
197 %************************************************************************
199 \subsubsection{@GenStgExpr@: case-expressions}
201 %************************************************************************
203 This has the same boxed/unboxed business as Core case expressions.
206 (GenStgExpr bndr occ)
207 -- the thing to examine
209 (GenStgLiveVars occ) -- Live vars of whole case expression,
210 -- plus everything that happens after the case
211 -- i.e., those which mustn't be overwritten
213 (GenStgLiveVars occ) -- Live vars of RHSs (plus what happens afterwards)
214 -- i.e., those which must be saved before eval.
216 -- note that an alt's constructor's
217 -- binder-variables are NOT counted in the
218 -- free vars for the alt's RHS
220 bndr -- binds the result of evaluating the scrutinee
222 SRT -- The SRT for the continuation
226 [GenStgAlt bndr occ] -- The DEFAULT case is always *first*
227 -- if it is there at all
230 %************************************************************************
232 \subsubsection{@GenStgExpr@: @let(rec)@-expressions}
234 %************************************************************************
236 The various forms of let(rec)-expression encode most of the
237 interesting things we want to do.
241 let-closure x = [free-vars] expr [args]
246 let x = (\free-vars -> \args -> expr) free-vars
248 \tr{args} may be empty (and is for most closures). It isn't under
249 circumstances like this:
255 let-closure x = [z] [y] (y+z)
257 The idea is that we compile code for @(y+z)@ in an environment in which
258 @z@ is bound to an offset from \tr{Node}, and @y@ is bound to an
259 offset from the stack pointer.
261 (A let-closure is an @StgLet@ with a @StgRhsClosure@ RHS.)
265 let-constructor x = Constructor [args]
269 (A let-constructor is an @StgLet@ with a @StgRhsCon@ RHS.)
272 Letrec-expressions are essentially the same deal as
273 let-closure/let-constructor, so we use a common structure and
274 distinguish between them with an @is_recursive@ boolean flag.
278 let-unboxed u = an arbitrary arithmetic expression in unboxed values
281 All the stuff on the RHS must be fully evaluated. No function calls either!
283 (We've backed away from this toward case-expressions with
284 suitably-magical alts ...)
287 ~[Advanced stuff here! Not to start with, but makes pattern matching
288 generate more efficient code.]
291 let-escapes-not fail = expr
294 Here the idea is that @e'@ guarantees not to put @fail@ in a data structure,
295 or pass it to another function. All @e'@ will ever do is tail-call @fail@.
296 Rather than build a closure for @fail@, all we need do is to record the stack
297 level at the moment of the @let-escapes-not@; then entering @fail@ is just
298 a matter of adjusting the stack pointer back down to that point and entering
303 f x y = let z = huge-expression in
309 (A let-escapes-not is an @StgLetNoEscape@.)
312 We may eventually want:
314 let-literal x = Literal
318 (ToDo: is this obsolete?)
321 And so the code for let(rec)-things:
324 (GenStgBinding bndr occ) -- right hand sides (see below)
325 (GenStgExpr bndr occ) -- body
327 | StgLetNoEscape -- remember: ``advanced stuff''
328 (GenStgLiveVars occ) -- Live in the whole let-expression
329 -- Mustn't overwrite these stack slots
330 -- *Doesn't* include binders of the let(rec).
332 (GenStgLiveVars occ) -- Live in the right hand sides (only)
333 -- These are the ones which must be saved on
334 -- the stack if they aren't there already
335 -- *Does* include binders of the let(rec) if recursive.
337 (GenStgBinding bndr occ) -- right hand sides (see below)
338 (GenStgExpr bndr occ) -- body
341 %************************************************************************
343 \subsubsection{@GenStgExpr@: @scc@ expressions}
345 %************************************************************************
347 Finally for @scc@ expressions we introduce a new STG construct.
351 CostCentre -- label of SCC expression
352 (GenStgExpr bndr occ) -- scc expression
356 %************************************************************************
358 \subsection{STG right-hand sides}
360 %************************************************************************
362 Here's the rest of the interesting stuff for @StgLet@s; the first
363 flavour is for closures:
365 data GenStgRhs bndr occ
367 CostCentreStack -- CCS to be attached (default is CurrentCCS)
368 StgBinderInfo -- Info about how this binder is used (see below)
369 [occ] -- non-global free vars; a list, rather than
370 -- a set, because order is important
371 !UpdateFlag -- ReEntrant | Updatable | SingleEntry
372 SRT -- The SRT reference
373 [bndr] -- arguments; if empty, then not a function;
374 -- as above, order is important.
375 (GenStgExpr bndr occ) -- body
377 An example may be in order. Consider:
379 let t = \x -> \y -> ... x ... y ... p ... q in e
381 Pulling out the free vars and stylising somewhat, we get the equivalent:
383 let t = (\[p,q] -> \[x,y] -> ... x ... y ... p ...q) p q
385 Stg-operationally, the @[x,y]@ are on the stack, the @[p,q]@ are
386 offsets from @Node@ into the closure, and the code ptr for the closure
387 will be exactly that in parentheses above.
389 The second flavour of right-hand-side is for constructors (simple but important):
392 CostCentreStack -- CCS to be attached (default is CurrentCCS).
393 -- Top-level (static) ones will end up with
394 -- DontCareCCS, because we don't count static
395 -- data in heap profiles, and we don't set CCCS
396 -- from static closure.
397 DataCon -- constructor
398 [GenStgArg occ] -- args
402 stgRhsArity :: StgRhs -> Int
403 stgRhsArity (StgRhsClosure _ _ _ _ _ bndrs _) = count isId bndrs
404 -- The arity never includes type parameters, so
405 -- when keeping type arguments and binders in the Stg syntax
406 -- (opt_RuntimeTypes) we have to fliter out the type binders.
407 stgRhsArity (StgRhsCon _ _ _) = 0
411 stgBindHasCafRefs :: GenStgBinding bndr Id -> Bool
412 stgBindHasCafRefs (StgNonRec _ rhs) = rhsHasCafRefs rhs
413 stgBindHasCafRefs (StgRec binds) = any rhsHasCafRefs (map snd binds)
415 rhsHasCafRefs (StgRhsClosure _ _ _ upd srt _ _)
416 = isUpdatable upd || nonEmptySRT srt
417 rhsHasCafRefs (StgRhsCon _ _ args)
418 = any stgArgHasCafRefs args
420 stgArgHasCafRefs (StgVarArg id) = mayHaveCafRefs (idCafInfo id)
421 stgArgHasCafRefs _ = False
424 Here's the @StgBinderInfo@ type, and its combining op:
428 | SatCallsOnly -- All occurrences are *saturated* *function* calls
429 -- This means we don't need to build an info table and
430 -- slow entry code for the thing
431 -- Thunks never get this value
433 noBinderInfo = NoStgBinderInfo
434 stgUnsatOcc = NoStgBinderInfo
435 stgSatOcc = SatCallsOnly
437 satCallsOnly :: StgBinderInfo -> Bool
438 satCallsOnly SatCallsOnly = True
439 satCallsOnly NoStgBinderInfo = False
441 combineStgBinderInfo :: StgBinderInfo -> StgBinderInfo -> StgBinderInfo
442 combineStgBinderInfo SatCallsOnly SatCallsOnly = SatCallsOnly
443 combineStgBinderInfo info1 info2 = NoStgBinderInfo
446 pp_binder_info NoStgBinderInfo = empty
447 pp_binder_info SatCallsOnly = ptext SLIT("sat-only")
450 %************************************************************************
452 \subsection[Stg-case-alternatives]{STG case alternatives}
454 %************************************************************************
456 Very like in @CoreSyntax@ (except no type-world stuff).
458 The type constructor is guaranteed not to be abstract; that is, we can
459 see its representation. This is important because the code generator
460 uses it to determine return conventions etc. But it's not trivial
461 where there's a moduule loop involved, because some versions of a type
462 constructor might not have all the constructors visible. So
463 mkStgAlgAlts (in CoreToStg) ensures that it gets the TyCon from the
464 constructors or literals (which are guaranteed to have the Real McCoy)
465 rather than from the scrutinee type.
468 type GenStgAlt bndr occ
469 = (AltCon, -- alts: data constructor,
470 [bndr], -- constructor's parameters,
471 [Bool], -- "use mask", same length as
472 -- parameters; a True in a
473 -- param's position if it is
475 GenStgExpr bndr occ) -- ...right-hand side.
478 = PolyAlt -- Polymorphic (a type variable)
479 | UbxTupAlt TyCon -- Unboxed tuple
480 | AlgAlt TyCon -- Algebraic data type; the AltCons will be DataAlts
481 | PrimAlt TyCon -- Primitive data type; the AltCons will be LitAlts
484 %************************************************************************
486 \subsection[Stg]{The Plain STG parameterisation}
488 %************************************************************************
490 This happens to be the only one we use at the moment.
493 type StgBinding = GenStgBinding Id Id
494 type StgArg = GenStgArg Id
495 type StgLiveVars = GenStgLiveVars Id
496 type StgExpr = GenStgExpr Id Id
497 type StgRhs = GenStgRhs Id Id
498 type StgAlt = GenStgAlt Id Id
501 %************************************************************************
503 \subsubsection[UpdateFlag-datatype]{@UpdateFlag@}
505 %************************************************************************
507 This is also used in @LambdaFormInfo@ in the @ClosureInfo@ module.
509 A @ReEntrant@ closure may be entered multiple times, but should not be
510 updated or blackholed. An @Updatable@ closure should be updated after
511 evaluation (and may be blackholed during evaluation). A @SingleEntry@
512 closure will only be entered once, and so need not be updated but may
513 safely be blackholed.
516 data UpdateFlag = ReEntrant | Updatable | SingleEntry
518 instance Outputable UpdateFlag where
520 = char (case u of { ReEntrant -> 'r'; Updatable -> 'u'; SingleEntry -> 's' })
522 isUpdatable ReEntrant = False
523 isUpdatable SingleEntry = False
524 isUpdatable Updatable = True
527 %************************************************************************
529 \subsubsection{StgOp}
531 %************************************************************************
533 An StgOp allows us to group together PrimOps and ForeignCalls.
534 It's quite useful to move these around together, notably
535 in StgOpApp and COpStmt.
538 data StgOp = StgPrimOp PrimOp
540 | StgFCallOp ForeignCall Unique
541 -- The Unique is occasionally needed by the C pretty-printer
542 -- (which lacks a unique supply), notably when generating a
543 -- typedef for foreign-export-dynamic
547 %************************************************************************
549 \subsubsection[Static Reference Tables]{@SRT@}
551 %************************************************************************
553 There is one SRT per top-level function group. Each local binding and
554 case expression within this binding group has a subrange of the whole
555 SRT, expressed as an offset and length.
557 In CoreToStg we collect the list of CafRefs at each SRT site, which is later
558 converted into the length and offset form by the SRT pass.
563 -- generated by CoreToStg
564 | SRT !Int{-offset-} !Int{-length-} !Bitmap{-bitmap-}
565 -- generated by computeSRTs
570 nonEmptySRT NoSRT = False
571 nonEmptySRT (SRTEntries vs) = not (isEmptyVarSet vs)
574 pprSRT (NoSRT) = ptext SLIT("_no_srt_")
575 pprSRT (SRTEntries ids) = text "SRT:" <> ppr ids
576 pprSRT (SRT off length bitmap) = parens (ppr off <> comma <> text "*bitmap*")
579 %************************************************************************
581 \subsection[Stg-pretty-printing]{Pretty-printing}
583 %************************************************************************
585 Robin Popplestone asked for semi-colon separators on STG binds; here's
586 hoping he likes terminators instead... Ditto for case alternatives.
589 pprGenStgBinding :: (Outputable bndr, Outputable bdee, Ord bdee)
590 => GenStgBinding bndr bdee -> SDoc
592 pprGenStgBinding (StgNonRec bndr rhs)
593 = hang (hsep [ppr bndr, equals])
594 4 ((<>) (ppr rhs) semi)
596 pprGenStgBinding (StgRec pairs)
597 = vcat ((ifPprDebug (ptext SLIT("{- StgRec (begin) -}"))) :
598 (map (ppr_bind) pairs) ++ [(ifPprDebug (ptext SLIT("{- StgRec (end) -}")))])
600 ppr_bind (bndr, expr)
601 = hang (hsep [ppr bndr, equals])
602 4 ((<>) (ppr expr) semi)
604 pprStgBinding :: StgBinding -> SDoc
605 pprStgBinding bind = pprGenStgBinding bind
607 pprStgBindings :: [StgBinding] -> SDoc
608 pprStgBindings binds = vcat (map pprGenStgBinding binds)
610 pprGenStgBindingWithSRT
611 :: (Outputable bndr, Outputable bdee, Ord bdee)
612 => (GenStgBinding bndr bdee,[(Id,[Id])]) -> SDoc
614 pprGenStgBindingWithSRT (bind,srts)
615 = vcat (pprGenStgBinding bind : map pprSRT srts)
616 where pprSRT (id,srt) =
617 ptext SLIT("SRT") <> parens (ppr id) <> ptext SLIT(": ") <> ppr srt
619 pprStgBindingsWithSRTs :: [(StgBinding,[(Id,[Id])])] -> SDoc
620 pprStgBindingsWithSRTs binds = vcat (map pprGenStgBindingWithSRT binds)
624 instance (Outputable bdee) => Outputable (GenStgArg bdee) where
627 instance (Outputable bndr, Outputable bdee, Ord bdee)
628 => Outputable (GenStgBinding bndr bdee) where
629 ppr = pprGenStgBinding
631 instance (Outputable bndr, Outputable bdee, Ord bdee)
632 => Outputable (GenStgExpr bndr bdee) where
635 instance (Outputable bndr, Outputable bdee, Ord bdee)
636 => Outputable (GenStgRhs bndr bdee) where
637 ppr rhs = pprStgRhs rhs
641 pprStgArg :: (Outputable bdee) => GenStgArg bdee -> SDoc
643 pprStgArg (StgVarArg var) = ppr var
644 pprStgArg (StgLitArg con) = ppr con
645 pprStgArg (StgTypeArg ty) = char '@' <+> ppr ty
649 pprStgExpr :: (Outputable bndr, Outputable bdee, Ord bdee)
650 => GenStgExpr bndr bdee -> SDoc
652 pprStgExpr (StgLit lit) = ppr lit
655 pprStgExpr (StgApp func args)
657 4 (sep (map (ppr) args))
661 pprStgExpr (StgConApp con args)
662 = hsep [ ppr con, brackets (interppSP args)]
664 pprStgExpr (StgOpApp op args _)
665 = hsep [ pprStgOp op, brackets (interppSP args)]
667 pprStgExpr (StgLam _ bndrs body)
668 =sep [ char '\\' <+> ppr bndrs <+> ptext SLIT("->"),
673 -- special case: let v = <very specific thing>
679 -- Very special! Suspicious! (SLPJ)
682 pprStgExpr (StgLet srt (StgNonRec bndr (StgRhsClosure cc bi free_vars upd_flag args rhs))
685 (hang (hcat [ptext SLIT("let { "), ppr bndr, ptext SLIT(" = "),
688 ptext SLIT(" ["), ifPprDebug (interppSP free_vars), ptext SLIT("] \\"),
689 ppr upd_flag, ptext SLIT(" ["),
690 interppSP args, char ']'])
691 8 (sep [hsep [ppr rhs, ptext SLIT("} in")]]))
695 -- special case: let ... in let ...
697 pprStgExpr (StgLet bind expr@(StgLet _ _))
699 (sep [hang (ptext SLIT("let {"))
700 2 (hsep [pprGenStgBinding bind, ptext SLIT("} in")])])
704 pprStgExpr (StgLet bind expr)
705 = sep [hang (ptext SLIT("let {")) 2 (pprGenStgBinding bind),
706 hang (ptext SLIT("} in ")) 2 (ppr expr)]
708 pprStgExpr (StgLetNoEscape lvs_whole lvs_rhss bind expr)
709 = sep [hang (ptext SLIT("let-no-escape {"))
710 2 (pprGenStgBinding bind),
711 hang ((<>) (ptext SLIT("} in "))
714 hcat [ptext SLIT("-- lvs: ["), interppSP (uniqSetToList lvs_whole),
715 ptext SLIT("]; rhs lvs: ["), interppSP (uniqSetToList lvs_rhss),
719 pprStgExpr (StgSCC cc expr)
720 = sep [ hsep [ptext SLIT("_scc_"), ppr cc],
723 pprStgExpr (StgCase expr lvs_whole lvs_rhss bndr srt alt_type alts)
724 = sep [sep [ptext SLIT("case"),
725 nest 4 (hsep [pprStgExpr expr,
726 ifPprDebug (dcolon <+> ppr alt_type)]),
727 ptext SLIT("of"), ppr bndr, char '{'],
730 hcat [ptext SLIT("-- lvs: ["), interppSP (uniqSetToList lvs_whole),
731 ptext SLIT("]; rhs lvs: ["), interppSP (uniqSetToList lvs_rhss),
734 nest 2 (vcat (map pprStgAlt alts)),
737 pprStgAlt (con, params, use_mask, expr)
738 = hang (hsep [ppr con, interppSP params, ptext SLIT("->")])
741 pprStgOp (StgPrimOp op) = ppr op
742 pprStgOp (StgFCallOp op _) = ppr op
744 instance Outputable AltType where
745 ppr PolyAlt = ptext SLIT("Polymorphic")
746 ppr (UbxTupAlt tc) = ptext SLIT("UbxTup") <+> ppr tc
747 ppr (AlgAlt tc) = ptext SLIT("Alg") <+> ppr tc
748 ppr (PrimAlt tc) = ptext SLIT("Prim") <+> ppr tc
752 pprStgLVs :: Outputable occ => GenStgLiveVars occ -> SDoc
754 = getPprStyle $ \ sty ->
755 if userStyle sty || isEmptyUniqSet lvs then
758 hcat [text "{-lvs:", interpp'SP (uniqSetToList lvs), text "-}"]
762 pprStgRhs :: (Outputable bndr, Outputable bdee, Ord bdee)
763 => GenStgRhs bndr bdee -> SDoc
766 pprStgRhs (StgRhsClosure cc bi [free_var] upd_flag srt [{-no args-}] (StgApp func []))
769 brackets (ifPprDebug (ppr free_var)),
770 ptext SLIT(" \\"), ppr upd_flag, pprMaybeSRT srt, ptext SLIT(" [] "), ppr func ]
773 pprStgRhs (StgRhsClosure cc bi free_vars upd_flag srt args body)
774 = hang (hsep [if opt_SccProfilingOn then ppr cc else empty,
776 ifPprDebug (brackets (interppSP free_vars)),
777 char '\\' <> ppr upd_flag, pprMaybeSRT srt, brackets (interppSP args)])
780 pprStgRhs (StgRhsCon cc con args)
782 space, ppr con, ptext SLIT("! "), brackets (interppSP args)]
784 pprMaybeSRT (NoSRT) = empty
785 pprMaybeSRT srt = ptext SLIT("srt:") <> pprSRT srt