2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1994
4 %********************************************************
6 \section[CgLetNoEscape]{Handling ``let-no-escapes''}
8 %********************************************************
11 #include "HsVersions.h"
13 module CgLetNoEscape ( cgLetNoEscapeClosure ) where
19 import CgBindery -- various things
20 import CgExpr ( cgExpr )
21 import CgHeapery ( heapCheck )
22 import CgRetConv ( assignRegs )
23 import CgStackery ( mkVirtStkOffsets )
24 import CgUsages ( setRealAndVirtualSps, getVirtSps )
25 import CLabelInfo ( mkStdEntryLabel )
26 import ClosureInfo ( mkLFLetNoEscape )
27 import Id ( getIdKind )
31 %************************************************************************
33 \subsection[what-is-non-escaping]{What {\em is} a ``non-escaping let''?}
35 %************************************************************************
37 [The {\em code} that detects these things is elsewhere.]
41 let x = fvs \ args -> e
44 if ... then x else ...
46 @x@ is used twice (so we probably can't unfold it), but when it is
47 entered, the stack is deeper than it was then the definition of @x@
48 happened. Specifically, if instead of allocating a closure for @x@,
49 we saved all @x@'s fvs on the stack, and remembered the stack depth at
50 that moment, then whenever we enter @x@ we can simply set the stack
51 pointer(s) to these remembered (compile-time-fixed) values, and jump
54 All of this is provided x is:
59 guaranteed to be entered before the stack retreats -- ie x is not
60 buried in a heap-allocated closure, or passed as an argument to something;
62 all the enters have exactly the right number of arguments,
65 all the enters are tail calls; that is, they return to the
66 caller enclosing the definition of @x@.
69 Under these circumstances we say that @x@ is {\em non-escaping}.
71 An example of when (4) does {\em not} hold:
74 in case x of ...alts...
77 Here, @x@ is certainly entered only when the stack is deeper than when
78 @x@ is defined, but here it must return to \tr{...alts...} So we can't
79 just adjust the stack down to @x@'s recalled points, because that
80 would lost @alts@' context.
82 Things can get a little more complicated. Consider:
85 in let x = fvs \ args -> ...y...
89 Now, if @x@ is used in a non-escaping way in \tr{...x...}, {\em and}
90 @y@ is used in a non-escaping way in \tr{...y...}, {\em then} @y@ is
93 @x@ can even be recursive! Eg:
95 letrec x = [y] \ [v] -> if v then x True else ...
101 %************************************************************************
103 \subsection[codeGen-for-non-escaping]{Generating code for a ``non-escaping let''}
105 %************************************************************************
108 Generating code for this is fun. It is all very very similar to what
109 we do for a case expression. The duality is between
119 That is, the RHS of @x@ (ie @b@) will execute {\em later}, just like
120 the alternative of the case; it needs to be compiled in an environment
121 in which all volatile bindings are forgotten, and the free vars are
122 bound only to stable things like stack locations.. The @e@ part will
123 execute {\em next}, just like the scrutinee of a case.
125 First, we need to save all @x@'s free vars
126 on the stack, if they aren't there already.
131 -> CostCentre -- NB: *** NOT USED *** ToDo (WDP 94/06)
132 -> StgBinderInfo -- NB: ditto
133 -> PlainStgLiveVars -- variables live in RHS, including the binders
134 -- themselves in the case of a recursive group
135 -> EndOfBlockInfo -- where are we going to?
136 -> Maybe VirtualSpBOffset -- Slot for current cost centre
137 -> [Id] -- args (as in \ args -> body)
138 -> PlainStgExpr -- body (as in above)
139 -> FCode (Id, CgIdInfo)
141 -- ToDo: deal with the cost-centre issues
143 cgLetNoEscapeClosure binder cc bi full_live_in_rhss rhs_eob_info maybe_cc_slot args body
146 lf_info = mkLFLetNoEscape arity full_live_in_rhss{-used???-}
150 (nukeDeadBindings full_live_in_rhss)
151 (forkAbsC (cgLetNoEscapeBody args body))
152 `thenFC` \ (vA, vB, code) ->
154 label = mkStdEntryLabel binder -- arity
156 absC (CCodeBlock label code) `thenC`
157 returnFC (binder, letNoEscapeIdInfo binder vA vB lf_info)
161 cgLetNoEscapeBody :: [Id] -- Args
162 -> PlainStgExpr -- Body
165 cgLetNoEscapeBody all_args rhs
166 = getVirtSps `thenFC` \ (vA, vB) ->
167 getIntSwitchChkrC `thenFC` \ isw_chkr ->
169 arg_kinds = map getIdKind all_args
170 (arg_regs, _) = assignRegs isw_chkr [{-nothing live-}] arg_kinds
171 stk_args = drop (length arg_regs) all_args
173 -- stk_args is the args which are passed on the stack at the fast-entry point
174 -- Using them, we define the stack layout
175 (spA_stk_args, spB_stk_args, stk_bxd_w_offsets, stk_ubxd_w_offsets)
177 vA vB -- Initial virtual SpA, SpB
182 -- Bind args to appropriate regs/stk locns
183 bindArgsToRegs all_args arg_regs `thenC`
184 mapCs bindNewToAStack stk_bxd_w_offsets `thenC`
185 mapCs bindNewToBStack stk_ubxd_w_offsets `thenC`
186 setRealAndVirtualSps spA_stk_args spB_stk_args `thenC`
188 {- ToDo: NOT SURE ABOUT COST CENTRES!
189 -- Enter the closures cc, if required
190 lexEnterCCcode closure_info maybe_cc `thenC`
193 -- [No need for stack check; forkEvalHelp dealt with that]
195 -- Do heap check [ToDo: omit for non-recursive case by recording in
196 -- in envt and absorbing at call site]
197 heapCheck arg_regs False {- Node doesn't point to it -} (
198 -- heapCheck *encloses* the rest