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,
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 rhsHasCafRefs (StgRhsClosure _ _ _ upd srt _ _)
400 = isUpdatable upd || nonEmptySRT srt
401 rhsHasCafRefs (StgRhsCon _ _ args)
402 = any stgArgHasCafRefs args
404 stgArgHasCafRefs (StgVarArg id) = mayHaveCafRefs (idCafInfo id)
405 stgArgHasCafRefs _ = False
408 Here's the @StgBinderInfo@ type, and its combining op:
412 | SatCallsOnly -- All occurrences are *saturated* *function* calls
413 -- This means we don't need to build an info table and
414 -- slow entry code for the thing
415 -- Thunks never get this value
417 noBinderInfo = NoStgBinderInfo
418 stgUnsatOcc = NoStgBinderInfo
419 stgSatOcc = SatCallsOnly
421 satCallsOnly :: StgBinderInfo -> Bool
422 satCallsOnly SatCallsOnly = True
423 satCallsOnly NoStgBinderInfo = False
425 combineStgBinderInfo :: StgBinderInfo -> StgBinderInfo -> StgBinderInfo
426 combineStgBinderInfo SatCallsOnly SatCallsOnly = SatCallsOnly
427 combineStgBinderInfo info1 info2 = NoStgBinderInfo
430 pp_binder_info NoStgBinderInfo = empty
431 pp_binder_info SatCallsOnly = ptext SLIT("sat-only")
434 %************************************************************************
436 \subsection[Stg-case-alternatives]{STG case alternatives}
438 %************************************************************************
440 Very like in @CoreSyntax@ (except no type-world stuff).
442 The type constructor is guaranteed not to be abstract; that is, we can
443 see its representation. This is important because the code generator
444 uses it to determine return conventions etc. But it's not trivial
445 where there's a moduule loop involved, because some versions of a type
446 constructor might not have all the constructors visible. So
447 mkStgAlgAlts (in CoreToStg) ensures that it gets the TyCon from the
448 constructors or literals (which are guaranteed to have the Real McCoy)
449 rather than from the scrutinee type.
452 type GenStgAlt bndr occ
453 = (AltCon, -- alts: data constructor,
454 [bndr], -- constructor's parameters,
455 [Bool], -- "use mask", same length as
456 -- parameters; a True in a
457 -- param's position if it is
459 GenStgExpr bndr occ) -- ...right-hand side.
462 = PolyAlt -- Polymorphic (a type variable)
463 | UbxTupAlt TyCon -- Unboxed tuple
464 | AlgAlt TyCon -- Algebraic data type; the AltCons will be DataAlts
465 | PrimAlt TyCon -- Primitive data type; the AltCons will be LitAlts
468 %************************************************************************
470 \subsection[Stg]{The Plain STG parameterisation}
472 %************************************************************************
474 This happens to be the only one we use at the moment.
477 type StgBinding = GenStgBinding Id Id
478 type StgArg = GenStgArg Id
479 type StgLiveVars = GenStgLiveVars Id
480 type StgExpr = GenStgExpr Id Id
481 type StgRhs = GenStgRhs Id Id
482 type StgAlt = GenStgAlt Id Id
485 %************************************************************************
487 \subsubsection[UpdateFlag-datatype]{@UpdateFlag@}
489 %************************************************************************
491 This is also used in @LambdaFormInfo@ in the @ClosureInfo@ module.
493 A @ReEntrant@ closure may be entered multiple times, but should not be
494 updated or blackholed. An @Updatable@ closure should be updated after
495 evaluation (and may be blackholed during evaluation). A @SingleEntry@
496 closure will only be entered once, and so need not be updated but may
497 safely be blackholed.
500 data UpdateFlag = ReEntrant | Updatable | SingleEntry
502 instance Outputable UpdateFlag where
504 = char (case u of { ReEntrant -> 'r'; Updatable -> 'u'; SingleEntry -> 's' })
506 isUpdatable ReEntrant = False
507 isUpdatable SingleEntry = False
508 isUpdatable Updatable = True
511 %************************************************************************
513 \subsubsection{StgOp}
515 %************************************************************************
517 An StgOp allows us to group together PrimOps and ForeignCalls.
518 It's quite useful to move these around together, notably
519 in StgOpApp and COpStmt.
522 data StgOp = StgPrimOp PrimOp
524 | StgFCallOp ForeignCall Unique
525 -- The Unique is occasionally needed by the C pretty-printer
526 -- (which lacks a unique supply), notably when generating a
527 -- typedef for foreign-export-dynamic
531 %************************************************************************
533 \subsubsection[Static Reference Tables]{@SRT@}
535 %************************************************************************
537 There is one SRT per top-level function group. Each local binding and
538 case expression within this binding group has a subrange of the whole
539 SRT, expressed as an offset and length.
541 In CoreToStg we collect the list of CafRefs at each SRT site, which is later
542 converted into the length and offset form by the SRT pass.
547 -- generated by CoreToStg
548 | SRT !Int{-offset-} !Int{-length-} !Bitmap{-bitmap-}
549 -- generated by computeSRTs
554 nonEmptySRT NoSRT = False
555 nonEmptySRT (SRTEntries vs) = not (isEmptyVarSet vs)
558 pprSRT (NoSRT) = ptext SLIT("_no_srt_")
559 pprSRT (SRTEntries ids) = text "SRT:" <> ppr ids
560 pprSRT (SRT off length bitmap) = parens (ppr off <> comma <> text "*bitmap*")
563 %************************************************************************
565 \subsection[Stg-pretty-printing]{Pretty-printing}
567 %************************************************************************
569 Robin Popplestone asked for semi-colon separators on STG binds; here's
570 hoping he likes terminators instead... Ditto for case alternatives.
573 pprGenStgBinding :: (Outputable bndr, Outputable bdee, Ord bdee)
574 => GenStgBinding bndr bdee -> SDoc
576 pprGenStgBinding (StgNonRec bndr rhs)
577 = hang (hsep [ppr bndr, equals])
578 4 ((<>) (ppr rhs) semi)
580 pprGenStgBinding (StgRec pairs)
581 = vcat ((ifPprDebug (ptext SLIT("{- StgRec (begin) -}"))) :
582 (map (ppr_bind) pairs) ++ [(ifPprDebug (ptext SLIT("{- StgRec (end) -}")))])
584 ppr_bind (bndr, expr)
585 = hang (hsep [ppr bndr, equals])
586 4 ((<>) (ppr expr) semi)
588 pprStgBinding :: StgBinding -> SDoc
589 pprStgBinding bind = pprGenStgBinding bind
591 pprStgBindings :: [StgBinding] -> SDoc
592 pprStgBindings binds = vcat (map pprGenStgBinding binds)
594 pprGenStgBindingWithSRT
595 :: (Outputable bndr, Outputable bdee, Ord bdee)
596 => (GenStgBinding bndr bdee,[(Id,[Id])]) -> SDoc
598 pprGenStgBindingWithSRT (bind,srts)
599 = vcat (pprGenStgBinding bind : map pprSRT srts)
600 where pprSRT (id,srt) =
601 ptext SLIT("SRT") <> parens (ppr id) <> ptext SLIT(": ") <> ppr srt
603 pprStgBindingsWithSRTs :: [(StgBinding,[(Id,[Id])])] -> SDoc
604 pprStgBindingsWithSRTs binds = vcat (map pprGenStgBindingWithSRT binds)
608 instance (Outputable bdee) => Outputable (GenStgArg bdee) where
611 instance (Outputable bndr, Outputable bdee, Ord bdee)
612 => Outputable (GenStgBinding bndr bdee) where
613 ppr = pprGenStgBinding
615 instance (Outputable bndr, Outputable bdee, Ord bdee)
616 => Outputable (GenStgExpr bndr bdee) where
619 instance (Outputable bndr, Outputable bdee, Ord bdee)
620 => Outputable (GenStgRhs bndr bdee) where
621 ppr rhs = pprStgRhs rhs
625 pprStgArg :: (Outputable bdee) => GenStgArg bdee -> SDoc
627 pprStgArg (StgVarArg var) = ppr var
628 pprStgArg (StgLitArg con) = ppr con
629 pprStgArg (StgTypeArg ty) = char '@' <+> ppr ty
633 pprStgExpr :: (Outputable bndr, Outputable bdee, Ord bdee)
634 => GenStgExpr bndr bdee -> SDoc
636 pprStgExpr (StgLit lit) = ppr lit
639 pprStgExpr (StgApp func args)
641 4 (sep (map (ppr) args))
645 pprStgExpr (StgConApp con args)
646 = hsep [ ppr con, brackets (interppSP args)]
648 pprStgExpr (StgOpApp op args _)
649 = hsep [ pprStgOp op, brackets (interppSP args)]
651 pprStgExpr (StgLam _ bndrs body)
652 =sep [ char '\\' <+> ppr bndrs <+> ptext SLIT("->"),
657 -- special case: let v = <very specific thing>
663 -- Very special! Suspicious! (SLPJ)
666 pprStgExpr (StgLet srt (StgNonRec bndr (StgRhsClosure cc bi free_vars upd_flag args rhs))
669 (hang (hcat [ptext SLIT("let { "), ppr bndr, ptext SLIT(" = "),
672 ptext SLIT(" ["), ifPprDebug (interppSP free_vars), ptext SLIT("] \\"),
673 ppr upd_flag, ptext SLIT(" ["),
674 interppSP args, char ']'])
675 8 (sep [hsep [ppr rhs, ptext SLIT("} in")]]))
679 -- special case: let ... in let ...
681 pprStgExpr (StgLet bind expr@(StgLet _ _))
683 (sep [hang (ptext SLIT("let {"))
684 2 (hsep [pprGenStgBinding bind, ptext SLIT("} in")])])
688 pprStgExpr (StgLet bind expr)
689 = sep [hang (ptext SLIT("let {")) 2 (pprGenStgBinding bind),
690 hang (ptext SLIT("} in ")) 2 (ppr expr)]
692 pprStgExpr (StgLetNoEscape lvs_whole lvs_rhss bind expr)
693 = sep [hang (ptext SLIT("let-no-escape {"))
694 2 (pprGenStgBinding bind),
695 hang ((<>) (ptext SLIT("} in "))
698 hcat [ptext SLIT("-- lvs: ["), interppSP (uniqSetToList lvs_whole),
699 ptext SLIT("]; rhs lvs: ["), interppSP (uniqSetToList lvs_rhss),
703 pprStgExpr (StgSCC cc expr)
704 = sep [ hsep [ptext SLIT("_scc_"), ppr cc],
707 pprStgExpr (StgCase expr lvs_whole lvs_rhss bndr srt alt_type alts)
708 = sep [sep [ptext SLIT("case"),
709 nest 4 (hsep [pprStgExpr expr,
710 ifPprDebug (dcolon <+> ppr alt_type)]),
711 ptext SLIT("of"), ppr bndr, char '{'],
714 hcat [ptext SLIT("-- lvs: ["), interppSP (uniqSetToList lvs_whole),
715 ptext SLIT("]; rhs lvs: ["), interppSP (uniqSetToList lvs_rhss),
718 nest 2 (vcat (map pprStgAlt alts)),
721 pprStgAlt (con, params, use_mask, expr)
722 = hang (hsep [ppr con, interppSP params, ptext SLIT("->")])
725 pprStgOp (StgPrimOp op) = ppr op
726 pprStgOp (StgFCallOp op _) = ppr op
728 instance Outputable AltType where
729 ppr PolyAlt = ptext SLIT("Polymorphic")
730 ppr (UbxTupAlt tc) = ptext SLIT("UbxTup") <+> ppr tc
731 ppr (AlgAlt tc) = ptext SLIT("Alg") <+> ppr tc
732 ppr (PrimAlt tc) = ptext SLIT("Prim") <+> ppr tc
736 pprStgLVs :: Outputable occ => GenStgLiveVars occ -> SDoc
738 = getPprStyle $ \ sty ->
739 if userStyle sty || isEmptyUniqSet lvs then
742 hcat [text "{-lvs:", interpp'SP (uniqSetToList lvs), text "-}"]
746 pprStgRhs :: (Outputable bndr, Outputable bdee, Ord bdee)
747 => GenStgRhs bndr bdee -> SDoc
750 pprStgRhs (StgRhsClosure cc bi [free_var] upd_flag srt [{-no args-}] (StgApp func []))
753 brackets (ifPprDebug (ppr free_var)),
754 ptext SLIT(" \\"), ppr upd_flag, pprMaybeSRT srt, ptext SLIT(" [] "), ppr func ]
757 pprStgRhs (StgRhsClosure cc bi free_vars upd_flag srt args body)
758 = hang (hsep [if opt_SccProfilingOn then ppr cc else empty,
760 ifPprDebug (brackets (interppSP free_vars)),
761 char '\\' <> ppr upd_flag, pprMaybeSRT srt, brackets (interppSP args)])
764 pprStgRhs (StgRhsCon cc con args)
766 space, ppr con, ptext SLIT("! "), brackets (interppSP args)]
768 pprMaybeSRT (NoSRT) = empty
769 pprMaybeSRT srt = ptext SLIT("srt:") <> pprSRT srt