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.
13 -- The above warning supression flag is a temporary kludge.
14 -- While working on this module you are encouraged to remove it and fix
15 -- any warnings in the module. See
16 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
23 GenStgBinding(..), GenStgExpr(..), GenStgRhs(..),
24 GenStgAlt, AltType(..),
26 UpdateFlag(..), isUpdatable,
29 noBinderInfo, stgSatOcc, stgUnsatOcc, satCallsOnly,
32 -- a set of synonyms for the most common (only :-) parameterisation
34 StgBinding, StgExpr, StgRhs, StgAlt,
43 stgBindHasCafRefs, stgArgHasCafRefs, stgRhsArity,
44 isDllConApp, isStgTypeArg,
47 pprStgBinding, pprStgBindings, pprStgBindingsWithSRTs
54 #include "HsVersions.h"
56 import CostCentre ( CostCentreStack, CostCentre )
57 import VarSet ( IdSet, isEmptyVarSet )
59 import Id ( Id, idName, idType, idCafInfo )
60 import IdInfo ( mayHaveCafRefs )
61 import Packages ( isDllName )
62 import PackageConfig ( PackageId )
63 import Literal ( Literal, literalType )
64 import ForeignCall ( ForeignCall )
65 import DataCon ( DataCon, dataConName )
66 import CoreSyn ( AltCon )
67 import PprCore ( {- instances -} )
68 import PrimOp ( PrimOp )
72 import TyCon ( TyCon )
73 import UniqSet ( isEmptyUniqSet, uniqSetToList, UniqSet )
74 import Unique ( Unique )
76 import StaticFlags ( opt_SccProfilingOn )
77 import Module ( Module, pprModule )
80 %************************************************************************
82 \subsection{@GenStgBinding@}
84 %************************************************************************
86 As usual, expressions are interesting; other things are boring. Here
87 are the boring things [except note the @GenStgRhs@], parameterised
88 with respect to binder and occurrence information (just as in
91 There is one SRT for each group of bindings.
94 data GenStgBinding bndr occ
95 = StgNonRec bndr (GenStgRhs bndr occ)
96 | StgRec [(bndr, GenStgRhs bndr occ)]
99 %************************************************************************
101 \subsection{@GenStgArg@}
103 %************************************************************************
109 | StgTypeArg Type -- For when we want to preserve all type info
113 isStgTypeArg (StgTypeArg _) = True
114 isStgTypeArg other = False
116 isDllArg :: PackageId -> StgArg -> Bool
117 -- Does this argument refer to something in a different DLL?
118 isDllArg this_pkg (StgTypeArg v) = False
119 isDllArg this_pkg (StgVarArg v) = isDllName this_pkg (idName v)
120 isDllArg this_pkg (StgLitArg lit) = False
122 isDllConApp :: PackageId -> DataCon -> [StgArg] -> Bool
123 -- Does this constructor application refer to
124 -- anything in a different DLL?
125 -- If so, we can't allocate it statically
126 isDllConApp this_pkg con args
127 = isDllName this_pkg (dataConName con) || any (isDllArg this_pkg) args
129 stgArgType :: StgArg -> Type
130 -- Very half baked becase we have lost the type arguments
131 stgArgType (StgVarArg v) = idType v
132 stgArgType (StgLitArg lit) = literalType lit
133 stgArgType (StgTypeArg lit) = panic "stgArgType called on stgTypeArg"
136 %************************************************************************
138 \subsection{STG expressions}
140 %************************************************************************
142 The @GenStgExpr@ data type is parameterised on binder and occurrence
145 %************************************************************************
147 \subsubsection{@GenStgExpr@ application}
149 %************************************************************************
151 An application is of a function to a list of atoms [not expressions].
152 Operationally, we want to push the arguments on the stack and call the
153 function. (If the arguments were expressions, we would have to build
154 their closures first.)
156 There is no constructor for a lone variable; it would appear as
159 type GenStgLiveVars occ = UniqSet occ
161 data GenStgExpr bndr occ
164 [GenStgArg occ] -- arguments; may be empty
167 %************************************************************************
169 \subsubsection{@StgConApp@ and @StgPrimApp@---saturated applications}
171 %************************************************************************
173 There are a specialised forms of application, for
174 constructors, primitives, and literals.
178 -- StgConApp is vital for returning unboxed tuples
179 -- which can't be let-bound first
181 [GenStgArg occ] -- Saturated
183 | StgOpApp StgOp -- Primitive op or foreign call
184 [GenStgArg occ] -- Saturated
186 -- We need to know this so that we can
187 -- assign result registers
190 %************************************************************************
192 \subsubsection{@StgLam@}
194 %************************************************************************
196 StgLam is used *only* during CoreToStg's work. Before CoreToStg has finished
197 it encodes (\x -> e) as (let f = \x -> e in f)
201 Type -- Type of whole lambda (useful when making a binder for it)
203 StgExpr -- Body of lambda
207 %************************************************************************
209 \subsubsection{@GenStgExpr@: case-expressions}
211 %************************************************************************
213 This has the same boxed/unboxed business as Core case expressions.
216 (GenStgExpr bndr occ)
217 -- the thing to examine
219 (GenStgLiveVars occ) -- Live vars of whole case expression,
220 -- plus everything that happens after the case
221 -- i.e., those which mustn't be overwritten
223 (GenStgLiveVars occ) -- Live vars of RHSs (plus what happens afterwards)
224 -- i.e., those which must be saved before eval.
226 -- note that an alt's constructor's
227 -- binder-variables are NOT counted in the
228 -- free vars for the alt's RHS
230 bndr -- binds the result of evaluating the scrutinee
232 SRT -- The SRT for the continuation
236 [GenStgAlt bndr occ] -- The DEFAULT case is always *first*
237 -- if it is there at all
240 %************************************************************************
242 \subsubsection{@GenStgExpr@: @let(rec)@-expressions}
244 %************************************************************************
246 The various forms of let(rec)-expression encode most of the
247 interesting things we want to do.
251 let-closure x = [free-vars] expr [args]
256 let x = (\free-vars -> \args -> expr) free-vars
258 \tr{args} may be empty (and is for most closures). It isn't under
259 circumstances like this:
265 let-closure x = [z] [y] (y+z)
267 The idea is that we compile code for @(y+z)@ in an environment in which
268 @z@ is bound to an offset from \tr{Node}, and @y@ is bound to an
269 offset from the stack pointer.
271 (A let-closure is an @StgLet@ with a @StgRhsClosure@ RHS.)
275 let-constructor x = Constructor [args]
279 (A let-constructor is an @StgLet@ with a @StgRhsCon@ RHS.)
282 Letrec-expressions are essentially the same deal as
283 let-closure/let-constructor, so we use a common structure and
284 distinguish between them with an @is_recursive@ boolean flag.
288 let-unboxed u = an arbitrary arithmetic expression in unboxed values
291 All the stuff on the RHS must be fully evaluated. No function calls either!
293 (We've backed away from this toward case-expressions with
294 suitably-magical alts ...)
297 ~[Advanced stuff here! Not to start with, but makes pattern matching
298 generate more efficient code.]
301 let-escapes-not fail = expr
304 Here the idea is that @e'@ guarantees not to put @fail@ in a data structure,
305 or pass it to another function. All @e'@ will ever do is tail-call @fail@.
306 Rather than build a closure for @fail@, all we need do is to record the stack
307 level at the moment of the @let-escapes-not@; then entering @fail@ is just
308 a matter of adjusting the stack pointer back down to that point and entering
313 f x y = let z = huge-expression in
319 (A let-escapes-not is an @StgLetNoEscape@.)
322 We may eventually want:
324 let-literal x = Literal
328 (ToDo: is this obsolete?)
331 And so the code for let(rec)-things:
334 (GenStgBinding bndr occ) -- right hand sides (see below)
335 (GenStgExpr bndr occ) -- body
337 | StgLetNoEscape -- remember: ``advanced stuff''
338 (GenStgLiveVars occ) -- Live in the whole let-expression
339 -- Mustn't overwrite these stack slots
340 -- *Doesn't* include binders of the let(rec).
342 (GenStgLiveVars occ) -- Live in the right hand sides (only)
343 -- These are the ones which must be saved on
344 -- the stack if they aren't there already
345 -- *Does* include binders of the let(rec) if recursive.
347 (GenStgBinding bndr occ) -- right hand sides (see below)
348 (GenStgExpr bndr occ) -- body
351 %************************************************************************
353 \subsubsection{@GenStgExpr@: @scc@ expressions}
355 %************************************************************************
357 Finally for @scc@ expressions we introduce a new STG construct.
361 CostCentre -- label of SCC expression
362 (GenStgExpr bndr occ) -- scc expression
365 %************************************************************************
367 \subsubsection{@GenStgExpr@: @hpc@ expressions}
369 %************************************************************************
371 Finally for @scc@ expressions we introduce a new STG construct.
375 Module -- the module of the source of this tick
377 (GenStgExpr bndr occ) -- sub expression
381 %************************************************************************
383 \subsection{STG right-hand sides}
385 %************************************************************************
387 Here's the rest of the interesting stuff for @StgLet@s; the first
388 flavour is for closures:
390 data GenStgRhs bndr occ
392 CostCentreStack -- CCS to be attached (default is CurrentCCS)
393 StgBinderInfo -- Info about how this binder is used (see below)
394 [occ] -- non-global free vars; a list, rather than
395 -- a set, because order is important
396 !UpdateFlag -- ReEntrant | Updatable | SingleEntry
397 SRT -- The SRT reference
398 [bndr] -- arguments; if empty, then not a function;
399 -- as above, order is important.
400 (GenStgExpr bndr occ) -- body
402 An example may be in order. Consider:
404 let t = \x -> \y -> ... x ... y ... p ... q in e
406 Pulling out the free vars and stylising somewhat, we get the equivalent:
408 let t = (\[p,q] -> \[x,y] -> ... x ... y ... p ...q) p q
410 Stg-operationally, the @[x,y]@ are on the stack, the @[p,q]@ are
411 offsets from @Node@ into the closure, and the code ptr for the closure
412 will be exactly that in parentheses above.
414 The second flavour of right-hand-side is for constructors (simple but important):
417 CostCentreStack -- CCS to be attached (default is CurrentCCS).
418 -- Top-level (static) ones will end up with
419 -- DontCareCCS, because we don't count static
420 -- data in heap profiles, and we don't set CCCS
421 -- from static closure.
422 DataCon -- constructor
423 [GenStgArg occ] -- args
427 stgRhsArity :: StgRhs -> Int
428 stgRhsArity (StgRhsClosure _ _ _ _ _ bndrs _) = count isId bndrs
429 -- The arity never includes type parameters, so
430 -- when keeping type arguments and binders in the Stg syntax
431 -- (opt_RuntimeTypes) we have to fliter out the type binders.
432 stgRhsArity (StgRhsCon _ _ _) = 0
436 stgBindHasCafRefs :: GenStgBinding bndr Id -> Bool
437 stgBindHasCafRefs (StgNonRec _ rhs) = rhsHasCafRefs rhs
438 stgBindHasCafRefs (StgRec binds) = any rhsHasCafRefs (map snd binds)
440 rhsHasCafRefs (StgRhsClosure _ _ _ upd srt _ _)
441 = isUpdatable upd || nonEmptySRT srt
442 rhsHasCafRefs (StgRhsCon _ _ args)
443 = any stgArgHasCafRefs args
445 stgArgHasCafRefs (StgVarArg id) = mayHaveCafRefs (idCafInfo id)
446 stgArgHasCafRefs _ = False
449 Here's the @StgBinderInfo@ type, and its combining op:
453 | SatCallsOnly -- All occurrences are *saturated* *function* calls
454 -- This means we don't need to build an info table and
455 -- slow entry code for the thing
456 -- Thunks never get this value
458 noBinderInfo = NoStgBinderInfo
459 stgUnsatOcc = NoStgBinderInfo
460 stgSatOcc = SatCallsOnly
462 satCallsOnly :: StgBinderInfo -> Bool
463 satCallsOnly SatCallsOnly = True
464 satCallsOnly NoStgBinderInfo = False
466 combineStgBinderInfo :: StgBinderInfo -> StgBinderInfo -> StgBinderInfo
467 combineStgBinderInfo SatCallsOnly SatCallsOnly = SatCallsOnly
468 combineStgBinderInfo info1 info2 = NoStgBinderInfo
471 pp_binder_info NoStgBinderInfo = empty
472 pp_binder_info SatCallsOnly = ptext SLIT("sat-only")
475 %************************************************************************
477 \subsection[Stg-case-alternatives]{STG case alternatives}
479 %************************************************************************
481 Very like in @CoreSyntax@ (except no type-world stuff).
483 The type constructor is guaranteed not to be abstract; that is, we can
484 see its representation. This is important because the code generator
485 uses it to determine return conventions etc. But it's not trivial
486 where there's a moduule loop involved, because some versions of a type
487 constructor might not have all the constructors visible. So
488 mkStgAlgAlts (in CoreToStg) ensures that it gets the TyCon from the
489 constructors or literals (which are guaranteed to have the Real McCoy)
490 rather than from the scrutinee type.
493 type GenStgAlt bndr occ
494 = (AltCon, -- alts: data constructor,
495 [bndr], -- constructor's parameters,
496 [Bool], -- "use mask", same length as
497 -- parameters; a True in a
498 -- param's position if it is
500 GenStgExpr bndr occ) -- ...right-hand side.
503 = PolyAlt -- Polymorphic (a type variable)
504 | UbxTupAlt TyCon -- Unboxed tuple
505 | AlgAlt TyCon -- Algebraic data type; the AltCons will be DataAlts
506 | PrimAlt TyCon -- Primitive data type; the AltCons will be LitAlts
509 %************************************************************************
511 \subsection[Stg]{The Plain STG parameterisation}
513 %************************************************************************
515 This happens to be the only one we use at the moment.
518 type StgBinding = GenStgBinding Id Id
519 type StgArg = GenStgArg Id
520 type StgLiveVars = GenStgLiveVars Id
521 type StgExpr = GenStgExpr Id Id
522 type StgRhs = GenStgRhs Id Id
523 type StgAlt = GenStgAlt Id Id
526 %************************************************************************
528 \subsubsection[UpdateFlag-datatype]{@UpdateFlag@}
530 %************************************************************************
532 This is also used in @LambdaFormInfo@ in the @ClosureInfo@ module.
534 A @ReEntrant@ closure may be entered multiple times, but should not be
535 updated or blackholed. An @Updatable@ closure should be updated after
536 evaluation (and may be blackholed during evaluation). A @SingleEntry@
537 closure will only be entered once, and so need not be updated but may
538 safely be blackholed.
541 data UpdateFlag = ReEntrant | Updatable | SingleEntry
543 instance Outputable UpdateFlag where
545 = char (case u of { ReEntrant -> 'r'; Updatable -> 'u'; SingleEntry -> 's' })
547 isUpdatable ReEntrant = False
548 isUpdatable SingleEntry = False
549 isUpdatable Updatable = True
552 %************************************************************************
554 \subsubsection{StgOp}
556 %************************************************************************
558 An StgOp allows us to group together PrimOps and ForeignCalls.
559 It's quite useful to move these around together, notably
560 in StgOpApp and COpStmt.
563 data StgOp = StgPrimOp PrimOp
565 | StgFCallOp ForeignCall Unique
566 -- The Unique is occasionally needed by the C pretty-printer
567 -- (which lacks a unique supply), notably when generating a
568 -- typedef for foreign-export-dynamic
572 %************************************************************************
574 \subsubsection[Static Reference Tables]{@SRT@}
576 %************************************************************************
578 There is one SRT per top-level function group. Each local binding and
579 case expression within this binding group has a subrange of the whole
580 SRT, expressed as an offset and length.
582 In CoreToStg we collect the list of CafRefs at each SRT site, which is later
583 converted into the length and offset form by the SRT pass.
588 -- generated by CoreToStg
589 | SRT !Int{-offset-} !Int{-length-} !Bitmap{-bitmap-}
590 -- generated by computeSRTs
595 nonEmptySRT NoSRT = False
596 nonEmptySRT (SRTEntries vs) = not (isEmptyVarSet vs)
599 pprSRT (NoSRT) = ptext SLIT("_no_srt_")
600 pprSRT (SRTEntries ids) = text "SRT:" <> ppr ids
601 pprSRT (SRT off length bitmap) = parens (ppr off <> comma <> text "*bitmap*")
604 %************************************************************************
606 \subsection[Stg-pretty-printing]{Pretty-printing}
608 %************************************************************************
610 Robin Popplestone asked for semi-colon separators on STG binds; here's
611 hoping he likes terminators instead... Ditto for case alternatives.
614 pprGenStgBinding :: (Outputable bndr, Outputable bdee, Ord bdee)
615 => GenStgBinding bndr bdee -> SDoc
617 pprGenStgBinding (StgNonRec bndr rhs)
618 = hang (hsep [ppr bndr, equals])
619 4 ((<>) (ppr rhs) semi)
621 pprGenStgBinding (StgRec pairs)
622 = vcat ((ifPprDebug (ptext SLIT("{- StgRec (begin) -}"))) :
623 (map (ppr_bind) pairs) ++ [(ifPprDebug (ptext SLIT("{- StgRec (end) -}")))])
625 ppr_bind (bndr, expr)
626 = hang (hsep [ppr bndr, equals])
627 4 ((<>) (ppr expr) semi)
629 pprStgBinding :: StgBinding -> SDoc
630 pprStgBinding bind = pprGenStgBinding bind
632 pprStgBindings :: [StgBinding] -> SDoc
633 pprStgBindings binds = vcat (map pprGenStgBinding binds)
635 pprGenStgBindingWithSRT
636 :: (Outputable bndr, Outputable bdee, Ord bdee)
637 => (GenStgBinding bndr bdee,[(Id,[Id])]) -> SDoc
639 pprGenStgBindingWithSRT (bind,srts)
640 = vcat (pprGenStgBinding bind : map pprSRT srts)
641 where pprSRT (id,srt) =
642 ptext SLIT("SRT") <> parens (ppr id) <> ptext SLIT(": ") <> ppr srt
644 pprStgBindingsWithSRTs :: [(StgBinding,[(Id,[Id])])] -> SDoc
645 pprStgBindingsWithSRTs binds = vcat (map pprGenStgBindingWithSRT binds)
649 instance (Outputable bdee) => Outputable (GenStgArg bdee) where
652 instance (Outputable bndr, Outputable bdee, Ord bdee)
653 => Outputable (GenStgBinding bndr bdee) where
654 ppr = pprGenStgBinding
656 instance (Outputable bndr, Outputable bdee, Ord bdee)
657 => Outputable (GenStgExpr bndr bdee) where
660 instance (Outputable bndr, Outputable bdee, Ord bdee)
661 => Outputable (GenStgRhs bndr bdee) where
662 ppr rhs = pprStgRhs rhs
666 pprStgArg :: (Outputable bdee) => GenStgArg bdee -> SDoc
668 pprStgArg (StgVarArg var) = ppr var
669 pprStgArg (StgLitArg con) = ppr con
670 pprStgArg (StgTypeArg ty) = char '@' <+> ppr ty
674 pprStgExpr :: (Outputable bndr, Outputable bdee, Ord bdee)
675 => GenStgExpr bndr bdee -> SDoc
677 pprStgExpr (StgLit lit) = ppr lit
680 pprStgExpr (StgApp func args)
682 4 (sep (map (ppr) args))
686 pprStgExpr (StgConApp con args)
687 = hsep [ ppr con, brackets (interppSP args)]
689 pprStgExpr (StgOpApp op args _)
690 = hsep [ pprStgOp op, brackets (interppSP args)]
692 pprStgExpr (StgLam _ bndrs body)
693 =sep [ char '\\' <+> ppr bndrs <+> ptext SLIT("->"),
698 -- special case: let v = <very specific thing>
704 -- Very special! Suspicious! (SLPJ)
707 pprStgExpr (StgLet srt (StgNonRec bndr (StgRhsClosure cc bi free_vars upd_flag args rhs))
710 (hang (hcat [ptext SLIT("let { "), ppr bndr, ptext SLIT(" = "),
713 ptext SLIT(" ["), ifPprDebug (interppSP free_vars), ptext SLIT("] \\"),
714 ppr upd_flag, ptext SLIT(" ["),
715 interppSP args, char ']'])
716 8 (sep [hsep [ppr rhs, ptext SLIT("} in")]]))
720 -- special case: let ... in let ...
722 pprStgExpr (StgLet bind expr@(StgLet _ _))
724 (sep [hang (ptext SLIT("let {"))
725 2 (hsep [pprGenStgBinding bind, ptext SLIT("} in")])])
729 pprStgExpr (StgLet bind expr)
730 = sep [hang (ptext SLIT("let {")) 2 (pprGenStgBinding bind),
731 hang (ptext SLIT("} in ")) 2 (ppr expr)]
733 pprStgExpr (StgLetNoEscape lvs_whole lvs_rhss bind expr)
734 = sep [hang (ptext SLIT("let-no-escape {"))
735 2 (pprGenStgBinding bind),
736 hang ((<>) (ptext SLIT("} in "))
739 hcat [ptext SLIT("-- lvs: ["), interppSP (uniqSetToList lvs_whole),
740 ptext SLIT("]; rhs lvs: ["), interppSP (uniqSetToList lvs_rhss),
744 pprStgExpr (StgSCC cc expr)
745 = sep [ hsep [ptext SLIT("_scc_"), ppr cc],
748 pprStgExpr (StgTick m n expr)
749 = sep [ hsep [ptext SLIT("_tick_"), pprModule m,text (show n)],
752 pprStgExpr (StgCase expr lvs_whole lvs_rhss bndr srt alt_type alts)
753 = sep [sep [ptext SLIT("case"),
754 nest 4 (hsep [pprStgExpr expr,
755 ifPprDebug (dcolon <+> ppr alt_type)]),
756 ptext SLIT("of"), ppr bndr, char '{'],
759 hcat [ptext SLIT("-- lvs: ["), interppSP (uniqSetToList lvs_whole),
760 ptext SLIT("]; rhs lvs: ["), interppSP (uniqSetToList lvs_rhss),
763 nest 2 (vcat (map pprStgAlt alts)),
766 pprStgAlt (con, params, use_mask, expr)
767 = hang (hsep [ppr con, interppSP params, ptext SLIT("->")])
770 pprStgOp (StgPrimOp op) = ppr op
771 pprStgOp (StgFCallOp op _) = ppr op
773 instance Outputable AltType where
774 ppr PolyAlt = ptext SLIT("Polymorphic")
775 ppr (UbxTupAlt tc) = ptext SLIT("UbxTup") <+> ppr tc
776 ppr (AlgAlt tc) = ptext SLIT("Alg") <+> ppr tc
777 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 "-}"]
791 pprStgRhs :: (Outputable bndr, Outputable bdee, Ord bdee)
792 => GenStgRhs bndr bdee -> SDoc
795 pprStgRhs (StgRhsClosure cc bi [free_var] upd_flag srt [{-no args-}] (StgApp func []))
798 brackets (ifPprDebug (ppr free_var)),
799 ptext SLIT(" \\"), ppr upd_flag, pprMaybeSRT srt, ptext SLIT(" [] "), ppr func ]
802 pprStgRhs (StgRhsClosure cc bi free_vars upd_flag srt args body)
803 = hang (hsep [if opt_SccProfilingOn then ppr cc else empty,
805 ifPprDebug (brackets (interppSP free_vars)),
806 char '\\' <> ppr upd_flag, pprMaybeSRT srt, brackets (interppSP args)])
809 pprStgRhs (StgRhsCon cc con args)
811 space, ppr con, ptext SLIT("! "), brackets (interppSP args)]
813 pprMaybeSRT (NoSRT) = empty
814 pprMaybeSRT srt = ptext SLIT("srt:") <> pprSRT srt