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 PackageConfig ( PackageId )
56 import Literal ( Literal, literalType )
57 import ForeignCall ( ForeignCall )
58 import DataCon ( DataCon, dataConName )
59 import CoreSyn ( AltCon )
60 import PprCore ( {- instances -} )
61 import PrimOp ( PrimOp )
65 import TyCon ( TyCon )
66 import UniqSet ( isEmptyUniqSet, uniqSetToList, UniqSet )
67 import Unique ( Unique )
69 import StaticFlags ( opt_SccProfilingOn )
72 %************************************************************************
74 \subsection{@GenStgBinding@}
76 %************************************************************************
78 As usual, expressions are interesting; other things are boring. Here
79 are the boring things [except note the @GenStgRhs@], parameterised
80 with respect to binder and occurrence information (just as in
83 There is one SRT for each group of bindings.
86 data GenStgBinding bndr occ
87 = StgNonRec bndr (GenStgRhs bndr occ)
88 | StgRec [(bndr, GenStgRhs bndr occ)]
91 %************************************************************************
93 \subsection{@GenStgArg@}
95 %************************************************************************
101 | StgTypeArg Type -- For when we want to preserve all type info
105 isStgTypeArg (StgTypeArg _) = True
106 isStgTypeArg other = False
108 isDllArg :: PackageId -> StgArg -> Bool
109 -- Does this argument refer to something in a different DLL?
110 isDllArg this_pkg (StgTypeArg v) = False
111 isDllArg this_pkg (StgVarArg v) = isDllName this_pkg (idName v)
112 isDllArg this_pkg (StgLitArg lit) = False
114 isDllConApp :: PackageId -> DataCon -> [StgArg] -> Bool
115 -- Does this constructor application refer to
116 -- anything in a different DLL?
117 -- If so, we can't allocate it statically
118 isDllConApp this_pkg con args
119 = isDllName this_pkg (dataConName con) || any (isDllArg this_pkg) args
121 stgArgType :: StgArg -> Type
122 -- Very half baked becase we have lost the type arguments
123 stgArgType (StgVarArg v) = idType v
124 stgArgType (StgLitArg lit) = literalType lit
125 stgArgType (StgTypeArg lit) = panic "stgArgType called on stgTypeArg"
128 %************************************************************************
130 \subsection{STG expressions}
132 %************************************************************************
134 The @GenStgExpr@ data type is parameterised on binder and occurrence
137 %************************************************************************
139 \subsubsection{@GenStgExpr@ application}
141 %************************************************************************
143 An application is of a function to a list of atoms [not expressions].
144 Operationally, we want to push the arguments on the stack and call the
145 function. (If the arguments were expressions, we would have to build
146 their closures first.)
148 There is no constructor for a lone variable; it would appear as
151 type GenStgLiveVars occ = UniqSet occ
153 data GenStgExpr bndr occ
156 [GenStgArg occ] -- arguments; may be empty
159 %************************************************************************
161 \subsubsection{@StgConApp@ and @StgPrimApp@---saturated applications}
163 %************************************************************************
165 There are a specialised forms of application, for
166 constructors, primitives, and literals.
171 [GenStgArg occ] -- Saturated
173 | StgOpApp StgOp -- Primitive op or foreign call
174 [GenStgArg occ] -- Saturated
175 Type -- Result type; we need to know the result type
176 -- so that we can assign result registers.
179 %************************************************************************
181 \subsubsection{@StgLam@}
183 %************************************************************************
185 StgLam is used *only* during CoreToStg's work. Before CoreToStg has finished
186 it encodes (\x -> e) as (let f = \x -> e in f)
190 Type -- Type of whole lambda (useful when making a binder for it)
192 StgExpr -- Body of lambda
196 %************************************************************************
198 \subsubsection{@GenStgExpr@: case-expressions}
200 %************************************************************************
202 This has the same boxed/unboxed business as Core case expressions.
205 (GenStgExpr bndr occ)
206 -- the thing to examine
208 (GenStgLiveVars occ) -- Live vars of whole case expression,
209 -- plus everything that happens after the case
210 -- i.e., those which mustn't be overwritten
212 (GenStgLiveVars occ) -- Live vars of RHSs (plus what happens afterwards)
213 -- i.e., those which must be saved before eval.
215 -- note that an alt's constructor's
216 -- binder-variables are NOT counted in the
217 -- free vars for the alt's RHS
219 bndr -- binds the result of evaluating the scrutinee
221 SRT -- The SRT for the continuation
225 [GenStgAlt bndr occ] -- The DEFAULT case is always *first*
226 -- if it is there at all
229 %************************************************************************
231 \subsubsection{@GenStgExpr@: @let(rec)@-expressions}
233 %************************************************************************
235 The various forms of let(rec)-expression encode most of the
236 interesting things we want to do.
240 let-closure x = [free-vars] expr [args]
245 let x = (\free-vars -> \args -> expr) free-vars
247 \tr{args} may be empty (and is for most closures). It isn't under
248 circumstances like this:
254 let-closure x = [z] [y] (y+z)
256 The idea is that we compile code for @(y+z)@ in an environment in which
257 @z@ is bound to an offset from \tr{Node}, and @y@ is bound to an
258 offset from the stack pointer.
260 (A let-closure is an @StgLet@ with a @StgRhsClosure@ RHS.)
264 let-constructor x = Constructor [args]
268 (A let-constructor is an @StgLet@ with a @StgRhsCon@ RHS.)
271 Letrec-expressions are essentially the same deal as
272 let-closure/let-constructor, so we use a common structure and
273 distinguish between them with an @is_recursive@ boolean flag.
277 let-unboxed u = an arbitrary arithmetic expression in unboxed values
280 All the stuff on the RHS must be fully evaluated. No function calls either!
282 (We've backed away from this toward case-expressions with
283 suitably-magical alts ...)
286 ~[Advanced stuff here! Not to start with, but makes pattern matching
287 generate more efficient code.]
290 let-escapes-not fail = expr
293 Here the idea is that @e'@ guarantees not to put @fail@ in a data structure,
294 or pass it to another function. All @e'@ will ever do is tail-call @fail@.
295 Rather than build a closure for @fail@, all we need do is to record the stack
296 level at the moment of the @let-escapes-not@; then entering @fail@ is just
297 a matter of adjusting the stack pointer back down to that point and entering
302 f x y = let z = huge-expression in
308 (A let-escapes-not is an @StgLetNoEscape@.)
311 We may eventually want:
313 let-literal x = Literal
317 (ToDo: is this obsolete?)
320 And so the code for let(rec)-things:
323 (GenStgBinding bndr occ) -- right hand sides (see below)
324 (GenStgExpr bndr occ) -- body
326 | StgLetNoEscape -- remember: ``advanced stuff''
327 (GenStgLiveVars occ) -- Live in the whole let-expression
328 -- Mustn't overwrite these stack slots
329 -- *Doesn't* include binders of the let(rec).
331 (GenStgLiveVars occ) -- Live in the right hand sides (only)
332 -- These are the ones which must be saved on
333 -- the stack if they aren't there already
334 -- *Does* include binders of the let(rec) if recursive.
336 (GenStgBinding bndr occ) -- right hand sides (see below)
337 (GenStgExpr bndr occ) -- body
340 %************************************************************************
342 \subsubsection{@GenStgExpr@: @scc@ expressions}
344 %************************************************************************
346 Finally for @scc@ expressions we introduce a new STG construct.
350 CostCentre -- label of SCC expression
351 (GenStgExpr bndr occ) -- scc expression
355 %************************************************************************
357 \subsection{STG right-hand sides}
359 %************************************************************************
361 Here's the rest of the interesting stuff for @StgLet@s; the first
362 flavour is for closures:
364 data GenStgRhs bndr occ
366 CostCentreStack -- CCS to be attached (default is CurrentCCS)
367 StgBinderInfo -- Info about how this binder is used (see below)
368 [occ] -- non-global free vars; a list, rather than
369 -- a set, because order is important
370 !UpdateFlag -- ReEntrant | Updatable | SingleEntry
371 SRT -- The SRT reference
372 [bndr] -- arguments; if empty, then not a function;
373 -- as above, order is important.
374 (GenStgExpr bndr occ) -- body
376 An example may be in order. Consider:
378 let t = \x -> \y -> ... x ... y ... p ... q in e
380 Pulling out the free vars and stylising somewhat, we get the equivalent:
382 let t = (\[p,q] -> \[x,y] -> ... x ... y ... p ...q) p q
384 Stg-operationally, the @[x,y]@ are on the stack, the @[p,q]@ are
385 offsets from @Node@ into the closure, and the code ptr for the closure
386 will be exactly that in parentheses above.
388 The second flavour of right-hand-side is for constructors (simple but important):
391 CostCentreStack -- CCS to be attached (default is CurrentCCS).
392 -- Top-level (static) ones will end up with
393 -- DontCareCCS, because we don't count static
394 -- data in heap profiles, and we don't set CCCS
395 -- from static closure.
396 DataCon -- constructor
397 [GenStgArg occ] -- args
401 stgRhsArity :: StgRhs -> Int
402 stgRhsArity (StgRhsClosure _ _ _ _ _ bndrs _) = count isId bndrs
403 -- The arity never includes type parameters, so
404 -- when keeping type arguments and binders in the Stg syntax
405 -- (opt_RuntimeTypes) we have to fliter out the type binders.
406 stgRhsArity (StgRhsCon _ _ _) = 0
410 stgBindHasCafRefs :: GenStgBinding bndr Id -> Bool
411 stgBindHasCafRefs (StgNonRec _ rhs) = rhsHasCafRefs rhs
412 stgBindHasCafRefs (StgRec binds) = any rhsHasCafRefs (map snd binds)
414 rhsHasCafRefs (StgRhsClosure _ _ _ upd srt _ _)
415 = isUpdatable upd || nonEmptySRT srt
416 rhsHasCafRefs (StgRhsCon _ _ args)
417 = any stgArgHasCafRefs args
419 stgArgHasCafRefs (StgVarArg id) = mayHaveCafRefs (idCafInfo id)
420 stgArgHasCafRefs _ = False
423 Here's the @StgBinderInfo@ type, and its combining op:
427 | SatCallsOnly -- All occurrences are *saturated* *function* calls
428 -- This means we don't need to build an info table and
429 -- slow entry code for the thing
430 -- Thunks never get this value
432 noBinderInfo = NoStgBinderInfo
433 stgUnsatOcc = NoStgBinderInfo
434 stgSatOcc = SatCallsOnly
436 satCallsOnly :: StgBinderInfo -> Bool
437 satCallsOnly SatCallsOnly = True
438 satCallsOnly NoStgBinderInfo = False
440 combineStgBinderInfo :: StgBinderInfo -> StgBinderInfo -> StgBinderInfo
441 combineStgBinderInfo SatCallsOnly SatCallsOnly = SatCallsOnly
442 combineStgBinderInfo info1 info2 = NoStgBinderInfo
445 pp_binder_info NoStgBinderInfo = empty
446 pp_binder_info SatCallsOnly = ptext SLIT("sat-only")
449 %************************************************************************
451 \subsection[Stg-case-alternatives]{STG case alternatives}
453 %************************************************************************
455 Very like in @CoreSyntax@ (except no type-world stuff).
457 The type constructor is guaranteed not to be abstract; that is, we can
458 see its representation. This is important because the code generator
459 uses it to determine return conventions etc. But it's not trivial
460 where there's a moduule loop involved, because some versions of a type
461 constructor might not have all the constructors visible. So
462 mkStgAlgAlts (in CoreToStg) ensures that it gets the TyCon from the
463 constructors or literals (which are guaranteed to have the Real McCoy)
464 rather than from the scrutinee type.
467 type GenStgAlt bndr occ
468 = (AltCon, -- alts: data constructor,
469 [bndr], -- constructor's parameters,
470 [Bool], -- "use mask", same length as
471 -- parameters; a True in a
472 -- param's position if it is
474 GenStgExpr bndr occ) -- ...right-hand side.
477 = PolyAlt -- Polymorphic (a type variable)
478 | UbxTupAlt TyCon -- Unboxed tuple
479 | AlgAlt TyCon -- Algebraic data type; the AltCons will be DataAlts
480 | PrimAlt TyCon -- Primitive data type; the AltCons will be LitAlts
483 %************************************************************************
485 \subsection[Stg]{The Plain STG parameterisation}
487 %************************************************************************
489 This happens to be the only one we use at the moment.
492 type StgBinding = GenStgBinding Id Id
493 type StgArg = GenStgArg Id
494 type StgLiveVars = GenStgLiveVars Id
495 type StgExpr = GenStgExpr Id Id
496 type StgRhs = GenStgRhs Id Id
497 type StgAlt = GenStgAlt Id Id
500 %************************************************************************
502 \subsubsection[UpdateFlag-datatype]{@UpdateFlag@}
504 %************************************************************************
506 This is also used in @LambdaFormInfo@ in the @ClosureInfo@ module.
508 A @ReEntrant@ closure may be entered multiple times, but should not be
509 updated or blackholed. An @Updatable@ closure should be updated after
510 evaluation (and may be blackholed during evaluation). A @SingleEntry@
511 closure will only be entered once, and so need not be updated but may
512 safely be blackholed.
515 data UpdateFlag = ReEntrant | Updatable | SingleEntry
517 instance Outputable UpdateFlag where
519 = char (case u of { ReEntrant -> 'r'; Updatable -> 'u'; SingleEntry -> 's' })
521 isUpdatable ReEntrant = False
522 isUpdatable SingleEntry = False
523 isUpdatable Updatable = True
526 %************************************************************************
528 \subsubsection{StgOp}
530 %************************************************************************
532 An StgOp allows us to group together PrimOps and ForeignCalls.
533 It's quite useful to move these around together, notably
534 in StgOpApp and COpStmt.
537 data StgOp = StgPrimOp PrimOp
539 | StgFCallOp ForeignCall Unique
540 -- The Unique is occasionally needed by the C pretty-printer
541 -- (which lacks a unique supply), notably when generating a
542 -- typedef for foreign-export-dynamic
546 %************************************************************************
548 \subsubsection[Static Reference Tables]{@SRT@}
550 %************************************************************************
552 There is one SRT per top-level function group. Each local binding and
553 case expression within this binding group has a subrange of the whole
554 SRT, expressed as an offset and length.
556 In CoreToStg we collect the list of CafRefs at each SRT site, which is later
557 converted into the length and offset form by the SRT pass.
562 -- generated by CoreToStg
563 | SRT !Int{-offset-} !Int{-length-} !Bitmap{-bitmap-}
564 -- generated by computeSRTs
569 nonEmptySRT NoSRT = False
570 nonEmptySRT (SRTEntries vs) = not (isEmptyVarSet vs)
573 pprSRT (NoSRT) = ptext SLIT("_no_srt_")
574 pprSRT (SRTEntries ids) = text "SRT:" <> ppr ids
575 pprSRT (SRT off length bitmap) = parens (ppr off <> comma <> text "*bitmap*")
578 %************************************************************************
580 \subsection[Stg-pretty-printing]{Pretty-printing}
582 %************************************************************************
584 Robin Popplestone asked for semi-colon separators on STG binds; here's
585 hoping he likes terminators instead... Ditto for case alternatives.
588 pprGenStgBinding :: (Outputable bndr, Outputable bdee, Ord bdee)
589 => GenStgBinding bndr bdee -> SDoc
591 pprGenStgBinding (StgNonRec bndr rhs)
592 = hang (hsep [ppr bndr, equals])
593 4 ((<>) (ppr rhs) semi)
595 pprGenStgBinding (StgRec pairs)
596 = vcat ((ifPprDebug (ptext SLIT("{- StgRec (begin) -}"))) :
597 (map (ppr_bind) pairs) ++ [(ifPprDebug (ptext SLIT("{- StgRec (end) -}")))])
599 ppr_bind (bndr, expr)
600 = hang (hsep [ppr bndr, equals])
601 4 ((<>) (ppr expr) semi)
603 pprStgBinding :: StgBinding -> SDoc
604 pprStgBinding bind = pprGenStgBinding bind
606 pprStgBindings :: [StgBinding] -> SDoc
607 pprStgBindings binds = vcat (map pprGenStgBinding binds)
609 pprGenStgBindingWithSRT
610 :: (Outputable bndr, Outputable bdee, Ord bdee)
611 => (GenStgBinding bndr bdee,[(Id,[Id])]) -> SDoc
613 pprGenStgBindingWithSRT (bind,srts)
614 = vcat (pprGenStgBinding bind : map pprSRT srts)
615 where pprSRT (id,srt) =
616 ptext SLIT("SRT") <> parens (ppr id) <> ptext SLIT(": ") <> ppr srt
618 pprStgBindingsWithSRTs :: [(StgBinding,[(Id,[Id])])] -> SDoc
619 pprStgBindingsWithSRTs binds = vcat (map pprGenStgBindingWithSRT binds)
623 instance (Outputable bdee) => Outputable (GenStgArg bdee) where
626 instance (Outputable bndr, Outputable bdee, Ord bdee)
627 => Outputable (GenStgBinding bndr bdee) where
628 ppr = pprGenStgBinding
630 instance (Outputable bndr, Outputable bdee, Ord bdee)
631 => Outputable (GenStgExpr bndr bdee) where
634 instance (Outputable bndr, Outputable bdee, Ord bdee)
635 => Outputable (GenStgRhs bndr bdee) where
636 ppr rhs = pprStgRhs rhs
640 pprStgArg :: (Outputable bdee) => GenStgArg bdee -> SDoc
642 pprStgArg (StgVarArg var) = ppr var
643 pprStgArg (StgLitArg con) = ppr con
644 pprStgArg (StgTypeArg ty) = char '@' <+> ppr ty
648 pprStgExpr :: (Outputable bndr, Outputable bdee, Ord bdee)
649 => GenStgExpr bndr bdee -> SDoc
651 pprStgExpr (StgLit lit) = ppr lit
654 pprStgExpr (StgApp func args)
656 4 (sep (map (ppr) args))
660 pprStgExpr (StgConApp con args)
661 = hsep [ ppr con, brackets (interppSP args)]
663 pprStgExpr (StgOpApp op args _)
664 = hsep [ pprStgOp op, brackets (interppSP args)]
666 pprStgExpr (StgLam _ bndrs body)
667 =sep [ char '\\' <+> ppr bndrs <+> ptext SLIT("->"),
672 -- special case: let v = <very specific thing>
678 -- Very special! Suspicious! (SLPJ)
681 pprStgExpr (StgLet srt (StgNonRec bndr (StgRhsClosure cc bi free_vars upd_flag args rhs))
684 (hang (hcat [ptext SLIT("let { "), ppr bndr, ptext SLIT(" = "),
687 ptext SLIT(" ["), ifPprDebug (interppSP free_vars), ptext SLIT("] \\"),
688 ppr upd_flag, ptext SLIT(" ["),
689 interppSP args, char ']'])
690 8 (sep [hsep [ppr rhs, ptext SLIT("} in")]]))
694 -- special case: let ... in let ...
696 pprStgExpr (StgLet bind expr@(StgLet _ _))
698 (sep [hang (ptext SLIT("let {"))
699 2 (hsep [pprGenStgBinding bind, ptext SLIT("} in")])])
703 pprStgExpr (StgLet bind expr)
704 = sep [hang (ptext SLIT("let {")) 2 (pprGenStgBinding bind),
705 hang (ptext SLIT("} in ")) 2 (ppr expr)]
707 pprStgExpr (StgLetNoEscape lvs_whole lvs_rhss bind expr)
708 = sep [hang (ptext SLIT("let-no-escape {"))
709 2 (pprGenStgBinding bind),
710 hang ((<>) (ptext SLIT("} in "))
713 hcat [ptext SLIT("-- lvs: ["), interppSP (uniqSetToList lvs_whole),
714 ptext SLIT("]; rhs lvs: ["), interppSP (uniqSetToList lvs_rhss),
718 pprStgExpr (StgSCC cc expr)
719 = sep [ hsep [ptext SLIT("_scc_"), ppr cc],
722 pprStgExpr (StgCase expr lvs_whole lvs_rhss bndr srt alt_type alts)
723 = sep [sep [ptext SLIT("case"),
724 nest 4 (hsep [pprStgExpr expr,
725 ifPprDebug (dcolon <+> ppr alt_type)]),
726 ptext SLIT("of"), ppr bndr, char '{'],
729 hcat [ptext SLIT("-- lvs: ["), interppSP (uniqSetToList lvs_whole),
730 ptext SLIT("]; rhs lvs: ["), interppSP (uniqSetToList lvs_rhss),
733 nest 2 (vcat (map pprStgAlt alts)),
736 pprStgAlt (con, params, use_mask, expr)
737 = hang (hsep [ppr con, interppSP params, ptext SLIT("->")])
740 pprStgOp (StgPrimOp op) = ppr op
741 pprStgOp (StgFCallOp op _) = ppr op
743 instance Outputable AltType where
744 ppr PolyAlt = ptext SLIT("Polymorphic")
745 ppr (UbxTupAlt tc) = ptext SLIT("UbxTup") <+> ppr tc
746 ppr (AlgAlt tc) = ptext SLIT("Alg") <+> ppr tc
747 ppr (PrimAlt tc) = ptext SLIT("Prim") <+> ppr tc
751 pprStgLVs :: Outputable occ => GenStgLiveVars occ -> SDoc
753 = getPprStyle $ \ sty ->
754 if userStyle sty || isEmptyUniqSet lvs then
757 hcat [text "{-lvs:", interpp'SP (uniqSetToList lvs), text "-}"]
761 pprStgRhs :: (Outputable bndr, Outputable bdee, Ord bdee)
762 => GenStgRhs bndr bdee -> SDoc
765 pprStgRhs (StgRhsClosure cc bi [free_var] upd_flag srt [{-no args-}] (StgApp func []))
768 brackets (ifPprDebug (ppr free_var)),
769 ptext SLIT(" \\"), ppr upd_flag, pprMaybeSRT srt, ptext SLIT(" [] "), ppr func ]
772 pprStgRhs (StgRhsClosure cc bi free_vars upd_flag srt args body)
773 = hang (hsep [if opt_SccProfilingOn then ppr cc else empty,
775 ifPprDebug (brackets (interppSP free_vars)),
776 char '\\' <> ppr upd_flag, pprMaybeSRT srt, brackets (interppSP args)])
779 pprStgRhs (StgRhsCon cc con args)
781 space, ppr con, ptext SLIT("! "), brackets (interppSP args)]
783 pprMaybeSRT (NoSRT) = empty
784 pprMaybeSRT srt = ptext SLIT("srt:") <> pprSRT srt