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 GenStgCaseAlts(..), GenStgCaseDefault(..),
19 UpdateFlag(..), isUpdatable,
22 noBinderInfo, stgSatOcc, stgUnsatOcc, satCallsOnly,
25 -- a set of synonyms for the most common (only :-) parameterisation
27 StgBinding, StgExpr, StgRhs,
28 StgCaseAlts, StgCaseDefault,
37 stgBindHasCafRefs, stgRhsArity, getArgPrimRep,
38 isLitLitArg, isDllConApp, isStgTypeArg,
39 stgArgType, stgBinders,
41 pprStgBinding, pprStgBindings, pprStgBindingsWithSRTs, pprStgAlts
48 #include "HsVersions.h"
50 import CostCentre ( CostCentreStack, CostCentre )
51 import VarSet ( IdSet, isEmptyVarSet )
52 import Id ( Id, idName, idPrimRep, idType )
53 import Name ( isDllName )
54 import Literal ( Literal, literalType, isLitLitLit, literalPrimRep )
55 import ForeignCall ( ForeignCall )
56 import DataCon ( DataCon, dataConName )
57 import PrimOp ( PrimOp )
60 import TyCon ( TyCon )
61 import UniqSet ( isEmptyUniqSet, uniqSetToList, UniqSet )
62 import Unique ( Unique )
63 import CmdLineOpts ( opt_SccProfilingOn )
66 %************************************************************************
68 \subsection{@GenStgBinding@}
70 %************************************************************************
72 As usual, expressions are interesting; other things are boring. Here
73 are the boring things [except note the @GenStgRhs@], parameterised
74 with respect to binder and occurrence information (just as in
77 There is one SRT for each group of bindings.
80 data GenStgBinding bndr occ
81 = StgNonRec SRT bndr (GenStgRhs bndr occ)
82 | StgRec SRT [(bndr, GenStgRhs bndr occ)]
84 stgBinders :: GenStgBinding bndr occ -> [bndr]
85 stgBinders (StgNonRec _ b _) = [b]
86 stgBinders (StgRec _ bs) = map fst bs
89 %************************************************************************
91 \subsection{@GenStgArg@}
93 %************************************************************************
99 | StgTypeArg Type -- For when we want to preserve all type info
103 getArgPrimRep (StgVarArg local) = idPrimRep local
104 getArgPrimRep (StgLitArg lit) = literalPrimRep lit
106 isLitLitArg (StgLitArg lit) = isLitLitLit lit
107 isLitLitArg _ = False
109 isStgTypeArg (StgTypeArg _) = True
110 isStgTypeArg other = False
112 isDllArg :: StgArg -> Bool
113 -- Does this argument refer to something in a different DLL?
114 isDllArg (StgVarArg v) = isDllName (idName v)
115 isDllArg (StgLitArg lit) = isLitLitLit lit
117 isDllConApp :: DataCon -> [StgArg] -> Bool
118 -- Does this constructor application refer to
119 -- anything in a different DLL?
120 -- If so, we can't allocate it statically
121 isDllConApp con args = isDllName (dataConName con) || any isDllArg args
123 stgArgType :: StgArg -> Type
124 -- Very half baked becase we have lost the type arguments
125 stgArgType (StgVarArg v) = idType v
126 stgArgType (StgLitArg lit) = literalType lit
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
210 -- expression; i.e., those which mustn't be
213 (GenStgLiveVars occ) -- Live vars of RHSs;
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
224 (GenStgCaseAlts bndr occ)
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 [bndr] -- arguments; if empty, then not a function;
370 -- as above, order is important.
371 (GenStgExpr bndr occ) -- body
373 An example may be in order. Consider:
375 let t = \x -> \y -> ... x ... y ... p ... q in e
377 Pulling out the free vars and stylising somewhat, we get the equivalent:
379 let t = (\[p,q] -> \[x,y] -> ... x ... y ... p ...q) p q
381 Stg-operationally, the @[x,y]@ are on the stack, the @[p,q]@ are
382 offsets from @Node@ into the closure, and the code ptr for the closure
383 will be exactly that in parentheses above.
385 The second flavour of right-hand-side is for constructors (simple but important):
388 CostCentreStack -- CCS to be attached (default is CurrentCCS).
389 -- Top-level (static) ones will end up with
390 -- DontCareCCS, because we don't count static
391 -- data in heap profiles, and we don't set CCCS
392 -- from static closure.
393 DataCon -- constructor
394 [GenStgArg occ] -- args
398 stgRhsArity :: GenStgRhs bndr occ -> Int
399 stgRhsArity (StgRhsClosure _ _ _ _ args _) = length args
400 stgRhsArity (StgRhsCon _ _ _) = 0
404 stgBindHasCafRefs :: GenStgBinding bndr occ -> Bool
405 stgBindHasCafRefs (StgNonRec srt _ rhs)
406 = nonEmptySRT srt || rhsIsUpdatable rhs
407 stgBindHasCafRefs (StgRec srt binds)
408 = nonEmptySRT srt || any rhsIsUpdatable (map snd binds)
410 rhsIsUpdatable (StgRhsClosure _ _ _ upd _ _) = isUpdatable upd
411 rhsIsUpdatable _ = False
414 Here's the @StgBinderInfo@ type, and its combining op:
418 | SatCallsOnly -- All occurrences are *saturated* *function* calls
419 -- This means we don't need to build an info table and
420 -- slow entry code for the thing
421 -- Thunks never get this value
423 noBinderInfo = NoStgBinderInfo
424 stgUnsatOcc = NoStgBinderInfo
425 stgSatOcc = SatCallsOnly
427 satCallsOnly :: StgBinderInfo -> Bool
428 satCallsOnly SatCallsOnly = True
429 satCallsOnly NoStgBinderInfo = False
431 combineStgBinderInfo :: StgBinderInfo -> StgBinderInfo -> StgBinderInfo
432 combineStgBinderInfo SatCallsOnly SatCallsOnly = SatCallsOnly
433 combineStgBinderInfo info1 info2 = NoStgBinderInfo
436 pp_binder_info NoStgBinderInfo = empty
437 pp_binder_info SatCallsOnly = ptext SLIT("sat-only")
440 %************************************************************************
442 \subsection[Stg-case-alternatives]{STG case alternatives}
444 %************************************************************************
446 Just like in @CoreSyntax@ (except no type-world stuff).
448 * Algebraic cases are done using
449 StgAlgAlts (Just tc) alts deflt
451 * Polymorphic cases, or case of a function type, are done using
452 StgAlgAlts Nothing [] (StgBindDefault e)
454 * Primitive cases are done using
455 StgPrimAlts tc alts deflt
457 We thought of giving polymorphic cases their own constructor,
458 but we get a bit more code sharing this way
460 The type constructor in StgAlgAlts/StgPrimAlts is guaranteed not
461 to be abstract; that is, we can see its representation. This is
462 important because the code generator uses it to determine return
463 conventions etc. But it's not trivial where there's a moduule loop
464 involved, because some versions of a type constructor might not have
465 all the constructors visible. So mkStgAlgAlts (in CoreToStg) ensures
466 that it gets the TyCon from the constructors or literals (which are
467 guaranteed to have the Real McCoy) rather than from the scrutinee type.
470 data GenStgCaseAlts bndr occ
471 = StgAlgAlts (Maybe TyCon) -- Just tc => scrutinee type is
472 -- an algebraic data type
473 -- Nothing => scrutinee type is a type
474 -- variable or function type
475 [(DataCon, -- alts: data constructor,
476 [bndr], -- constructor's parameters,
477 [Bool], -- "use mask", same length as
478 -- parameters; a True in a
479 -- param's position if it is
481 GenStgExpr bndr occ)] -- ...right-hand side.
482 (GenStgCaseDefault bndr occ)
485 [(Literal, -- alts: unboxed literal,
486 GenStgExpr bndr occ)] -- rhs.
487 (GenStgCaseDefault bndr occ)
489 data GenStgCaseDefault bndr occ
490 = StgNoDefault -- small con family: all
491 -- constructor accounted for
492 | StgBindDefault (GenStgExpr bndr occ)
495 %************************************************************************
497 \subsection[Stg]{The Plain STG parameterisation}
499 %************************************************************************
501 This happens to be the only one we use at the moment.
504 type StgBinding = GenStgBinding Id Id
505 type StgArg = GenStgArg Id
506 type StgLiveVars = GenStgLiveVars Id
507 type StgExpr = GenStgExpr Id Id
508 type StgRhs = GenStgRhs Id Id
509 type StgCaseAlts = GenStgCaseAlts Id Id
510 type StgCaseDefault = GenStgCaseDefault Id Id
513 %************************************************************************
515 \subsubsection[UpdateFlag-datatype]{@UpdateFlag@}
517 %************************************************************************
519 This is also used in @LambdaFormInfo@ in the @ClosureInfo@ module.
521 A @ReEntrant@ closure may be entered multiple times, but should not be
522 updated or blackholed. An @Updatable@ closure should be updated after
523 evaluation (and may be blackholed during evaluation). A @SingleEntry@
524 closure will only be entered once, and so need not be updated but may
525 safely be blackholed.
528 data UpdateFlag = ReEntrant | Updatable | SingleEntry
530 instance Outputable UpdateFlag where
532 = char (case u of { ReEntrant -> 'r'; Updatable -> 'u'; SingleEntry -> 's' })
534 isUpdatable ReEntrant = False
535 isUpdatable SingleEntry = False
536 isUpdatable Updatable = True
539 %************************************************************************
541 \subsubsection{StgOp}
543 %************************************************************************
545 An StgOp allows us to group together PrimOps and ForeignCalls.
546 It's quite useful to move these around together, notably
547 in StgOpApp and COpStmt.
550 data StgOp = StgPrimOp PrimOp
552 | StgFCallOp ForeignCall Unique
553 -- The Unique is occasionally needed by the C pretty-printer
554 -- (which lacks a unique supply), notably when generating a
555 -- typedef for foreign-export-dynamic
559 %************************************************************************
561 \subsubsection[Static Reference Tables]{@SRT@}
563 %************************************************************************
565 There is one SRT per top-level function group. Each local binding and
566 case expression within this binding group has a subrange of the whole
567 SRT, expressed as an offset and length.
569 In CoreToStg we collect the list of CafRefs at each SRT site, which is later
570 converted into the length and offset form by the SRT pass.
574 | SRTEntries IdSet -- generated by CoreToStg
575 | SRT !Int{-offset-} !Int{-length-} -- generated by computeSRTs
580 nonEmptySRT NoSRT = False
581 nonEmptySRT (SRTEntries vs) = not (isEmptyVarSet vs)
584 pprSRT (NoSRT) = ptext SLIT("_no_srt_")
585 pprSRT (SRTEntries ids) = text "SRT:" <> ppr ids
586 pprSRT (SRT off len) = parens (ppr off <> comma <> ppr len)
589 %************************************************************************
591 \subsection[Stg-pretty-printing]{Pretty-printing}
593 %************************************************************************
595 Robin Popplestone asked for semi-colon separators on STG binds; here's
596 hoping he likes terminators instead... Ditto for case alternatives.
599 pprGenStgBinding :: (Outputable bndr, Outputable bdee, Ord bdee)
600 => GenStgBinding bndr bdee -> SDoc
602 pprGenStgBinding (StgNonRec srt bndr rhs)
603 = pprMaybeSRT srt $$ hang (hsep [ppr bndr, equals])
604 4 ((<>) (ppr rhs) semi)
606 pprGenStgBinding (StgRec srt pairs)
607 = vcat ((ifPprDebug (ptext SLIT("{- StgRec (begin) -}"))) :
609 (map (ppr_bind) pairs) ++ [(ifPprDebug (ptext SLIT("{- StgRec (end) -}")))])
611 ppr_bind (bndr, expr)
612 = hang (hsep [ppr bndr, equals])
613 4 ((<>) (ppr expr) semi)
615 pprStgBinding :: StgBinding -> SDoc
616 pprStgBinding bind = pprGenStgBinding bind
618 pprStgBindings :: [StgBinding] -> SDoc
619 pprStgBindings binds = vcat (map pprGenStgBinding binds)
621 pprGenStgBindingWithSRT
622 :: (Outputable bndr, Outputable bdee, Ord bdee)
623 => (GenStgBinding bndr bdee,[Id]) -> SDoc
625 pprGenStgBindingWithSRT (bind,srt)
626 = vcat [ pprGenStgBinding bind,
627 ptext SLIT("SRT: ") <> ppr srt ]
629 pprStgBindingsWithSRTs :: [(StgBinding,[Id])] -> SDoc
630 pprStgBindingsWithSRTs binds = vcat (map pprGenStgBindingWithSRT binds)
634 instance (Outputable bdee) => Outputable (GenStgArg bdee) where
637 instance (Outputable bndr, Outputable bdee, Ord bdee)
638 => Outputable (GenStgBinding bndr bdee) where
639 ppr = pprGenStgBinding
641 instance (Outputable bndr, Outputable bdee, Ord bdee)
642 => Outputable (GenStgExpr bndr bdee) where
645 instance (Outputable bndr, Outputable bdee, Ord bdee)
646 => Outputable (GenStgRhs bndr bdee) where
647 ppr rhs = pprStgRhs rhs
651 pprStgArg :: (Outputable bdee) => GenStgArg bdee -> SDoc
653 pprStgArg (StgVarArg var) = ppr var
654 pprStgArg (StgLitArg con) = ppr con
655 pprStgArg (StgTypeArg ty) = char '@' <+> ppr ty
659 pprStgExpr :: (Outputable bndr, Outputable bdee, Ord bdee)
660 => GenStgExpr bndr bdee -> SDoc
662 pprStgExpr (StgLit lit) = ppr lit
665 pprStgExpr (StgApp func args)
667 4 (sep (map (ppr) args))
671 pprStgExpr (StgConApp con args)
672 = hsep [ ppr con, brackets (interppSP args)]
674 pprStgExpr (StgOpApp op args _)
675 = hsep [ pprStgOp op, brackets (interppSP args)]
677 pprStgExpr (StgLam _ bndrs body)
678 =sep [ char '\\' <+> ppr bndrs <+> ptext SLIT("->"),
683 -- special case: let v = <very specific thing>
689 -- Very special! Suspicious! (SLPJ)
692 pprStgExpr (StgLet srt (StgNonRec bndr (StgRhsClosure cc bi free_vars upd_flag args rhs))
695 (hang (hcat [ptext SLIT("let { "), ppr bndr, ptext SLIT(" = "),
698 ptext SLIT(" ["), ifPprDebug (interppSP free_vars), ptext SLIT("] \\"),
699 ppr upd_flag, ptext SLIT(" ["),
700 interppSP args, char ']'])
701 8 (sep [hsep [ppr rhs, ptext SLIT("} in")]]))
705 -- special case: let ... in let ...
707 pprStgExpr (StgLet bind expr@(StgLet _ _))
709 (sep [hang (ptext SLIT("let {"))
710 2 (hsep [pprGenStgBinding bind, ptext SLIT("} in")])])
714 pprStgExpr (StgLet bind expr)
715 = sep [hang (ptext SLIT("let {")) 2 (pprGenStgBinding bind),
716 hang (ptext SLIT("} in ")) 2 (ppr expr)]
718 pprStgExpr (StgLetNoEscape lvs_whole lvs_rhss bind expr)
719 = sep [hang (ptext SLIT("let-no-escape {"))
720 2 (pprGenStgBinding bind),
721 hang ((<>) (ptext SLIT("} in "))
724 hcat [ptext SLIT("-- lvs: ["), interppSP (uniqSetToList lvs_whole),
725 ptext SLIT("]; rhs lvs: ["), interppSP (uniqSetToList lvs_rhss),
731 pprStgExpr (StgSCC cc expr)
732 = sep [ hsep [ptext SLIT("_scc_"), ppr cc],
737 pprStgExpr (StgCase expr lvs_whole lvs_rhss bndr srt alts)
738 = sep [sep [ptext SLIT("case"),
739 nest 4 (hsep [pprStgExpr expr,
740 ifPprDebug (dcolon <+> pp_ty alts)]),
741 ptext SLIT("of"), ppr bndr, char '{'],
744 hcat [ptext SLIT("-- lvs: ["), interppSP (uniqSetToList lvs_whole),
745 ptext SLIT("]; rhs lvs: ["), interppSP (uniqSetToList lvs_rhss),
748 nest 2 (pprStgAlts alts),
751 pp_ty (StgAlgAlts maybe_tycon _ _) = ppr maybe_tycon
752 pp_ty (StgPrimAlts tycon _ _) = ppr tycon
754 pprStgAlts (StgAlgAlts _ alts deflt)
755 = vcat [ vcat (map (ppr_bxd_alt) alts),
756 pprStgDefault deflt ]
758 ppr_bxd_alt (con, params, use_mask, expr)
759 = hang (hsep [ppr con, interppSP params, ptext SLIT("->")])
760 4 ((<>) (ppr expr) semi)
762 pprStgAlts (StgPrimAlts _ alts deflt)
763 = vcat [ vcat (map (ppr_ubxd_alt) alts),
764 pprStgDefault deflt ]
766 ppr_ubxd_alt (lit, expr)
767 = hang (hsep [ppr lit, ptext SLIT("->")])
768 4 ((<>) (ppr expr) semi)
770 pprStgDefault StgNoDefault = empty
771 pprStgDefault (StgBindDefault expr) = hang (hsep [ptext SLIT("DEFAULT"), ptext SLIT("->")])
774 pprStgOp (StgPrimOp op) = ppr op
775 pprStgOp (StgFCallOp op _) = ppr op
779 pprStgLVs :: Outputable occ => GenStgLiveVars occ -> SDoc
781 = getPprStyle $ \ sty ->
782 if userStyle sty || isEmptyUniqSet lvs then
785 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 [{-no args-}] (StgApp func []))
796 brackets (ifPprDebug (ppr free_var)),
797 ptext SLIT(" \\"), ppr upd_flag, ptext SLIT(" [] "), ppr func ]
800 pprStgRhs (StgRhsClosure cc bi free_vars upd_flag args body)
801 = hang (hsep [if opt_SccProfilingOn then ppr cc else empty,
803 ifPprDebug (brackets (interppSP free_vars)),
804 char '\\' <> ppr upd_flag, brackets (interppSP args)])
807 pprStgRhs (StgRhsCon cc con args)
809 space, ppr con, ptext SLIT("! "), brackets (interppSP args)]
811 pprMaybeSRT (NoSRT) = empty
812 pprMaybeSRT srt = ptext SLIT("srt: ") <> pprSRT srt