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,
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, isNullaryDataCon )
52 import PrimOp ( PrimOp )
53 import PrimRep ( PrimRep(..) )
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).
437 data GenStgCaseAlts bndr occ
438 = StgAlgAlts Type -- so we can find out things about constructor family
439 [(DataCon, -- alts: data constructor,
440 [bndr], -- constructor's parameters,
441 [Bool], -- "use mask", same length as
442 -- parameters; a True in a
443 -- param's position if it is
445 GenStgExpr bndr occ)] -- ...right-hand side.
446 (GenStgCaseDefault bndr occ)
447 | StgPrimAlts Type -- so we can find out things about constructor family
448 [(Literal, -- alts: unboxed literal,
449 GenStgExpr bndr occ)] -- rhs.
450 (GenStgCaseDefault bndr occ)
452 data GenStgCaseDefault bndr occ
453 = StgNoDefault -- small con family: all
454 -- constructor accounted for
455 | StgBindDefault (GenStgExpr bndr occ)
458 %************************************************************************
460 \subsection[Stg]{The Plain STG parameterisation}
462 %************************************************************************
464 This happens to be the only one we use at the moment.
467 type StgBinding = GenStgBinding Id Id
468 type StgArg = GenStgArg Id
469 type StgLiveVars = GenStgLiveVars Id
470 type StgExpr = GenStgExpr Id Id
471 type StgRhs = GenStgRhs Id Id
472 type StgCaseAlts = GenStgCaseAlts Id Id
473 type StgCaseDefault = GenStgCaseDefault Id Id
476 %************************************************************************
478 \subsubsection[UpdateFlag-datatype]{@UpdateFlag@}
480 %************************************************************************
482 This is also used in @LambdaFormInfo@ in the @ClosureInfo@ module.
484 A @ReEntrant@ closure may be entered multiple times, but should not be
485 updated or blackholed. An @Updatable@ closure should be updated after
486 evaluation (and may be blackholed during evaluation). A @SingleEntry@
487 closure will only be entered once, and so need not be updated but may
488 safely be blackholed.
491 data UpdateFlag = ReEntrant | Updatable | SingleEntry
493 instance Outputable UpdateFlag where
495 = char (case u of { ReEntrant -> 'r'; Updatable -> 'u'; SingleEntry -> 's' })
497 isUpdatable ReEntrant = False
498 isUpdatable SingleEntry = False
499 isUpdatable Updatable = True
502 %************************************************************************
504 \subsubsection[Static Reference Tables]{@SRT@}
506 %************************************************************************
508 There is one SRT per top-level function group. Each local binding and
509 case expression within this binding group has a subrange of the whole
510 SRT, expressed as an offset and length.
514 | SRT !Int{-offset-} !Int{-length-}
519 pprSRT (NoSRT) = ptext SLIT("_no_srt_")
520 pprSRT (SRT off len) = parens (ppr off <> comma <> ppr len)
523 %************************************************************************
525 \subsection[Stg-utility-functions]{Utility functions}
527 %************************************************************************
530 For doing interfaces, we want the exported top-level Ids from the
531 final pre-codegen STG code, so as to be sure we have the
532 latest/greatest pragma info.
535 collectFinalStgBinders
536 :: [StgBinding] -- input program
539 collectFinalStgBinders [] = []
540 collectFinalStgBinders (StgNonRec b _ : binds) = b : collectFinalStgBinders binds
541 collectFinalStgBinders (StgRec bs : binds) = map fst bs ++ collectFinalStgBinders binds
544 %************************************************************************
546 \subsection[Stg-pretty-printing]{Pretty-printing}
548 %************************************************************************
550 Robin Popplestone asked for semi-colon separators on STG binds; here's
551 hoping he likes terminators instead... Ditto for case alternatives.
554 pprGenStgBinding :: (Outputable bndr, Outputable bdee, Ord bdee)
555 => GenStgBinding bndr bdee -> SDoc
557 pprGenStgBinding (StgNonRec bndr rhs)
558 = hang (hsep [ppr bndr, equals])
559 4 ((<>) (ppr rhs) semi)
561 pprGenStgBinding (StgRec pairs)
562 = vcat ((ifPprDebug (ptext SLIT("{- StgRec (begin) -}"))) :
563 (map (ppr_bind) pairs) ++ [(ifPprDebug (ptext SLIT("{- StgRec (end) -}")))])
565 ppr_bind (bndr, expr)
566 = hang (hsep [ppr bndr, equals])
567 4 ((<>) (ppr expr) semi)
569 pprStgBinding :: StgBinding -> SDoc
570 pprStgBinding bind = pprGenStgBinding bind
572 pprStgBindings :: [StgBinding] -> SDoc
573 pprStgBindings binds = vcat (map pprGenStgBinding binds)
575 pprGenStgBindingWithSRT
576 :: (Outputable bndr, Outputable bdee, Ord bdee)
577 => (GenStgBinding bndr bdee,[Id]) -> SDoc
579 pprGenStgBindingWithSRT (bind,srt)
580 = vcat [ pprGenStgBinding bind,
581 ptext SLIT("SRT: ") <> ppr srt ]
583 pprStgBindingsWithSRTs :: [(StgBinding,[Id])] -> SDoc
584 pprStgBindingsWithSRTs binds = vcat (map pprGenStgBindingWithSRT binds)
588 instance (Outputable bdee) => Outputable (GenStgArg bdee) where
591 instance (Outputable bndr, Outputable bdee, Ord bdee)
592 => Outputable (GenStgBinding bndr bdee) where
593 ppr = pprGenStgBinding
595 instance (Outputable bndr, Outputable bdee, Ord bdee)
596 => Outputable (GenStgExpr bndr bdee) where
599 instance (Outputable bndr, Outputable bdee, Ord bdee)
600 => Outputable (GenStgRhs bndr bdee) where
601 ppr rhs = pprStgRhs rhs
605 pprStgArg :: (Outputable bdee) => GenStgArg bdee -> SDoc
607 pprStgArg (StgVarArg var) = ppr var
608 pprStgArg (StgLitArg con) = ppr con
609 pprStgARg (StgTypeArg ty) = char '@' <+> ppr ty
613 pprStgExpr :: (Outputable bndr, Outputable bdee, Ord bdee)
614 => GenStgExpr bndr bdee -> SDoc
616 pprStgExpr (StgLit lit) = ppr lit
619 pprStgExpr (StgApp func args)
621 4 (sep (map (ppr) args))
625 pprStgExpr (StgConApp con args)
626 = hsep [ ppr con, brackets (interppSP args)]
628 pprStgExpr (StgPrimApp op args _)
629 = hsep [ ppr op, brackets (interppSP args)]
631 pprStgExpr (StgLam _ bndrs body)
632 =sep [ char '\\' <+> ppr bndrs <+> ptext SLIT("->"),
637 -- special case: let v = <very specific thing>
643 -- Very special! Suspicious! (SLPJ)
645 pprStgExpr (StgLet (StgNonRec bndr (StgRhsClosure cc bi srt free_vars upd_flag args rhs))
648 (hang (hcat [ptext SLIT("let { "), ppr bndr, ptext SLIT(" = "),
651 ptext SLIT(" ["), ifPprDebug (interppSP free_vars), ptext SLIT("] \\"),
652 ppr upd_flag, ptext SLIT(" ["),
653 interppSP args, char ']'])
654 8 (sep [hsep [ppr rhs, ptext SLIT("} in")]]))
657 -- special case: let ... in let ...
659 pprStgExpr (StgLet bind expr@(StgLet _ _))
661 (sep [hang (ptext SLIT("let {")) 2 (hsep [pprGenStgBinding bind, ptext SLIT("} in")])])
665 pprStgExpr (StgLet bind expr)
666 = sep [hang (ptext SLIT("let {")) 2 (pprGenStgBinding bind),
667 hang (ptext SLIT("} in ")) 2 (ppr expr)]
669 pprStgExpr (StgLetNoEscape lvs_whole lvs_rhss bind expr)
670 = sep [hang (ptext SLIT("let-no-escape {"))
671 2 (pprGenStgBinding bind),
672 hang ((<>) (ptext SLIT("} in "))
675 hcat [ptext SLIT("-- lvs: ["), interppSP (uniqSetToList lvs_whole),
676 ptext SLIT("]; rhs lvs: ["), interppSP (uniqSetToList lvs_rhss),
682 pprStgExpr (StgSCC cc expr)
683 = sep [ hsep [ptext SLIT("_scc_"), ppr cc],
688 pprStgExpr (StgCase expr lvs_whole lvs_rhss bndr srt alts)
689 = sep [sep [ptext SLIT("case"),
690 nest 4 (hsep [pprStgExpr expr,
691 ifPprDebug (dcolon <+> pp_ty alts)]),
692 ptext SLIT("of"), ppr bndr, char '{'],
695 hcat [ptext SLIT("-- lvs: ["), interppSP (uniqSetToList lvs_whole),
696 ptext SLIT("]; rhs lvs: ["), interppSP (uniqSetToList lvs_rhss),
699 nest 2 (ppr_alts alts),
702 ppr_default StgNoDefault = empty
703 ppr_default (StgBindDefault expr)
704 = hang (hsep [ptext SLIT("DEFAULT"), ptext SLIT("->")]) 4 (ppr expr)
706 pp_ty (StgAlgAlts ty _ _) = ppr ty
707 pp_ty (StgPrimAlts ty _ _) = ppr ty
709 ppr_alts (StgAlgAlts ty alts deflt)
710 = vcat [ vcat (map (ppr_bxd_alt) alts),
713 ppr_bxd_alt (con, params, use_mask, expr)
714 = hang (hsep [ppr con, interppSP params, ptext SLIT("->")])
715 4 ((<>) (ppr expr) semi)
717 ppr_alts (StgPrimAlts ty alts deflt)
718 = vcat [ vcat (map (ppr_ubxd_alt) alts),
721 ppr_ubxd_alt (lit, expr)
722 = hang (hsep [ppr lit, ptext SLIT("->")])
723 4 ((<>) (ppr expr) semi)
727 pprStgLVs :: Outputable occ => GenStgLiveVars occ -> SDoc
729 = getPprStyle $ \ sty ->
730 if userStyle sty || isEmptyUniqSet lvs then
733 hcat [text "{-lvs:", interpp'SP (uniqSetToList lvs), text "-}"]
737 pprStgRhs :: (Outputable bndr, Outputable bdee, Ord bdee)
738 => GenStgRhs bndr bdee -> SDoc
741 pprStgRhs (StgRhsClosure cc bi srt [free_var] upd_flag [{-no args-}] (StgApp func []))
745 brackets (ifPprDebug (ppr free_var)),
746 ptext SLIT(" \\"), ppr upd_flag, ptext SLIT(" [] "), ppr func ]
749 pprStgRhs (StgRhsClosure cc bi srt free_vars upd_flag args body)
750 = hang (hcat [ppr cc,
753 brackets (ifPprDebug (interppSP free_vars)),
754 ptext SLIT(" \\"), ppr upd_flag, brackets (interppSP args)])
757 pprStgRhs (StgRhsCon cc con args)
759 space, ppr con, ptext SLIT("! "), brackets (interppSP args)]
761 pprMaybeSRT (NoSRT) = empty
762 pprMaybeSRT srt = ptext SLIT(" srt: ") <> pprSRT srt
766 pp_binder_info NoStgBinderInfo = empty
768 -- cases so boring that we print nothing
769 pp_binder_info (StgBinderInfo True b c d e) = empty
772 pp_binder_info (StgBinderInfo a b c d e)
773 = getPprStyle $ \ sty ->
774 if userStyle sty then
777 parens (hsep (punctuate comma (map ppr [a,b,c,d,e])))
780 Collect @IdInfo@ stuff that is most easily just snaffled straight
781 from the STG bindings.
784 stgArity :: StgRhs -> Int
786 stgArity (StgRhsCon _ _ _) = 0 -- it's a constructor, fully applied
787 stgArity (StgRhsClosure _ _ _ _ _ args _ ) = length args