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,
33 pprStgBinding, pprStgBindings, pprStgBindingsWithSRTs,
34 getArgPrimRep, pprStgAlts,
35 isLitLitArg, isDllConApp, isStgTypeArg,
43 #include "HsVersions.h"
45 import CostCentre ( CostCentreStack, CostCentre )
46 import Id ( Id, idName, idPrimRep, idType )
47 import Name ( isDllName )
48 import Literal ( Literal, literalType, isLitLitLit, literalPrimRep )
49 import DataCon ( DataCon, dataConName )
50 import PrimOp ( PrimOp )
53 import TyCon ( TyCon )
54 import UniqSet ( isEmptyUniqSet, uniqSetToList, UniqSet )
57 %************************************************************************
59 \subsection{@GenStgBinding@}
61 %************************************************************************
63 As usual, expressions are interesting; other things are boring. Here
64 are the boring things [except note the @GenStgRhs@], parameterised
65 with respect to binder and occurrence information (just as in
69 data GenStgBinding bndr occ
70 = StgNonRec bndr (GenStgRhs bndr occ)
71 | StgRec [(bndr, GenStgRhs bndr occ)]
74 %************************************************************************
76 \subsection{@GenStgArg@}
78 %************************************************************************
84 | StgTypeArg Type -- For when we want to preserve all type info
88 getArgPrimRep (StgVarArg local) = idPrimRep local
89 getArgPrimRep (StgLitArg lit) = literalPrimRep lit
91 isLitLitArg (StgLitArg lit) = isLitLitLit lit
94 isStgTypeArg (StgTypeArg _) = True
95 isStgTypeArg other = False
97 isDllArg :: StgArg -> Bool
98 -- Does this argument refer to something in a different DLL?
99 isDllArg (StgVarArg v) = isDllName (idName v)
100 isDllArg (StgLitArg lit) = isLitLitLit lit
102 isDllConApp :: DataCon -> [StgArg] -> Bool
103 -- Does this constructor application refer to
104 -- anything in a different DLL?
105 -- If so, we can't allocate it statically
106 isDllConApp con args = isDllName (dataConName con) || any isDllArg args
108 stgArgType :: StgArg -> Type
109 -- Very half baked becase we have lost the type arguments
110 stgArgType (StgVarArg v) = idType v
111 stgArgType (StgLitArg lit) = literalType lit
114 %************************************************************************
116 \subsection{STG expressions}
118 %************************************************************************
120 The @GenStgExpr@ data type is parameterised on binder and occurrence
123 %************************************************************************
125 \subsubsection{@GenStgExpr@ application}
127 %************************************************************************
129 An application is of a function to a list of atoms [not expressions].
130 Operationally, we want to push the arguments on the stack and call the
131 function. (If the arguments were expressions, we would have to build
132 their closures first.)
134 There is no constructor for a lone variable; it would appear as
137 type GenStgLiveVars occ = UniqSet occ
139 data GenStgExpr bndr occ
142 [GenStgArg occ] -- arguments; may be empty
145 %************************************************************************
147 \subsubsection{@StgConApp@ and @StgPrimApp@---saturated applications}
149 %************************************************************************
151 There are a specialised forms of application, for
152 constructors, primitives, and literals.
157 [GenStgArg occ] -- Saturated
160 [GenStgArg occ] -- Saturated
161 Type -- Result type; we need to know the result type
162 -- so that we can assign result registers.
165 %************************************************************************
167 \subsubsection{@StgLam@}
169 %************************************************************************
171 StgLam is used *only* during CoreToStg's work. Before CoreToStg has finished
172 it encodes (\x -> e) as (let f = \x -> e in f)
176 Type -- Type of whole lambda (useful when making a binder for it)
178 StgExpr -- Body of lambda
182 %************************************************************************
184 \subsubsection{@GenStgExpr@: case-expressions}
186 %************************************************************************
188 This has the same boxed/unboxed business as Core case expressions.
191 (GenStgExpr bndr occ)
192 -- the thing to examine
194 (GenStgLiveVars occ) -- Live vars of whole case
195 -- expression; i.e., those which mustn't be
198 (GenStgLiveVars occ) -- Live vars of RHSs;
199 -- i.e., those which must be saved before eval.
201 -- note that an alt's constructor's
202 -- binder-variables are NOT counted in the
203 -- free vars for the alt's RHS
205 bndr -- binds the result of evaluating the scrutinee
207 SRT -- The SRT for the continuation
209 (GenStgCaseAlts bndr occ)
212 %************************************************************************
214 \subsubsection{@GenStgExpr@: @let(rec)@-expressions}
216 %************************************************************************
218 The various forms of let(rec)-expression encode most of the
219 interesting things we want to do.
223 let-closure x = [free-vars] expr [args]
228 let x = (\free-vars -> \args -> expr) free-vars
230 \tr{args} may be empty (and is for most closures). It isn't under
231 circumstances like this:
237 let-closure x = [z] [y] (y+z)
239 The idea is that we compile code for @(y+z)@ in an environment in which
240 @z@ is bound to an offset from \tr{Node}, and @y@ is bound to an
241 offset from the stack pointer.
243 (A let-closure is an @StgLet@ with a @StgRhsClosure@ RHS.)
247 let-constructor x = Constructor [args]
251 (A let-constructor is an @StgLet@ with a @StgRhsCon@ RHS.)
254 Letrec-expressions are essentially the same deal as
255 let-closure/let-constructor, so we use a common structure and
256 distinguish between them with an @is_recursive@ boolean flag.
260 let-unboxed u = an arbitrary arithmetic expression in unboxed values
263 All the stuff on the RHS must be fully evaluated. No function calls either!
265 (We've backed away from this toward case-expressions with
266 suitably-magical alts ...)
269 ~[Advanced stuff here! Not to start with, but makes pattern matching
270 generate more efficient code.]
273 let-escapes-not fail = expr
276 Here the idea is that @e'@ guarantees not to put @fail@ in a data structure,
277 or pass it to another function. All @e'@ will ever do is tail-call @fail@.
278 Rather than build a closure for @fail@, all we need do is to record the stack
279 level at the moment of the @let-escapes-not@; then entering @fail@ is just
280 a matter of adjusting the stack pointer back down to that point and entering
285 f x y = let z = huge-expression in
291 (A let-escapes-not is an @StgLetNoEscape@.)
294 We may eventually want:
296 let-literal x = Literal
300 (ToDo: is this obsolete?)
303 And so the code for let(rec)-things:
306 (GenStgBinding bndr occ) -- right hand sides (see below)
307 (GenStgExpr bndr occ) -- body
309 | StgLetNoEscape -- remember: ``advanced stuff''
310 (GenStgLiveVars occ) -- Live in the whole let-expression
311 -- Mustn't overwrite these stack slots
312 -- *Doesn't* include binders of the let(rec).
314 (GenStgLiveVars occ) -- Live in the right hand sides (only)
315 -- These are the ones which must be saved on
316 -- the stack if they aren't there already
317 -- *Does* include binders of the let(rec) if recursive.
319 (GenStgBinding bndr occ) -- right hand sides (see below)
320 (GenStgExpr bndr occ) -- body
323 %************************************************************************
325 \subsubsection{@GenStgExpr@: @scc@ expressions}
327 %************************************************************************
329 Finally for @scc@ expressions we introduce a new STG construct.
333 CostCentre -- label of SCC expression
334 (GenStgExpr bndr occ) -- scc expression
338 %************************************************************************
340 \subsection{STG right-hand sides}
342 %************************************************************************
344 Here's the rest of the interesting stuff for @StgLet@s; the first
345 flavour is for closures:
347 data GenStgRhs bndr occ
349 CostCentreStack -- CCS to be attached (default is CurrentCCS)
350 StgBinderInfo -- Info about how this binder is used (see below)
351 SRT -- The closures's SRT
352 [occ] -- non-global free vars; a list, rather than
353 -- a set, because order is important
354 UpdateFlag -- ReEntrant | Updatable | SingleEntry
355 [bndr] -- arguments; if empty, then not a function;
356 -- as above, order is important.
357 (GenStgExpr bndr occ) -- body
359 An example may be in order. Consider:
361 let t = \x -> \y -> ... x ... y ... p ... q in e
363 Pulling out the free vars and stylising somewhat, we get the equivalent:
365 let t = (\[p,q] -> \[x,y] -> ... x ... y ... p ...q) p q
367 Stg-operationally, the @[x,y]@ are on the stack, the @[p,q]@ are
368 offsets from @Node@ into the closure, and the code ptr for the closure
369 will be exactly that in parentheses above.
371 The second flavour of right-hand-side is for constructors (simple but important):
374 CostCentreStack -- CCS to be attached (default is CurrentCCS).
375 -- Top-level (static) ones will end up with
376 -- DontCareCCS, because we don't count static
377 -- data in heap profiles, and we don't set CCCS
378 -- from static closure.
379 DataCon -- constructor
380 [GenStgArg occ] -- args
383 Here's the @StgBinderInfo@ type, and its combining op:
387 | SatCallsOnly -- All occurrences are *saturated* *function* calls
388 -- This means we don't need to build an info table and
389 -- slow entry code for the thing
390 -- Thunks never get this value
392 noBinderInfo = NoStgBinderInfo
393 stgUnsatOcc = NoStgBinderInfo
394 stgSatOcc = SatCallsOnly
396 satCallsOnly :: StgBinderInfo -> Bool
397 satCallsOnly SatCallsOnly = True
398 satCallsOnly NoStgBinderInfo = False
400 combineStgBinderInfo :: StgBinderInfo -> StgBinderInfo -> StgBinderInfo
401 combineStgBinderInfo SatCallsOnly SatCallsOnly = SatCallsOnly
402 combineStgBinderInfo info1 info2 = NoStgBinderInfo
405 pp_binder_info NoStgBinderInfo = empty
406 pp_binder_info SatCallsOnly = ptext SLIT("sat-only")
409 %************************************************************************
411 \subsection[Stg-case-alternatives]{STG case alternatives}
413 %************************************************************************
415 Just like in @CoreSyntax@ (except no type-world stuff).
417 * Algebraic cases are done using
418 StgAlgAlts (Just tc) alts deflt
420 * Polymorphic cases, or case of a function type, are done using
421 StgAlgAlts Nothing [] (StgBindDefault e)
423 * Primitive cases are done using
424 StgPrimAlts tc alts deflt
426 We thought of giving polymorphic cases their own constructor,
427 but we get a bit more code sharing this way
429 The type constructor in StgAlgAlts/StgPrimAlts is guaranteed not
430 to be abstract; that is, we can see its representation. This is
431 important because the code generator uses it to determine return
432 conventions etc. But it's not trivial where there's a moduule loop
433 involved, because some versions of a type constructor might not have
434 all the constructors visible. So mkStgAlgAlts (in CoreToStg) ensures
435 that it gets the TyCon from the constructors or literals (which are
436 guaranteed to have the Real McCoy) rather than from the scrutinee type.
439 data GenStgCaseAlts bndr occ
440 = StgAlgAlts (Maybe TyCon) -- Just tc => scrutinee type is
441 -- an algebraic data type
442 -- Nothing => scrutinee type is a type
443 -- variable or function type
444 [(DataCon, -- alts: data constructor,
445 [bndr], -- constructor's parameters,
446 [Bool], -- "use mask", same length as
447 -- parameters; a True in a
448 -- param's position if it is
450 GenStgExpr bndr occ)] -- ...right-hand side.
451 (GenStgCaseDefault bndr occ)
454 [(Literal, -- alts: unboxed literal,
455 GenStgExpr bndr occ)] -- rhs.
456 (GenStgCaseDefault bndr occ)
458 data GenStgCaseDefault bndr occ
459 = StgNoDefault -- small con family: all
460 -- constructor accounted for
461 | StgBindDefault (GenStgExpr bndr occ)
464 %************************************************************************
466 \subsection[Stg]{The Plain STG parameterisation}
468 %************************************************************************
470 This happens to be the only one we use at the moment.
473 type StgBinding = GenStgBinding Id Id
474 type StgArg = GenStgArg Id
475 type StgLiveVars = GenStgLiveVars Id
476 type StgExpr = GenStgExpr Id Id
477 type StgRhs = GenStgRhs Id Id
478 type StgCaseAlts = GenStgCaseAlts Id Id
479 type StgCaseDefault = GenStgCaseDefault Id Id
482 %************************************************************************
484 \subsubsection[UpdateFlag-datatype]{@UpdateFlag@}
486 %************************************************************************
488 This is also used in @LambdaFormInfo@ in the @ClosureInfo@ module.
490 A @ReEntrant@ closure may be entered multiple times, but should not be
491 updated or blackholed. An @Updatable@ closure should be updated after
492 evaluation (and may be blackholed during evaluation). A @SingleEntry@
493 closure will only be entered once, and so need not be updated but may
494 safely be blackholed.
497 data UpdateFlag = ReEntrant | Updatable | SingleEntry
499 instance Outputable UpdateFlag where
501 = char (case u of { ReEntrant -> 'r'; Updatable -> 'u'; SingleEntry -> 's' })
503 isUpdatable ReEntrant = False
504 isUpdatable SingleEntry = False
505 isUpdatable Updatable = True
508 %************************************************************************
510 \subsubsection[Static Reference Tables]{@SRT@}
512 %************************************************************************
514 There is one SRT per top-level function group. Each local binding and
515 case expression within this binding group has a subrange of the whole
516 SRT, expressed as an offset and length.
520 | SRT !Int{-offset-} !Int{-length-}
525 pprSRT (NoSRT) = ptext SLIT("_no_srt_")
526 pprSRT (SRT off len) = parens (ppr off <> comma <> ppr len)
529 %************************************************************************
531 \subsection[Stg-pretty-printing]{Pretty-printing}
533 %************************************************************************
535 Robin Popplestone asked for semi-colon separators on STG binds; here's
536 hoping he likes terminators instead... Ditto for case alternatives.
539 pprGenStgBinding :: (Outputable bndr, Outputable bdee, Ord bdee)
540 => GenStgBinding bndr bdee -> SDoc
542 pprGenStgBinding (StgNonRec bndr rhs)
543 = hang (hsep [ppr bndr, equals])
544 4 ((<>) (ppr rhs) semi)
546 pprGenStgBinding (StgRec pairs)
547 = vcat ((ifPprDebug (ptext SLIT("{- StgRec (begin) -}"))) :
548 (map (ppr_bind) pairs) ++ [(ifPprDebug (ptext SLIT("{- StgRec (end) -}")))])
550 ppr_bind (bndr, expr)
551 = hang (hsep [ppr bndr, equals])
552 4 ((<>) (ppr expr) semi)
554 pprStgBinding :: StgBinding -> SDoc
555 pprStgBinding bind = pprGenStgBinding bind
557 pprStgBindings :: [StgBinding] -> SDoc
558 pprStgBindings binds = vcat (map pprGenStgBinding binds)
560 pprGenStgBindingWithSRT
561 :: (Outputable bndr, Outputable bdee, Ord bdee)
562 => (GenStgBinding bndr bdee,[Id]) -> SDoc
564 pprGenStgBindingWithSRT (bind,srt)
565 = vcat [ pprGenStgBinding bind,
566 ptext SLIT("SRT: ") <> ppr srt ]
568 pprStgBindingsWithSRTs :: [(StgBinding,[Id])] -> SDoc
569 pprStgBindingsWithSRTs binds = vcat (map pprGenStgBindingWithSRT binds)
573 instance (Outputable bdee) => Outputable (GenStgArg bdee) where
576 instance (Outputable bndr, Outputable bdee, Ord bdee)
577 => Outputable (GenStgBinding bndr bdee) where
578 ppr = pprGenStgBinding
580 instance (Outputable bndr, Outputable bdee, Ord bdee)
581 => Outputable (GenStgExpr bndr bdee) where
584 instance (Outputable bndr, Outputable bdee, Ord bdee)
585 => Outputable (GenStgRhs bndr bdee) where
586 ppr rhs = pprStgRhs rhs
590 pprStgArg :: (Outputable bdee) => GenStgArg bdee -> SDoc
592 pprStgArg (StgVarArg var) = ppr var
593 pprStgArg (StgLitArg con) = ppr con
594 pprStgArg (StgTypeArg ty) = char '@' <+> ppr ty
598 pprStgExpr :: (Outputable bndr, Outputable bdee, Ord bdee)
599 => GenStgExpr bndr bdee -> SDoc
601 pprStgExpr (StgLit lit) = ppr lit
604 pprStgExpr (StgApp func args)
606 4 (sep (map (ppr) args))
610 pprStgExpr (StgConApp con args)
611 = hsep [ ppr con, brackets (interppSP args)]
613 pprStgExpr (StgPrimApp op args _)
614 = hsep [ ppr op, brackets (interppSP args)]
616 pprStgExpr (StgLam _ bndrs body)
617 =sep [ char '\\' <+> ppr bndrs <+> ptext SLIT("->"),
622 -- special case: let v = <very specific thing>
628 -- Very special! Suspicious! (SLPJ)
630 pprStgExpr (StgLet (StgNonRec bndr (StgRhsClosure cc bi srt free_vars upd_flag args rhs))
633 (hang (hcat [ptext SLIT("let { "), ppr bndr, ptext SLIT(" = "),
636 ptext SLIT(" ["), ifPprDebug (interppSP free_vars), ptext SLIT("] \\"),
637 ppr upd_flag, ptext SLIT(" ["),
638 interppSP args, char ']'])
639 8 (sep [hsep [ppr rhs, ptext SLIT("} in")]]))
642 -- special case: let ... in let ...
644 pprStgExpr (StgLet bind expr@(StgLet _ _))
646 (sep [hang (ptext SLIT("let {")) 2 (hsep [pprGenStgBinding bind, ptext SLIT("} in")])])
650 pprStgExpr (StgLet bind expr)
651 = sep [hang (ptext SLIT("let {")) 2 (pprGenStgBinding bind),
652 hang (ptext SLIT("} in ")) 2 (ppr expr)]
654 pprStgExpr (StgLetNoEscape lvs_whole lvs_rhss bind expr)
655 = sep [hang (ptext SLIT("let-no-escape {"))
656 2 (pprGenStgBinding bind),
657 hang ((<>) (ptext SLIT("} in "))
660 hcat [ptext SLIT("-- lvs: ["), interppSP (uniqSetToList lvs_whole),
661 ptext SLIT("]; rhs lvs: ["), interppSP (uniqSetToList lvs_rhss),
667 pprStgExpr (StgSCC cc expr)
668 = sep [ hsep [ptext SLIT("_scc_"), ppr cc],
673 pprStgExpr (StgCase expr lvs_whole lvs_rhss bndr srt alts)
674 = sep [sep [ptext SLIT("case"),
675 nest 4 (hsep [pprStgExpr expr,
676 ifPprDebug (dcolon <+> pp_ty alts)]),
677 ptext SLIT("of"), ppr bndr, char '{'],
680 hcat [ptext SLIT("-- lvs: ["), interppSP (uniqSetToList lvs_whole),
681 ptext SLIT("]; rhs lvs: ["), interppSP (uniqSetToList lvs_rhss),
684 nest 2 (pprStgAlts alts),
687 pp_ty (StgAlgAlts maybe_tycon _ _) = ppr maybe_tycon
688 pp_ty (StgPrimAlts tycon _ _) = ppr tycon
690 pprStgAlts (StgAlgAlts _ alts deflt)
691 = vcat [ vcat (map (ppr_bxd_alt) alts),
692 pprStgDefault deflt ]
694 ppr_bxd_alt (con, params, use_mask, expr)
695 = hang (hsep [ppr con, interppSP params, ptext SLIT("->")])
696 4 ((<>) (ppr expr) semi)
698 pprStgAlts (StgPrimAlts _ alts deflt)
699 = vcat [ vcat (map (ppr_ubxd_alt) alts),
700 pprStgDefault deflt ]
702 ppr_ubxd_alt (lit, expr)
703 = hang (hsep [ppr lit, ptext SLIT("->")])
704 4 ((<>) (ppr expr) semi)
706 pprStgDefault StgNoDefault = empty
707 pprStgDefault (StgBindDefault expr) = hang (hsep [ptext SLIT("DEFAULT"), ptext SLIT("->")])
713 pprStgLVs :: Outputable occ => GenStgLiveVars occ -> SDoc
715 = getPprStyle $ \ sty ->
716 if userStyle sty || isEmptyUniqSet lvs then
719 hcat [text "{-lvs:", interpp'SP (uniqSetToList lvs), text "-}"]
723 pprStgRhs :: (Outputable bndr, Outputable bdee, Ord bdee)
724 => GenStgRhs bndr bdee -> SDoc
727 pprStgRhs (StgRhsClosure cc bi srt [free_var] upd_flag [{-no args-}] (StgApp func []))
731 brackets (ifPprDebug (ppr free_var)),
732 ptext SLIT(" \\"), ppr upd_flag, ptext SLIT(" [] "), ppr func ]
735 pprStgRhs (StgRhsClosure cc bi srt free_vars upd_flag args body)
736 = hang (hcat [ppr cc,
739 brackets (ifPprDebug (interppSP free_vars)),
740 ptext SLIT(" \\"), ppr upd_flag, brackets (interppSP args)])
743 pprStgRhs (StgRhsCon cc con args)
745 space, ppr con, ptext SLIT("! "), brackets (interppSP args)]
747 pprMaybeSRT (NoSRT) = empty
748 pprMaybeSRT srt = ptext SLIT(" srt: ") <> pprSRT srt
751 Collect @IdInfo@ stuff that is most easily just snaffled straight
752 from the STG bindings.
755 stgArity :: StgRhs -> Int
757 stgArity (StgRhsCon _ _ _) = 0 -- it's a constructor, fully applied
758 stgArity (StgRhsClosure _ _ _ _ _ args _ ) = length args