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 stgArgOcc, stgUnsatOcc, stgStdHeapOcc, stgNoUpdHeapOcc,
23 stgNormalOcc, stgFakeFunAppOcc,
26 -- a set of synonyms for the most common (only :-) parameterisation
28 StgBinding, StgExpr, StgRhs,
29 StgCaseAlts, StgCaseDefault,
34 pprStgBinding, pprStgBindings, pprStgBindingsWithSRTs,
35 getArgPrimRep, pprStgAlts,
36 isLitLitArg, isDllConApp, isStgTypeArg,
38 collectFinalStgBinders
45 #include "HsVersions.h"
47 import CostCentre ( CostCentreStack, CostCentre )
48 import Id ( Id, idName, idPrimRep, idType )
49 import Name ( isDllName )
50 import Literal ( Literal, literalType, isLitLitLit, literalPrimRep )
51 import DataCon ( DataCon, dataConName )
52 import PrimOp ( PrimOp )
55 import TyCon ( TyCon )
56 import UniqSet ( isEmptyUniqSet, uniqSetToList, UniqSet )
59 %************************************************************************
61 \subsection{@GenStgBinding@}
63 %************************************************************************
65 As usual, expressions are interesting; other things are boring. Here
66 are the boring things [except note the @GenStgRhs@], parameterised
67 with respect to binder and occurrence information (just as in
71 data GenStgBinding bndr occ
72 = StgNonRec bndr (GenStgRhs bndr occ)
73 | StgRec [(bndr, GenStgRhs bndr occ)]
76 %************************************************************************
78 \subsection{@GenStgArg@}
80 %************************************************************************
86 | StgTypeArg Type -- For when we want to preserve all type info
90 getArgPrimRep (StgVarArg local) = idPrimRep local
91 getArgPrimRep (StgLitArg lit) = literalPrimRep lit
93 isLitLitArg (StgLitArg lit) = isLitLitLit lit
96 isStgTypeArg (StgTypeArg _) = True
97 isStgTypeArg other = False
99 isDllArg :: StgArg -> Bool
100 -- Does this argument refer to something in a different DLL?
101 isDllArg (StgVarArg v) = isDllName (idName v)
102 isDllArg (StgLitArg lit) = isLitLitLit lit
104 isDllConApp :: DataCon -> [StgArg] -> Bool
105 -- Does this constructor application refer to
106 -- anything in a different DLL?
107 -- If so, we can't allocate it statically
108 isDllConApp con args = isDllName (dataConName con) || any isDllArg args
110 stgArgType :: StgArg -> Type
111 -- Very half baked becase we have lost the type arguments
112 stgArgType (StgVarArg v) = idType v
113 stgArgType (StgLitArg lit) = literalType lit
116 %************************************************************************
118 \subsection{STG expressions}
120 %************************************************************************
122 The @GenStgExpr@ data type is parameterised on binder and occurrence
125 %************************************************************************
127 \subsubsection{@GenStgExpr@ application}
129 %************************************************************************
131 An application is of a function to a list of atoms [not expressions].
132 Operationally, we want to push the arguments on the stack and call the
133 function. (If the arguments were expressions, we would have to build
134 their closures first.)
136 There is no constructor for a lone variable; it would appear as
139 type GenStgLiveVars occ = UniqSet occ
141 data GenStgExpr bndr occ
144 [GenStgArg occ] -- arguments; may be empty
147 %************************************************************************
149 \subsubsection{@StgConApp@ and @StgPrimApp@---saturated applications}
151 %************************************************************************
153 There are a specialised forms of application, for
154 constructors, primitives, and literals.
159 [GenStgArg occ] -- Saturated
162 [GenStgArg occ] -- Saturated
163 Type -- Result type; we need to know the result type
164 -- so that we can assign result registers.
167 %************************************************************************
169 \subsubsection{@StgLam@}
171 %************************************************************************
173 StgLam is used *only* during CoreToStg's work. Before CoreToStg has finished
174 it encodes (\x -> e) as (let f = \x -> e in f)
178 Type -- Type of whole lambda (useful when making a binder for it)
180 StgExpr -- Body of lambda
184 %************************************************************************
186 \subsubsection{@GenStgExpr@: case-expressions}
188 %************************************************************************
190 This has the same boxed/unboxed business as Core case expressions.
193 (GenStgExpr bndr occ)
194 -- the thing to examine
196 (GenStgLiveVars occ) -- Live vars of whole case
197 -- expression; i.e., those which mustn't be
200 (GenStgLiveVars occ) -- Live vars of RHSs;
201 -- i.e., those which must be saved before eval.
203 -- note that an alt's constructor's
204 -- binder-variables are NOT counted in the
205 -- free vars for the alt's RHS
207 bndr -- binds the result of evaluating the scrutinee
209 SRT -- The SRT for the continuation
211 (GenStgCaseAlts bndr occ)
214 %************************************************************************
216 \subsubsection{@GenStgExpr@: @let(rec)@-expressions}
218 %************************************************************************
220 The various forms of let(rec)-expression encode most of the
221 interesting things we want to do.
225 let-closure x = [free-vars] expr [args]
230 let x = (\free-vars -> \args -> expr) free-vars
232 \tr{args} may be empty (and is for most closures). It isn't under
233 circumstances like this:
239 let-closure x = [z] [y] (y+z)
241 The idea is that we compile code for @(y+z)@ in an environment in which
242 @z@ is bound to an offset from \tr{Node}, and @y@ is bound to an
243 offset from the stack pointer.
245 (A let-closure is an @StgLet@ with a @StgRhsClosure@ RHS.)
249 let-constructor x = Constructor [args]
253 (A let-constructor is an @StgLet@ with a @StgRhsCon@ RHS.)
256 Letrec-expressions are essentially the same deal as
257 let-closure/let-constructor, so we use a common structure and
258 distinguish between them with an @is_recursive@ boolean flag.
262 let-unboxed u = an arbitrary arithmetic expression in unboxed values
265 All the stuff on the RHS must be fully evaluated. No function calls either!
267 (We've backed away from this toward case-expressions with
268 suitably-magical alts ...)
271 ~[Advanced stuff here! Not to start with, but makes pattern matching
272 generate more efficient code.]
275 let-escapes-not fail = expr
278 Here the idea is that @e'@ guarantees not to put @fail@ in a data structure,
279 or pass it to another function. All @e'@ will ever do is tail-call @fail@.
280 Rather than build a closure for @fail@, all we need do is to record the stack
281 level at the moment of the @let-escapes-not@; then entering @fail@ is just
282 a matter of adjusting the stack pointer back down to that point and entering
287 f x y = let z = huge-expression in
293 (A let-escapes-not is an @StgLetNoEscape@.)
296 We may eventually want:
298 let-literal x = Literal
302 (ToDo: is this obsolete?)
305 And so the code for let(rec)-things:
308 (GenStgBinding bndr occ) -- right hand sides (see below)
309 (GenStgExpr bndr occ) -- body
311 | StgLetNoEscape -- remember: ``advanced stuff''
312 (GenStgLiveVars occ) -- Live in the whole let-expression
313 -- Mustn't overwrite these stack slots
314 -- *Doesn't* include binders of the let(rec).
316 (GenStgLiveVars occ) -- Live in the right hand sides (only)
317 -- These are the ones which must be saved on
318 -- the stack if they aren't there already
319 -- *Does* include binders of the let(rec) if recursive.
321 (GenStgBinding bndr occ) -- right hand sides (see below)
322 (GenStgExpr bndr occ) -- body
325 %************************************************************************
327 \subsubsection{@GenStgExpr@: @scc@ expressions}
329 %************************************************************************
331 Finally for @scc@ expressions we introduce a new STG construct.
335 CostCentre -- label of SCC expression
336 (GenStgExpr bndr occ) -- scc expression
340 %************************************************************************
342 \subsection{STG right-hand sides}
344 %************************************************************************
346 Here's the rest of the interesting stuff for @StgLet@s; the first
347 flavour is for closures:
349 data GenStgRhs bndr occ
351 CostCentreStack -- CCS to be attached (default is CurrentCCS)
352 StgBinderInfo -- Info about how this binder is used (see below)
353 SRT -- The closures's SRT
354 [occ] -- non-global free vars; a list, rather than
355 -- a set, because order is important
356 UpdateFlag -- ReEntrant | Updatable | SingleEntry
357 [bndr] -- arguments; if empty, then not a function;
358 -- as above, order is important.
359 (GenStgExpr bndr occ) -- body
361 An example may be in order. Consider:
363 let t = \x -> \y -> ... x ... y ... p ... q in e
365 Pulling out the free vars and stylising somewhat, we get the equivalent:
367 let t = (\[p,q] -> \[x,y] -> ... x ... y ... p ...q) p q
369 Stg-operationally, the @[x,y]@ are on the stack, the @[p,q]@ are
370 offsets from @Node@ into the closure, and the code ptr for the closure
371 will be exactly that in parentheses above.
373 The second flavour of right-hand-side is for constructors (simple but important):
376 CostCentreStack -- CCS to be attached (default is CurrentCCS).
377 -- Top-level (static) ones will end up with
378 -- DontCareCCS, because we don't count static
379 -- data in heap profiles, and we don't set CCCS
380 -- from static closure.
381 DataCon -- constructor
382 [GenStgArg occ] -- args
385 Here's the @StgBinderInfo@ type, and its combining op:
390 Bool -- At least one occurrence as an argument
392 Bool -- At least one occurrence in an unsaturated application
394 Bool -- This thing (f) has at least occurrence of the form:
395 -- x = [..] \u [] -> f a b c
396 -- where the application is saturated
398 Bool -- Ditto for non-updatable x.
400 Bool -- At least one fake application occurrence, that is
401 -- an StgApp f args where args is an empty list
402 -- This is due to the fact that we do not have a
403 -- StgVar constructor.
404 -- Used by the lambda lifter.
405 -- True => "at least one unsat app" is True too
407 stgArgOcc = StgBinderInfo True False False False False
408 stgUnsatOcc = StgBinderInfo False True False False False
409 stgStdHeapOcc = StgBinderInfo False False True False False
410 stgNoUpdHeapOcc = StgBinderInfo False False False True False
411 stgNormalOcc = StgBinderInfo False False False False False
412 -- [Andre] can't think of a good name for the last one.
413 stgFakeFunAppOcc = StgBinderInfo False True False False True
415 combineStgBinderInfo :: StgBinderInfo -> StgBinderInfo -> StgBinderInfo
417 combineStgBinderInfo NoStgBinderInfo info2 = info2
418 combineStgBinderInfo info1 NoStgBinderInfo = info1
419 combineStgBinderInfo (StgBinderInfo arg1 unsat1 std_heap1 upd_heap1 fkap1)
420 (StgBinderInfo arg2 unsat2 std_heap2 upd_heap2 fkap2)
421 = StgBinderInfo (arg1 || arg2)
423 (std_heap1 || std_heap2)
424 (upd_heap1 || upd_heap2)
428 %************************************************************************
430 \subsection[Stg-case-alternatives]{STG case alternatives}
432 %************************************************************************
434 Just like in @CoreSyntax@ (except no type-world stuff).
436 * Algebraic cases are done using
437 StgAlgAlts (Just tc) alts deflt
439 * Polymorphic cases, or case of a function type, are done using
440 StgAlgAlts Nothing [] (StgBindDefault e)
442 * Primitive cases are done using
443 StgPrimAlts tc alts deflt
445 We thought of giving polymorphic cases their own constructor,
446 but we get a bit more code sharing this way
448 The type constructor in StgAlgAlts/StgPrimAlts is guaranteed not
449 to be abstract; that is, we can see its representation. This is
450 important because the code generator uses it to determine return
451 conventions etc. But it's not trivial where there's a moduule loop
452 involved, because some versions of a type constructor might not have
453 all the constructors visible. So mkStgAlgAlts (in CoreToStg) ensures
454 that it gets the TyCon from the constructors or literals (which are
455 guaranteed to have the Real McCoy) rather than from the scrutinee type.
458 data GenStgCaseAlts bndr occ
459 = StgAlgAlts (Maybe TyCon) -- Just tc => scrutinee type is
460 -- an algebraic data type
461 -- Nothing => scrutinee type is a type
462 -- variable or function type
463 [(DataCon, -- alts: data constructor,
464 [bndr], -- constructor's parameters,
465 [Bool], -- "use mask", same length as
466 -- parameters; a True in a
467 -- param's position if it is
469 GenStgExpr bndr occ)] -- ...right-hand side.
470 (GenStgCaseDefault bndr occ)
473 [(Literal, -- alts: unboxed literal,
474 GenStgExpr bndr occ)] -- rhs.
475 (GenStgCaseDefault bndr occ)
477 data GenStgCaseDefault bndr occ
478 = StgNoDefault -- small con family: all
479 -- constructor accounted for
480 | StgBindDefault (GenStgExpr bndr occ)
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 StgCaseAlts = GenStgCaseAlts Id Id
498 type StgCaseDefault = GenStgCaseDefault Id Id
501 %************************************************************************
503 \subsubsection[UpdateFlag-datatype]{@UpdateFlag@}
505 %************************************************************************
507 This is also used in @LambdaFormInfo@ in the @ClosureInfo@ module.
509 A @ReEntrant@ closure may be entered multiple times, but should not be
510 updated or blackholed. An @Updatable@ closure should be updated after
511 evaluation (and may be blackholed during evaluation). A @SingleEntry@
512 closure will only be entered once, and so need not be updated but may
513 safely be blackholed.
516 data UpdateFlag = ReEntrant | Updatable | SingleEntry
518 instance Outputable UpdateFlag where
520 = char (case u of { ReEntrant -> 'r'; Updatable -> 'u'; SingleEntry -> 's' })
522 isUpdatable ReEntrant = False
523 isUpdatable SingleEntry = False
524 isUpdatable Updatable = True
527 %************************************************************************
529 \subsubsection[Static Reference Tables]{@SRT@}
531 %************************************************************************
533 There is one SRT per top-level function group. Each local binding and
534 case expression within this binding group has a subrange of the whole
535 SRT, expressed as an offset and length.
539 | SRT !Int{-offset-} !Int{-length-}
544 pprSRT (NoSRT) = ptext SLIT("_no_srt_")
545 pprSRT (SRT off len) = parens (ppr off <> comma <> ppr len)
548 %************************************************************************
550 \subsection[Stg-utility-functions]{Utility functions}
552 %************************************************************************
555 For doing interfaces, we want the exported top-level Ids from the
556 final pre-codegen STG code, so as to be sure we have the
557 latest/greatest pragma info.
560 collectFinalStgBinders
561 :: [StgBinding] -- input program
564 collectFinalStgBinders [] = []
565 collectFinalStgBinders (StgNonRec b _ : binds) = b : collectFinalStgBinders binds
566 collectFinalStgBinders (StgRec bs : binds) = map fst bs ++ collectFinalStgBinders binds
569 %************************************************************************
571 \subsection[Stg-pretty-printing]{Pretty-printing}
573 %************************************************************************
575 Robin Popplestone asked for semi-colon separators on STG binds; here's
576 hoping he likes terminators instead... Ditto for case alternatives.
579 pprGenStgBinding :: (Outputable bndr, Outputable bdee, Ord bdee)
580 => GenStgBinding bndr bdee -> SDoc
582 pprGenStgBinding (StgNonRec bndr rhs)
583 = hang (hsep [ppr bndr, equals])
584 4 ((<>) (ppr rhs) semi)
586 pprGenStgBinding (StgRec pairs)
587 = vcat ((ifPprDebug (ptext SLIT("{- StgRec (begin) -}"))) :
588 (map (ppr_bind) pairs) ++ [(ifPprDebug (ptext SLIT("{- StgRec (end) -}")))])
590 ppr_bind (bndr, expr)
591 = hang (hsep [ppr bndr, equals])
592 4 ((<>) (ppr expr) semi)
594 pprStgBinding :: StgBinding -> SDoc
595 pprStgBinding bind = pprGenStgBinding bind
597 pprStgBindings :: [StgBinding] -> SDoc
598 pprStgBindings binds = vcat (map pprGenStgBinding binds)
600 pprGenStgBindingWithSRT
601 :: (Outputable bndr, Outputable bdee, Ord bdee)
602 => (GenStgBinding bndr bdee,[Id]) -> SDoc
604 pprGenStgBindingWithSRT (bind,srt)
605 = vcat [ pprGenStgBinding bind,
606 ptext SLIT("SRT: ") <> ppr srt ]
608 pprStgBindingsWithSRTs :: [(StgBinding,[Id])] -> SDoc
609 pprStgBindingsWithSRTs binds = vcat (map pprGenStgBindingWithSRT binds)
613 instance (Outputable bdee) => Outputable (GenStgArg bdee) where
616 instance (Outputable bndr, Outputable bdee, Ord bdee)
617 => Outputable (GenStgBinding bndr bdee) where
618 ppr = pprGenStgBinding
620 instance (Outputable bndr, Outputable bdee, Ord bdee)
621 => Outputable (GenStgExpr bndr bdee) where
624 instance (Outputable bndr, Outputable bdee, Ord bdee)
625 => Outputable (GenStgRhs bndr bdee) where
626 ppr rhs = pprStgRhs rhs
630 pprStgArg :: (Outputable bdee) => GenStgArg bdee -> SDoc
632 pprStgArg (StgVarArg var) = ppr var
633 pprStgArg (StgLitArg con) = ppr con
634 pprStgArg (StgTypeArg ty) = char '@' <+> ppr ty
638 pprStgExpr :: (Outputable bndr, Outputable bdee, Ord bdee)
639 => GenStgExpr bndr bdee -> SDoc
641 pprStgExpr (StgLit lit) = ppr lit
644 pprStgExpr (StgApp func args)
646 4 (sep (map (ppr) args))
650 pprStgExpr (StgConApp con args)
651 = hsep [ ppr con, brackets (interppSP args)]
653 pprStgExpr (StgPrimApp op args _)
654 = hsep [ ppr op, brackets (interppSP args)]
656 pprStgExpr (StgLam _ bndrs body)
657 =sep [ char '\\' <+> ppr bndrs <+> ptext SLIT("->"),
662 -- special case: let v = <very specific thing>
668 -- Very special! Suspicious! (SLPJ)
670 pprStgExpr (StgLet (StgNonRec bndr (StgRhsClosure cc bi srt free_vars upd_flag args rhs))
673 (hang (hcat [ptext SLIT("let { "), ppr bndr, ptext SLIT(" = "),
676 ptext SLIT(" ["), ifPprDebug (interppSP free_vars), ptext SLIT("] \\"),
677 ppr upd_flag, ptext SLIT(" ["),
678 interppSP args, char ']'])
679 8 (sep [hsep [ppr rhs, ptext SLIT("} in")]]))
682 -- special case: let ... in let ...
684 pprStgExpr (StgLet bind expr@(StgLet _ _))
686 (sep [hang (ptext SLIT("let {")) 2 (hsep [pprGenStgBinding bind, ptext SLIT("} in")])])
690 pprStgExpr (StgLet bind expr)
691 = sep [hang (ptext SLIT("let {")) 2 (pprGenStgBinding bind),
692 hang (ptext SLIT("} in ")) 2 (ppr expr)]
694 pprStgExpr (StgLetNoEscape lvs_whole lvs_rhss bind expr)
695 = sep [hang (ptext SLIT("let-no-escape {"))
696 2 (pprGenStgBinding bind),
697 hang ((<>) (ptext SLIT("} in "))
700 hcat [ptext SLIT("-- lvs: ["), interppSP (uniqSetToList lvs_whole),
701 ptext SLIT("]; rhs lvs: ["), interppSP (uniqSetToList lvs_rhss),
707 pprStgExpr (StgSCC cc expr)
708 = sep [ hsep [ptext SLIT("_scc_"), ppr cc],
713 pprStgExpr (StgCase expr lvs_whole lvs_rhss bndr srt alts)
714 = sep [sep [ptext SLIT("case"),
715 nest 4 (hsep [pprStgExpr expr,
716 ifPprDebug (dcolon <+> pp_ty alts)]),
717 ptext SLIT("of"), ppr bndr, char '{'],
720 hcat [ptext SLIT("-- lvs: ["), interppSP (uniqSetToList lvs_whole),
721 ptext SLIT("]; rhs lvs: ["), interppSP (uniqSetToList lvs_rhss),
724 nest 2 (pprStgAlts alts),
727 pp_ty (StgAlgAlts maybe_tycon _ _) = ppr maybe_tycon
728 pp_ty (StgPrimAlts tycon _ _) = ppr tycon
730 pprStgAlts (StgAlgAlts _ alts deflt)
731 = vcat [ vcat (map (ppr_bxd_alt) alts),
732 pprStgDefault deflt ]
734 ppr_bxd_alt (con, params, use_mask, expr)
735 = hang (hsep [ppr con, interppSP params, ptext SLIT("->")])
736 4 ((<>) (ppr expr) semi)
738 pprStgAlts (StgPrimAlts _ alts deflt)
739 = vcat [ vcat (map (ppr_ubxd_alt) alts),
740 pprStgDefault deflt ]
742 ppr_ubxd_alt (lit, expr)
743 = hang (hsep [ppr lit, ptext SLIT("->")])
744 4 ((<>) (ppr expr) semi)
746 pprStgDefault StgNoDefault = empty
747 pprStgDefault (StgBindDefault expr) = hang (hsep [ptext SLIT("DEFAULT"), ptext SLIT("->")])
753 pprStgLVs :: Outputable occ => GenStgLiveVars occ -> SDoc
755 = getPprStyle $ \ sty ->
756 if userStyle sty || isEmptyUniqSet lvs then
759 hcat [text "{-lvs:", interpp'SP (uniqSetToList lvs), text "-}"]
763 pprStgRhs :: (Outputable bndr, Outputable bdee, Ord bdee)
764 => GenStgRhs bndr bdee -> SDoc
767 pprStgRhs (StgRhsClosure cc bi srt [free_var] upd_flag [{-no args-}] (StgApp func []))
771 brackets (ifPprDebug (ppr free_var)),
772 ptext SLIT(" \\"), ppr upd_flag, ptext SLIT(" [] "), ppr func ]
775 pprStgRhs (StgRhsClosure cc bi srt free_vars upd_flag args body)
776 = hang (hcat [ppr cc,
779 brackets (ifPprDebug (interppSP free_vars)),
780 ptext SLIT(" \\"), ppr upd_flag, brackets (interppSP args)])
783 pprStgRhs (StgRhsCon cc con args)
785 space, ppr con, ptext SLIT("! "), brackets (interppSP args)]
787 pprMaybeSRT (NoSRT) = empty
788 pprMaybeSRT srt = ptext SLIT(" srt: ") <> pprSRT srt
792 pp_binder_info NoStgBinderInfo = empty
794 -- cases so boring that we print nothing
795 pp_binder_info (StgBinderInfo True b c d e) = empty
798 pp_binder_info (StgBinderInfo a b c d e)
799 = getPprStyle $ \ sty ->
800 if userStyle sty then
803 parens (hsep (punctuate comma (map ppr [a,b,c,d,e])))
806 Collect @IdInfo@ stuff that is most easily just snaffled straight
807 from the STG bindings.
810 stgArity :: StgRhs -> Int
812 stgArity (StgRhsCon _ _ _) = 0 -- it's a constructor, fully applied
813 stgArity (StgRhsClosure _ _ _ _ _ args _ ) = length args