2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 % $Id: CgClosure.lhs,v 1.58 2002/09/13 15:02:27 simonpj Exp $
6 \section[CgClosure]{Code generation for closures}
8 This module provides the support code for @StgToAbstractC@ to deal
9 with {\em closures} on the RHSs of let(rec)s. See also
10 @CgCon@, which deals with constructors.
13 module CgClosure ( cgTopRhsClosure,
16 closureCodeBody ) where
18 #include "HsVersions.h"
20 import {-# SOURCE #-} CgExpr ( cgExpr )
26 import AbsCUtils ( mkAbstractCs, getAmodeRep )
27 import CgBindery ( getCAddrMode, getArgAmodes,
28 getCAddrModeAndInfo, bindNewToNode,
30 bindNewToReg, bindArgsToRegs,
31 stableAmodeIdInfo, heapIdInfo, CgIdInfo
33 import CgUpdate ( pushUpdateFrame )
34 import CgHeapery ( allocDynClosure,
35 fetchAndReschedule, yield, -- HWL
36 fastEntryChecks, thunkChecks
38 import CgStackery ( mkTaggedVirtStkOffsets, freeStackSlots )
39 import CgUsages ( adjustSpAndHp, setRealAndVirtualSp, getVirtSp,
40 getSpRelOffset, getHpRelOffset
42 import CLabel ( CLabel, mkClosureLabel, mkFastEntryLabel,
43 mkRednCountsLabel, mkInfoTableLabel
45 import ClosureInfo -- lots and lots of stuff
46 import CmdLineOpts ( opt_GranMacros, opt_SccProfilingOn, opt_DoTickyProfiling )
48 import Id ( Id, idName, idType, idPrimRep )
49 import Name ( Name, isInternalName )
50 import Module ( Module, pprModule )
51 import ListSetOps ( minusList )
52 import PrimRep ( PrimRep(..) )
53 import PprType ( showTypeCategory )
54 import Util ( isIn, splitAtList )
55 import CmdLineOpts ( opt_SccProfilingOn )
59 import Name ( nameOccName )
60 import OccName ( occNameFS )
61 import FastTypes ( iBox )
64 %********************************************************
66 \subsection[closures-no-free-vars]{Top-level closures}
68 %********************************************************
70 For closures bound at top level, allocate in static space.
71 They should have no free variables.
75 -> CostCentreStack -- Optional cost centre annotation
81 -> FCode (Id, CgIdInfo)
83 cgTopRhsClosure id ccs binder_info srt args body lf_info
86 getSRTInfo srt `thenFC` \ srt_info ->
89 closure_info = layOutStaticNoFVClosure name lf_info srt_info
90 closure_label = mkClosureLabel name
91 cg_id_info = stableAmodeIdInfo id (CLbl closure_label PtrRep) lf_info
94 -- BUILD THE OBJECT (IF NECESSARY)
96 ({- if staticClosureRequired name binder_info lf_info
98 absC (mkStaticClosure closure_info ccs [] True)
104 -- GENERATE THE INFO TABLE (IF NECESSARY)
105 forkClosureBody (closureCodeBody binder_info closure_info
110 returnFC (id, cg_id_info)
114 %********************************************************
116 \subsection[non-top-level-closures]{Non top-level closures}
118 %********************************************************
120 For closures with free vars, allocate in heap.
125 -> CostCentreStack -- Optional cost centre annotation
131 -> [StgArg] -- payload
132 -> FCode (Id, CgIdInfo)
134 cgStdRhsClosure binder cc binder_info fvs args body lf_info payload
135 -- AHA! A STANDARD-FORM THUNK
137 -- LAY OUT THE OBJECT
138 getArgAmodes payload `thenFC` \ amodes ->
140 (closure_info, amodes_w_offsets)
141 = layOutDynClosure (idName binder) getAmodeRep amodes lf_info NoC_SRT
142 -- No SRT for a standard-form closure
144 (use_cc, blame_cc) = chooseDynCostCentres cc args fvs body
147 allocDynClosure closure_info use_cc blame_cc amodes_w_offsets
149 `thenFC` \ heap_offset ->
152 returnFC (binder, heapIdInfo binder heap_offset lf_info)
155 Here's the general case.
159 -> CostCentreStack -- Optional cost centre annotation
166 -> FCode (Id, CgIdInfo)
168 cgRhsClosure binder cc binder_info srt fvs args body lf_info
170 -- LAY OUT THE OBJECT
172 -- If the binder is itself a free variable, then don't store
173 -- it in the closure. Instead, just bind it to Node on entry.
174 -- NB we can be sure that Node will point to it, because we
175 -- havn't told mkClosureLFInfo about this; so if the binder
176 -- *was* a free var of its RHS, mkClosureLFInfo thinks it *is*
177 -- stored in the closure itself, so it will make sure that
178 -- Node points to it...
180 is_elem = isIn "cgRhsClosure"
182 binder_is_a_fv = binder `is_elem` fvs
183 reduced_fvs = if binder_is_a_fv
184 then fvs `minusList` [binder]
187 mapFCs getCAddrModeAndInfo reduced_fvs `thenFC` \ fvs_w_amodes_and_info ->
188 getSRTInfo srt `thenFC` \ srt_info ->
190 closure_info :: ClosureInfo
191 bind_details :: [((Id, CAddrMode, LambdaFormInfo), VirtualHeapOffset)]
193 (closure_info, bind_details)
194 = layOutDynClosure (idName binder) get_kind
195 fvs_w_amodes_and_info lf_info srt_info
197 bind_fv ((id, _, lf_info), offset) = bindNewToNode id offset lf_info
199 amodes_w_offsets = [(amode,offset) | ((_,amode,_), offset) <- bind_details]
201 get_kind (id, _, _) = idPrimRep id
203 -- BUILD ITS INFO TABLE AND CODE
206 mapCs bind_fv bind_details `thenC`
208 -- Bind the binder itself, if it is a free var
209 (if binder_is_a_fv then
210 bindNewToReg binder node lf_info
215 closureCodeBody binder_info closure_info cc args body
220 (use_cc, blame_cc) = chooseDynCostCentres cc args fvs body
222 allocDynClosure closure_info use_cc blame_cc amodes_w_offsets
223 ) `thenFC` \ heap_offset ->
226 returnFC (binder, heapIdInfo binder heap_offset lf_info)
229 %************************************************************************
231 \subsection[code-for-closures]{The code for closures}
233 %************************************************************************
236 closureCodeBody :: StgBinderInfo
237 -> ClosureInfo -- Lots of information about this closure
238 -> CostCentreStack -- Optional cost centre attached to closure
244 There are two main cases for the code for closures. If there are {\em
245 no arguments}, then the closure is a thunk, and not in normal form.
246 So it should set up an update frame (if it is shared). Also, it has
247 no argument satisfaction check, so fast and slow entry-point labels
251 closureCodeBody binder_info closure_info cc [] body
252 = -- thunks cannot have a primitive type!
253 getAbsC body_code `thenFC` \ body_absC ->
254 moduleName `thenFC` \ mod_name ->
256 absC (CClosureInfoAndCode closure_info body_absC Nothing
259 cl_descr mod_name = closureDescription mod_name (closureName closure_info)
261 body_label = entryLabelFromCI closure_info
263 is_box = case body of { StgApp fun [] -> True; _ -> False }
265 ticky_ent_lit = if (isStaticClosure closure_info)
266 then FSLIT("TICK_ENT_STATIC_THK")
267 else FSLIT("TICK_ENT_DYN_THK")
269 body_code = profCtrC ticky_ent_lit [] `thenC`
270 -- node always points when profiling, so this is ok:
272 thunkWrapper closure_info body_label (
273 -- We only enter cc after setting up update so
274 -- that cc of enclosing scope will be recorded
275 -- in update frame CAF/DICT functions will be
276 -- subsumed by this enclosing cc
277 enterCostCentreCode closure_info cc IsThunk is_box `thenC`
283 If there is {\em at least one argument}, then this closure is in
284 normal form, so there is no need to set up an update frame. On the
285 other hand, we do have to check that there are enough args, and
286 perform an update if not!
288 The Macros for GrAnSim are produced at the beginning of the
289 argSatisfactionCheck (by calling fetchAndReschedule). There info if
290 Node points to closure is available. -- HWL
293 closureCodeBody binder_info closure_info cc all_args body
294 = getEntryConvention name lf_info
295 (map idPrimRep all_args) `thenFC` \ entry_conv ->
297 -- get the current virtual Sp (it might not be zero, eg. if we're
298 -- compiling a let-no-escape).
299 getVirtSp `thenFC` \vSp ->
302 -- Figure out what is needed and what isn't
304 -- SDM: need everything for now in case the heap/stack check refers
306 slow_code_needed = True
307 --slowFunEntryCodeRequired name binder_info entry_conv
308 info_table_needed = True
309 --funInfoTableRequired name binder_info lf_info
311 -- Arg mapping for standard (slow) entry point; all args on stack,
313 (sp_all_args, arg_offsets, _)
314 = mkTaggedVirtStkOffsets vSp idPrimRep all_args
316 -- Arg mapping for the fast entry point; as many args as poss in
317 -- registers; the rest on the stack
318 -- arg_regs are the registers used for arg passing
319 -- stk_args are the args which are passed on the stack
321 -- Args passed on the stack are tagged, but the tags may not
322 -- actually be present (just gaps) if the function is called
323 -- by jumping directly to the fast entry point.
325 arg_regs = case entry_conv of
326 DirectEntry lbl arity regs -> regs
327 other -> [] -- "(HWL ignored; no args passed in regs)"
329 (reg_args, stk_args) = splitAtList arg_regs all_args
331 (sp_stk_args, stk_offsets, stk_tags)
332 = mkTaggedVirtStkOffsets vSp idPrimRep stk_args
334 -- HWL; Note: empty list of live regs in slow entry code
335 -- Old version (reschedule combined with heap check);
336 -- see argSatisfactionCheck for new version
337 --slow_entry_code = forceHeapCheck [node] True slow_entry_code'
338 -- where node = UnusedReg PtrRep 1
339 --slow_entry_code = forceHeapCheck [] True slow_entry_code'
342 = profCtrC slow_ticky_ent_lit [
343 CLbl ticky_ctr_label DataPtrRep
346 -- Bind args, and record expected position of stk ptrs
347 mapCs bindNewToStack arg_offsets `thenC`
348 setRealAndVirtualSp sp_all_args `thenC`
350 argSatisfactionCheck closure_info arg_regs `thenC`
352 -- OK, so there are enough args. Now we need to stuff as
353 -- many of them in registers as the fast-entry code
354 -- expects. Note that the zipWith will give up when it hits
355 -- the end of arg_regs.
357 mapFCs getCAddrMode all_args `thenFC` \ stk_amodes ->
358 absC (mkAbstractCs (zipWith assign_to_reg arg_regs stk_amodes))
361 -- Now adjust real stack pointers (no need to adjust Hp,
362 -- but call this function for convenience).
363 adjustSpAndHp sp_stk_args `thenC`
365 absC (CFallThrough (CLbl fast_label CodePtrRep))
367 assign_to_reg reg_id amode = CAssign (CReg reg_id) amode
370 -- Old version (reschedule combined with heap check);
371 -- see argSatisfactionCheck for new version
372 -- fast_entry_code = forceHeapCheck [] True fast_entry_code'
375 mod_name <- moduleName
376 profCtrC FSLIT("TICK_CTR") [
377 CLbl ticky_ctr_label DataPtrRep,
378 mkCString (mkFastString (ppr_for_ticky_name mod_name name)),
379 mkIntCLit stg_arity, -- total # of args
380 mkIntCLit sp_stk_args, -- # passed on stk
381 mkCString (mkFastString (map (showTypeCategory . idType) all_args))
384 profCtrC fast_ticky_ent_lit [
385 CLbl ticky_ctr_label DataPtrRep
388 -- Nuked for now; see comment at end of file
389 -- CString (mkFastString (show_wrapper_name wrapper_maybe)),
390 -- CString (mkFastString (show_wrapper_arg_kinds wrapper_maybe))
393 -- Bind args to regs/stack as appropriate, and
394 -- record expected position of sps.
395 bindArgsToRegs reg_args arg_regs
396 mapCs bindNewToStack stk_offsets
397 setRealAndVirtualSp sp_stk_args
399 -- free up the stack slots containing tags
400 freeStackSlots (map fst stk_tags)
402 -- Enter the closures cc, if required
403 enterCostCentreCode closure_info cc IsFunction False
406 funWrapper closure_info arg_regs stk_tags info_label
407 (prof >> cgExpr body)
410 setTickyCtrLabel ticky_ctr_label (
412 -- Make a labelled code-block for the slow and fast entry code
413 forkAbsC (if slow_code_needed then slow_entry_code else absC AbsCNop)
414 `thenFC` \ slow_abs_c ->
415 forkAbsC fast_entry_code `thenFC` \ fast_abs_c ->
416 moduleName `thenFC` \ mod_name ->
418 -- Now either construct the info table, or put the fast code in alone
419 -- (We never have slow code without an info table)
420 -- XXX probably need the info table and slow entry code in case of
421 -- a heap check failure.
423 if info_table_needed then
424 CClosureInfoAndCode closure_info slow_abs_c (Just fast_abs_c)
427 CCodeBlock fast_label fast_abs_c
431 ticky_ctr_label = mkRednCountsLabel name
433 (slow_ticky_ent_lit, fast_ticky_ent_lit) =
434 if (isStaticClosure closure_info)
435 then (FSLIT("TICK_ENT_STATIC_FUN_STD"), FSLIT("TICK_ENT_STATIC_FUN_DIRECT"))
436 else (FSLIT("TICK_ENT_DYN_FUN_STD"), FSLIT("TICK_ENT_DYN_FUN_DIRECT"))
438 stg_arity = length all_args
439 lf_info = closureLFInfo closure_info
441 cl_descr mod_name = closureDescription mod_name name
443 -- Manufacture labels
444 name = closureName closure_info
445 fast_label = mkFastEntryLabel name stg_arity
446 info_label = mkInfoTableLabel name
449 -- When printing the name of a thing in a ticky file, we want to
450 -- give the module name even for *local* things. We print
451 -- just "x (M)" rather that "M.x" to distinguish them from the global kind.
452 ppr_for_ticky_name mod_name name
453 | isInternalName name = showSDocDebug (ppr name <+> (parens (ppr mod_name)))
454 | otherwise = showSDocDebug (ppr name)
457 For lexically scoped profiling we have to load the cost centre from
458 the closure entered, if the costs are not supposed to be inherited.
459 This is done immediately on entering the fast entry point.
461 Load current cost centre from closure, if not inherited.
462 Node is guaranteed to point to it, if profiling and not inherited.
465 data IsThunk = IsThunk | IsFunction -- Bool-like, local
471 :: ClosureInfo -> CostCentreStack
473 -> Bool -- is_box: this closure is a special box introduced by SCCfinal
476 enterCostCentreCode closure_info ccs is_thunk is_box
477 = if not opt_SccProfilingOn then
480 ASSERT2(not (noCCSAttached ccs), ppr (closureName closure_info) <+> ppr ccs)
482 if isSubsumedCCS ccs then
483 ASSERT(isToplevClosure closure_info)
484 ASSERT(is_thunk == IsFunction)
485 costCentresC FSLIT("ENTER_CCS_FSUB") []
487 else if isDerivedFromCurrentCCS ccs then
488 if re_entrant && not is_box
489 then costCentresC FSLIT("ENTER_CCS_FCL") [CReg node]
490 else costCentresC FSLIT("ENTER_CCS_TCL") [CReg node]
492 else if isCafCCS ccs then
493 ASSERT(isToplevClosure closure_info)
494 ASSERT(is_thunk == IsThunk)
495 -- might be a PAP, in which case we want to subsume costs
497 then costCentresC FSLIT("ENTER_CCS_FSUB") []
498 else costCentresC FSLIT("ENTER_CCS_CAF") c_ccs
500 else panic "enterCostCentreCode"
503 c_ccs = [mkCCostCentreStack ccs]
504 re_entrant = closureReEntrant closure_info
507 %************************************************************************
509 \subsubsection[pre-closure-code-stuff]{Pre-closure-code code}
511 %************************************************************************
513 The argument-satisfaction check code is placed after binding
514 the arguments to their stack locations. Hence, the virtual stack
515 pointer is pointing after all the args, and virtual offset 1 means
516 the base of frame and hence most distant arg. Hence
517 virtual offset 0 is just beyond the most distant argument; the
518 relative offset of this word tells how many words of arguments
522 argSatisfactionCheck :: ClosureInfo -> [MagicId] {-GRAN-} -> Code
524 argSatisfactionCheck closure_info arg_regs
526 = nodeMustPointToIt (closureLFInfo closure_info) `thenFC` \ node_points ->
529 -- emit_gran_macros = opt_GranMacros
533 -- absC (CMacroStmt GRAN_FETCH []) `thenC`
534 -- forceHeapCheck [] node_points (absC AbsCNop) `thenC`
536 -- then if node_points
537 -- then fetchAndReschedule arg_regs node_points
538 -- else yield arg_regs node_points
539 -- else absC AbsCNop) `thenC`
541 getSpRelOffset 0 `thenFC` \ (SpRel sp) ->
544 rel_arg = mkIntCLit off
548 absC (CMacroStmt ARGS_CHK [rel_arg]) -- node already points
550 absC (CMacroStmt ARGS_CHK_LOAD_NODE [rel_arg, set_Node_to_this])
552 -- We must tell the arg-satis macro whether Node is pointing to
553 -- the closure or not. If it isn't so pointing, then we give to
554 -- the macro the (static) address of the closure.
556 set_Node_to_this = CLbl (closureLabelFromCI closure_info) PtrRep
559 %************************************************************************
561 \subsubsection[closure-code-wrappers]{Wrappers around closure code}
563 %************************************************************************
566 thunkWrapper:: ClosureInfo -> CLabel -> Code -> Code
567 thunkWrapper closure_info lbl thunk_code
568 = -- Stack and heap overflow checks
569 nodeMustPointToIt (closureLFInfo closure_info) `thenFC` \ node_points ->
571 -- HWL: insert macros for GrAnSim; 2 versions depending on liveness of node
572 -- (we prefer fetchAndReschedule-style context switches to yield ones)
575 then fetchAndReschedule [] node_points
576 else yield [] node_points
577 else absC AbsCNop) `thenC`
579 -- stack and/or heap checks
580 thunkChecks lbl node_points (
582 -- Overwrite with black hole if necessary
583 blackHoleIt closure_info node_points `thenC`
585 setupUpdate closure_info ( -- setupUpdate *encloses* the rest
587 -- Finally, do the business
591 funWrapper :: ClosureInfo -- Closure whose code body this is
592 -> [MagicId] -- List of argument registers (if any)
593 -> [(VirtualSpOffset,Int)] -- tagged stack slots
594 -> CLabel -- info table for heap check ret.
595 -> Code -- Body of function being compiled
597 funWrapper closure_info arg_regs stk_tags info_label fun_body
598 = -- Stack overflow check
599 nodeMustPointToIt (closureLFInfo closure_info) `thenFC` \ node_points ->
601 -- enter for Ldv profiling
602 (if node_points then ldvEnter else nopC) `thenC`
605 then yield arg_regs node_points
606 else absC AbsCNop) `thenC`
608 -- heap and/or stack checks
609 fastEntryChecks arg_regs stk_tags info_label node_points (
611 -- Finally, do the business
617 %************************************************************************
619 \subsubsubsection[update-and-BHs]{Update and black-hole wrappers}
621 %************************************************************************
625 blackHoleIt :: ClosureInfo -> Bool -> Code -- Only called for closures with no args
627 blackHoleIt closure_info node_points
628 = if blackHoleOnEntry closure_info && node_points
631 info_label = infoTableLabelFromCI closure_info
632 args = [ CLbl info_label DataPtrRep ]
634 absC (if closureSingleEntry(closure_info) then
635 CMacroStmt UPD_BH_SINGLE_ENTRY args
637 CMacroStmt UPD_BH_UPDATABLE args)
643 setupUpdate :: ClosureInfo -> Code -> Code -- Only called for closures with no args
644 -- Nota Bene: this function does not change Node (even if it's a CAF),
645 -- so that the cost centre in the original closure can still be
646 -- extracted by a subsequent ENTER_CC_TCL
648 -- I've tidied up the code for this function, but it should still do the same as
649 -- it did before (modulo ticky stuff). KSW 1999-04.
650 setupUpdate closure_info code
651 = if closureReEntrant closure_info
655 case (closureUpdReqd closure_info, isStaticClosure closure_info) of
656 (False,False) -> profCtrC FSLIT("TICK_UPDF_OMITTED") [] `thenC`
658 (False,True ) -> (if opt_DoTickyProfiling
660 -- blackhole the SE CAF
661 link_caf seCafBlackHoleClosureInfo `thenFC` \ _ -> nopC
664 profCtrC FSLIT("TICK_UPD_CAF_BH_SINGLE_ENTRY") [mkCString cl_name] `thenC`
665 profCtrC FSLIT("TICK_UPDF_OMITTED") [] `thenC`
667 (True ,False) -> pushUpdateFrame (CReg node) code
668 (True ,True ) -> -- blackhole the (updatable) CAF:
669 link_caf cafBlackHoleClosureInfo `thenFC` \ update_closure ->
670 profCtrC FSLIT("TICK_UPD_CAF_BH_UPDATABLE") [mkCString cl_name] `thenC`
671 pushUpdateFrame update_closure code
673 cl_name :: FastString
674 cl_name = (occNameFS . nameOccName . closureName) closure_info
676 link_caf :: (ClosureInfo -> ClosureInfo) -- function yielding BH closure_info
677 -> FCode CAddrMode -- Returns amode for closure to be updated
679 = -- To update a CAF we must allocate a black hole, link the CAF onto the
680 -- CAF list, then update the CAF to point to the fresh black hole.
681 -- This function returns the address of the black hole, so it can be
682 -- updated with the new value when available.
684 -- Alloc black hole specifying CC_HDR(Node) as the cost centre
686 use_cc = CMacroExpr PtrRep CCS_HDR [nodeReg]
689 allocDynClosure (bhCI closure_info) use_cc blame_cc [] `thenFC` \ heap_offset ->
690 getHpRelOffset heap_offset `thenFC` \ hp_rel ->
691 let amode = CAddr hp_rel
693 absC (CMacroStmt UPD_CAF [CReg node, amode]) `thenC`
697 %************************************************************************
699 \subsection[CgClosure-Description]{Profiling Closure Description.}
701 %************************************************************************
703 For "global" data constructors the description is simply occurrence
704 name of the data constructor itself (see \ref{CgConTbls-info-tables}).
706 Otherwise it is determind by @closureDescription@ from the let
710 closureDescription :: Module -- Module
711 -> Name -- Id of closure binding
714 -- Not called for StgRhsCon which have global info tables built in
715 -- CgConTbls.lhs with a description generated from the data constructor
717 closureDescription mod_name name
727 chooseDynCostCentres ccs args fvs body
729 use_cc -- cost-centre we record in the object
730 = if currentOrSubsumedCCS ccs
731 then CReg CurCostCentre
732 else mkCCostCentreStack ccs
734 blame_cc -- cost-centre on whom we blame the allocation
735 = case (args, fvs, body) of
736 ([], _, StgApp fun [{-no args-}])
737 -> mkCCostCentreStack overheadCCS
740 -- if it's an utterly trivial RHS, then it must be
741 -- one introduced by boxHigherOrderArgs for profiling,
742 -- so we charge it to "OVERHEAD".
744 -- This looks like a HACK to me --SDM