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 import CostCentre ( CostCentreStack, CostCentre )
48 import VarSet ( IdSet, isEmptyVarSet )
50 import Id ( Id, idName, idType, idCafInfo )
51 import IdInfo ( mayHaveCafRefs )
52 import Packages ( isDllName )
53 import Literal ( Literal, literalType )
54 import ForeignCall ( ForeignCall )
55 import DataCon ( DataCon, dataConName )
56 import CoreSyn ( AltCon )
57 import PprCore ( {- instances -} )
58 import PrimOp ( PrimOp )
61 import TyCon ( TyCon )
63 import Unique ( Unique )
65 import StaticFlags ( 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)]
89 %************************************************************************
91 \subsection{@GenStgArg@}
93 %************************************************************************
99 | StgTypeArg Type -- For when we want to preserve all type info
103 isStgTypeArg :: StgArg -> Bool
104 isStgTypeArg (StgTypeArg _) = True
105 isStgTypeArg _ = False
107 isDllArg :: PackageId -> StgArg -> Bool
108 -- Does this argument refer to something in a different DLL?
109 isDllArg this_pkg (StgVarArg v) = isDllName this_pkg (idName v)
112 isDllConApp :: PackageId -> DataCon -> [StgArg] -> Bool
113 -- Does this constructor application refer to
114 -- anything in a different DLL?
115 -- If so, we can't allocate it statically
116 isDllConApp this_pkg con args
117 = isDllName this_pkg (dataConName con) || any (isDllArg this_pkg) 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 _) = 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.
168 -- StgConApp is vital for returning unboxed tuples
169 -- which can't be let-bound first
171 [GenStgArg occ] -- Saturated
173 | StgOpApp StgOp -- Primitive op or foreign call
174 [GenStgArg occ] -- Saturated
176 -- We need to know this so that we can
177 -- 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
355 %************************************************************************
357 \subsubsection{@GenStgExpr@: @hpc@ expressions}
359 %************************************************************************
361 Finally for @scc@ expressions we introduce a new STG construct.
365 Module -- the module of the source of this tick
367 (GenStgExpr bndr occ) -- sub expression
371 %************************************************************************
373 \subsection{STG right-hand sides}
375 %************************************************************************
377 Here's the rest of the interesting stuff for @StgLet@s; the first
378 flavour is for closures:
380 data GenStgRhs bndr occ
382 CostCentreStack -- CCS to be attached (default is CurrentCCS)
383 StgBinderInfo -- Info about how this binder is used (see below)
384 [occ] -- non-global free vars; a list, rather than
385 -- a set, because order is important
386 !UpdateFlag -- ReEntrant | Updatable | SingleEntry
387 SRT -- The SRT reference
388 [bndr] -- arguments; if empty, then not a function;
389 -- as above, order is important.
390 (GenStgExpr bndr occ) -- body
392 An example may be in order. Consider:
394 let t = \x -> \y -> ... x ... y ... p ... q in e
396 Pulling out the free vars and stylising somewhat, we get the equivalent:
398 let t = (\[p,q] -> \[x,y] -> ... x ... y ... p ...q) p q
400 Stg-operationally, the @[x,y]@ are on the stack, the @[p,q]@ are
401 offsets from @Node@ into the closure, and the code ptr for the closure
402 will be exactly that in parentheses above.
404 The second flavour of right-hand-side is for constructors (simple but important):
407 CostCentreStack -- CCS to be attached (default is CurrentCCS).
408 -- Top-level (static) ones will end up with
409 -- DontCareCCS, because we don't count static
410 -- data in heap profiles, and we don't set CCCS
411 -- from static closure.
412 DataCon -- constructor
413 [GenStgArg occ] -- args
417 stgRhsArity :: StgRhs -> Int
418 stgRhsArity (StgRhsClosure _ _ _ _ _ bndrs _)
419 = ASSERT( all isId bndrs ) length bndrs
420 -- The arity never includes type parameters, but they should have gone by now
421 stgRhsArity (StgRhsCon _ _ _) = 0
425 stgBindHasCafRefs :: GenStgBinding bndr Id -> Bool
426 stgBindHasCafRefs (StgNonRec _ rhs) = rhsHasCafRefs rhs
427 stgBindHasCafRefs (StgRec binds) = any rhsHasCafRefs (map snd binds)
429 rhsHasCafRefs :: GenStgRhs bndr Id -> Bool
430 rhsHasCafRefs (StgRhsClosure _ _ _ upd srt _ _)
431 = isUpdatable upd || nonEmptySRT srt
432 rhsHasCafRefs (StgRhsCon _ _ args)
433 = any stgArgHasCafRefs args
435 stgArgHasCafRefs :: GenStgArg Id -> Bool
436 stgArgHasCafRefs (StgVarArg id) = mayHaveCafRefs (idCafInfo id)
437 stgArgHasCafRefs _ = False
440 Here's the @StgBinderInfo@ type, and its combining op:
444 | SatCallsOnly -- All occurrences are *saturated* *function* calls
445 -- This means we don't need to build an info table and
446 -- slow entry code for the thing
447 -- Thunks never get this value
449 noBinderInfo, stgUnsatOcc, stgSatOcc :: StgBinderInfo
450 noBinderInfo = NoStgBinderInfo
451 stgUnsatOcc = NoStgBinderInfo
452 stgSatOcc = SatCallsOnly
454 satCallsOnly :: StgBinderInfo -> Bool
455 satCallsOnly SatCallsOnly = True
456 satCallsOnly NoStgBinderInfo = False
458 combineStgBinderInfo :: StgBinderInfo -> StgBinderInfo -> StgBinderInfo
459 combineStgBinderInfo SatCallsOnly SatCallsOnly = SatCallsOnly
460 combineStgBinderInfo _ _ = NoStgBinderInfo
463 pp_binder_info :: StgBinderInfo -> SDoc
464 pp_binder_info NoStgBinderInfo = empty
465 pp_binder_info SatCallsOnly = ptext (sLit "sat-only")
468 %************************************************************************
470 \subsection[Stg-case-alternatives]{STG case alternatives}
472 %************************************************************************
474 Very like in @CoreSyntax@ (except no type-world stuff).
476 The type constructor is guaranteed not to be abstract; that is, we can
477 see its representation. This is important because the code generator
478 uses it to determine return conventions etc. But it's not trivial
479 where there's a moduule loop involved, because some versions of a type
480 constructor might not have all the constructors visible. So
481 mkStgAlgAlts (in CoreToStg) ensures that it gets the TyCon from the
482 constructors or literals (which are guaranteed to have the Real McCoy)
483 rather than from the scrutinee type.
486 type GenStgAlt bndr occ
487 = (AltCon, -- alts: data constructor,
488 [bndr], -- constructor's parameters,
489 [Bool], -- "use mask", same length as
490 -- parameters; a True in a
491 -- param's position if it is
493 GenStgExpr bndr occ) -- ...right-hand side.
496 = PolyAlt -- Polymorphic (a type variable)
497 | UbxTupAlt TyCon -- Unboxed tuple
498 | AlgAlt TyCon -- Algebraic data type; the AltCons will be DataAlts
499 | PrimAlt TyCon -- Primitive data type; the AltCons will be LitAlts
502 %************************************************************************
504 \subsection[Stg]{The Plain STG parameterisation}
506 %************************************************************************
508 This happens to be the only one we use at the moment.
511 type StgBinding = GenStgBinding Id Id
512 type StgArg = GenStgArg Id
513 type StgLiveVars = GenStgLiveVars Id
514 type StgExpr = GenStgExpr Id Id
515 type StgRhs = GenStgRhs Id Id
516 type StgAlt = GenStgAlt Id Id
519 %************************************************************************
521 \subsubsection[UpdateFlag-datatype]{@UpdateFlag@}
523 %************************************************************************
525 This is also used in @LambdaFormInfo@ in the @ClosureInfo@ module.
527 A @ReEntrant@ closure may be entered multiple times, but should not be
528 updated or blackholed. An @Updatable@ closure should be updated after
529 evaluation (and may be blackholed during evaluation). A @SingleEntry@
530 closure will only be entered once, and so need not be updated but may
531 safely be blackholed.
534 data UpdateFlag = ReEntrant | Updatable | SingleEntry
536 instance Outputable UpdateFlag where
538 = char (case u of { ReEntrant -> 'r'; Updatable -> 'u'; SingleEntry -> 's' })
540 isUpdatable :: UpdateFlag -> Bool
541 isUpdatable ReEntrant = False
542 isUpdatable SingleEntry = False
543 isUpdatable Updatable = True
546 %************************************************************************
548 \subsubsection{StgOp}
550 %************************************************************************
552 An StgOp allows us to group together PrimOps and ForeignCalls.
553 It's quite useful to move these around together, notably
554 in StgOpApp and COpStmt.
557 data StgOp = StgPrimOp PrimOp
559 | StgFCallOp ForeignCall Unique
560 -- The Unique is occasionally needed by the C pretty-printer
561 -- (which lacks a unique supply), notably when generating a
562 -- typedef for foreign-export-dynamic
566 %************************************************************************
568 \subsubsection[Static Reference Tables]{@SRT@}
570 %************************************************************************
572 There is one SRT per top-level function group. Each local binding and
573 case expression within this binding group has a subrange of the whole
574 SRT, expressed as an offset and length.
576 In CoreToStg we collect the list of CafRefs at each SRT site, which is later
577 converted into the length and offset form by the SRT pass.
582 -- generated by CoreToStg
583 | SRT !Int{-offset-} !Int{-length-} !Bitmap{-bitmap-}
584 -- generated by computeSRTs
586 nonEmptySRT :: SRT -> Bool
587 nonEmptySRT NoSRT = False
588 nonEmptySRT (SRTEntries vs) = not (isEmptyVarSet vs)
591 pprSRT :: SRT -> SDoc
592 pprSRT (NoSRT) = ptext (sLit "_no_srt_")
593 pprSRT (SRTEntries ids) = text "SRT:" <> ppr ids
594 pprSRT (SRT off _ _) = parens (ppr off <> comma <> text "*bitmap*")
597 %************************************************************************
599 \subsection[Stg-pretty-printing]{Pretty-printing}
601 %************************************************************************
603 Robin Popplestone asked for semi-colon separators on STG binds; here's
604 hoping he likes terminators instead... Ditto for case alternatives.
607 pprGenStgBinding :: (Outputable bndr, Outputable bdee, Ord bdee)
608 => GenStgBinding bndr bdee -> SDoc
610 pprGenStgBinding (StgNonRec bndr rhs)
611 = hang (hsep [ppr bndr, equals])
612 4 ((<>) (ppr rhs) semi)
614 pprGenStgBinding (StgRec pairs)
615 = vcat ((ifPprDebug (ptext (sLit "{- StgRec (begin) -}"))) :
616 (map (ppr_bind) pairs) ++ [(ifPprDebug (ptext (sLit "{- StgRec (end) -}")))])
618 ppr_bind (bndr, expr)
619 = hang (hsep [ppr bndr, equals])
620 4 ((<>) (ppr expr) semi)
622 pprStgBinding :: StgBinding -> SDoc
623 pprStgBinding bind = pprGenStgBinding bind
625 pprStgBindings :: [StgBinding] -> SDoc
626 pprStgBindings binds = vcat (map pprGenStgBinding binds)
628 pprGenStgBindingWithSRT
629 :: (Outputable bndr, Outputable bdee, Ord bdee)
630 => (GenStgBinding bndr bdee,[(Id,[Id])]) -> SDoc
632 pprGenStgBindingWithSRT (bind,srts)
633 = vcat (pprGenStgBinding bind : map pprSRT srts)
634 where pprSRT (id,srt) =
635 ptext (sLit "SRT") <> parens (ppr id) <> ptext (sLit ": ") <> ppr srt
637 pprStgBindingsWithSRTs :: [(StgBinding,[(Id,[Id])])] -> SDoc
638 pprStgBindingsWithSRTs binds = vcat (map pprGenStgBindingWithSRT binds)
642 instance (Outputable bdee) => Outputable (GenStgArg bdee) where
645 instance (Outputable bndr, Outputable bdee, Ord bdee)
646 => Outputable (GenStgBinding bndr bdee) where
647 ppr = pprGenStgBinding
649 instance (Outputable bndr, Outputable bdee, Ord bdee)
650 => Outputable (GenStgExpr bndr bdee) where
653 instance (Outputable bndr, Outputable bdee, Ord bdee)
654 => Outputable (GenStgRhs bndr bdee) where
655 ppr rhs = pprStgRhs rhs
659 pprStgArg :: (Outputable bdee) => GenStgArg bdee -> SDoc
661 pprStgArg (StgVarArg var) = ppr var
662 pprStgArg (StgLitArg con) = ppr con
663 pprStgArg (StgTypeArg ty) = char '@' <+> ppr ty
667 pprStgExpr :: (Outputable bndr, Outputable bdee, Ord bdee)
668 => GenStgExpr bndr bdee -> SDoc
670 pprStgExpr (StgLit lit) = ppr lit
673 pprStgExpr (StgApp func args)
675 4 (sep (map (ppr) args))
679 pprStgExpr (StgConApp con args)
680 = hsep [ ppr con, brackets (interppSP args)]
682 pprStgExpr (StgOpApp op args _)
683 = hsep [ pprStgOp op, brackets (interppSP args)]
685 pprStgExpr (StgLam _ bndrs body)
686 =sep [ char '\\' <+> ppr bndrs <+> ptext (sLit "->"),
691 -- special case: let v = <very specific thing>
697 -- Very special! Suspicious! (SLPJ)
700 pprStgExpr (StgLet srt (StgNonRec bndr (StgRhsClosure cc bi free_vars upd_flag args rhs))
703 (hang (hcat [ptext (sLit "let { "), ppr bndr, ptext (sLit " = "),
706 ptext (sLit " ["), ifPprDebug (interppSP free_vars), ptext (sLit "] \\"),
707 ppr upd_flag, ptext (sLit " ["),
708 interppSP args, char ']'])
709 8 (sep [hsep [ppr rhs, ptext (sLit "} in")]]))
713 -- special case: let ... in let ...
715 pprStgExpr (StgLet bind expr@(StgLet _ _))
717 (sep [hang (ptext (sLit "let {"))
718 2 (hsep [pprGenStgBinding bind, ptext (sLit "} in")])])
722 pprStgExpr (StgLet bind expr)
723 = sep [hang (ptext (sLit "let {")) 2 (pprGenStgBinding bind),
724 hang (ptext (sLit "} in ")) 2 (ppr expr)]
726 pprStgExpr (StgLetNoEscape lvs_whole lvs_rhss bind expr)
727 = sep [hang (ptext (sLit "let-no-escape {"))
728 2 (pprGenStgBinding bind),
729 hang ((<>) (ptext (sLit "} in "))
732 hcat [ptext (sLit "-- lvs: ["), interppSP (uniqSetToList lvs_whole),
733 ptext (sLit "]; rhs lvs: ["), interppSP (uniqSetToList lvs_rhss),
737 pprStgExpr (StgSCC cc expr)
738 = sep [ hsep [ptext (sLit "_scc_"), ppr cc],
741 pprStgExpr (StgTick m n expr)
742 = sep [ hsep [ptext (sLit "_tick_"), pprModule m,text (show n)],
745 pprStgExpr (StgCase expr lvs_whole lvs_rhss bndr srt alt_type alts)
746 = sep [sep [ptext (sLit "case"),
747 nest 4 (hsep [pprStgExpr expr,
748 ifPprDebug (dcolon <+> ppr alt_type)]),
749 ptext (sLit "of"), ppr bndr, char '{'],
752 hcat [ptext (sLit "-- lvs: ["), interppSP (uniqSetToList lvs_whole),
753 ptext (sLit "]; rhs lvs: ["), interppSP (uniqSetToList lvs_rhss),
756 nest 2 (vcat (map pprStgAlt alts)),
759 pprStgAlt :: (Outputable bndr, Outputable occ, Ord occ)
760 => GenStgAlt bndr occ -> SDoc
761 pprStgAlt (con, params, _use_mask, expr)
762 = hang (hsep [ppr con, interppSP params, ptext (sLit "->")])
765 pprStgOp :: StgOp -> SDoc
766 pprStgOp (StgPrimOp op) = ppr op
767 pprStgOp (StgFCallOp op _) = ppr op
769 instance Outputable AltType where
770 ppr PolyAlt = ptext (sLit "Polymorphic")
771 ppr (UbxTupAlt tc) = ptext (sLit "UbxTup") <+> ppr tc
772 ppr (AlgAlt tc) = ptext (sLit "Alg") <+> ppr tc
773 ppr (PrimAlt tc) = ptext (sLit "Prim") <+> ppr tc
778 pprStgLVs :: Outputable occ => GenStgLiveVars occ -> SDoc
780 = getPprStyle $ \ sty ->
781 if userStyle sty || isEmptyUniqSet lvs then
784 hcat [text "{-lvs:", interpp'SP (uniqSetToList lvs), text "-}"]
789 pprStgRhs :: (Outputable bndr, Outputable bdee, Ord bdee)
790 => GenStgRhs bndr bdee -> SDoc
793 pprStgRhs (StgRhsClosure cc bi [free_var] upd_flag srt [{-no args-}] (StgApp func []))
796 brackets (ifPprDebug (ppr free_var)),
797 ptext (sLit " \\"), ppr upd_flag, pprMaybeSRT srt, ptext (sLit " [] "), ppr func ]
800 pprStgRhs (StgRhsClosure cc bi free_vars upd_flag srt args body)
801 = hang (hsep [if opt_SccProfilingOn then ppr cc else empty,
803 ifPprDebug (brackets (interppSP free_vars)),
804 char '\\' <> ppr upd_flag, pprMaybeSRT srt, brackets (interppSP args)])
807 pprStgRhs (StgRhsCon cc con args)
809 space, ppr con, ptext (sLit "! "), brackets (interppSP args)]
811 pprMaybeSRT :: SRT -> SDoc
812 pprMaybeSRT (NoSRT) = empty
813 pprMaybeSRT srt = ptext (sLit "srt:") <> pprSRT srt