2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1995
4 \section[CgClosure]{Code generation for closures}
6 This module provides the support code for @StgToAbstractC@ to deal
7 with {\em closures} on the RHSs of let(rec)s. See also
8 @CgCon@, which deals with constructors.
11 #include "HsVersions.h"
13 module CgClosure ( cgTopRhsClosure, cgRhsClosure ) where
16 IMPORT_DELOOPER(CgLoop2) ( cgExpr )
22 import AbsCUtils ( mkAbstractCs, getAmodeRep )
23 import CgBindery ( getCAddrMode, getArgAmodes,
24 getCAddrModeAndInfo, bindNewToNode,
25 bindNewToAStack, bindNewToBStack,
26 bindNewToReg, bindArgsToRegs,
27 stableAmodeIdInfo, heapIdInfo
29 import CgCompInfo ( spARelToInt, spBRelToInt )
30 import CgUpdate ( pushUpdateFrame )
31 import CgHeapery ( allocDynClosure, heapCheck
32 , heapCheckOnly, fetchAndReschedule, yield -- HWL
34 import CgRetConv ( mkLiveRegsMask,
35 ctrlReturnConvAlg, dataReturnConvAlg,
36 CtrlReturnConvention(..), DataReturnConvention(..)
38 import CgStackery ( getFinalStackHW, mkVirtStkOffsets,
41 import CgUsages ( getVirtSps, setRealAndVirtualSps,
42 getSpARelOffset, getSpBRelOffset,
45 import CLabel ( mkClosureLabel, mkConUpdCodePtrVecLabel,
46 mkStdUpdCodePtrVecLabel, mkStdUpdVecTblLabel,
47 mkErrorStdEntryLabel, mkRednCountsLabel
49 import ClosureInfo -- lots and lots of stuff
50 import CmdLineOpts ( opt_ForConcurrent, opt_GranMacros )
51 import CostCentre ( useCurrentCostCentre, currentOrSubsumedCosts,
52 noCostCentreAttached, costsAreSubsumed,
53 isCafCC, isDictCC, overheadCostCentre
55 import HeapOffs ( SYN_IE(VirtualHeapOffset) )
56 import Id ( idType, idPrimRep,
57 showId, getIdStrictness, dataConTag,
59 GenId{-instance Outputable-}
61 import ListSetOps ( minusList )
62 import Maybes ( maybeToBool )
63 import PprStyle ( PprStyle(..) )
64 import PprType ( GenType{-instance Outputable-}, TyCon{-ditto-} )
65 import Pretty ( prettyToUn, ppBesides, ppChar, ppPStr )
66 import PrimRep ( isFollowableRep, PrimRep(..) )
67 import TyCon ( isPrimTyCon, tyConDataCons )
68 import Unpretty ( uppShow )
69 import Util ( isIn, panic, pprPanic, assertPanic )
71 myWrapperMaybe = panic "CgClosure.myWrapperMaybe (ToDo)"
72 showTypeCategory = panic "CgClosure.showTypeCategory (ToDo)"
73 getWrapperArgTypeCategories = panic "CgClosure.getWrapperArgTypeCategories (ToDo)"
76 %********************************************************
78 \subsection[closures-no-free-vars]{Top-level closures}
80 %********************************************************
82 For closures bound at top level, allocate in static space.
83 They should have no free variables.
87 -> CostCentre -- Optional cost centre annotation
92 -> FCode (Id, CgIdInfo)
94 cgTopRhsClosure name cc binder_info args body lf_info
95 = -- LAY OUT THE OBJECT
97 closure_info = layOutStaticNoFVClosure name lf_info
100 -- GENERATE THE INFO TABLE (IF NECESSARY)
101 forkClosureBody (closureCodeBody binder_info closure_info
105 -- BUILD VAP INFO TABLES IF NECESSARY
106 -- Don't build Vap info tables etc for
107 -- a function whose result is an unboxed type,
108 -- because we can never have thunks with such a type.
109 (if closureReturnsUnboxedType closure_info then
113 bind_the_fun = addBindC name cg_id_info -- It's global!
115 cgVapInfoTables True {- Top level -} bind_the_fun binder_info name args lf_info
118 -- BUILD THE OBJECT (IF NECESSARY)
119 (if staticClosureRequired name binder_info lf_info
122 cost_centre = mkCCostCentre cc
125 closure_label -- Labelled with the name on lhs of defn
133 returnFC (name, cg_id_info)
135 closure_label = mkClosureLabel name
136 cg_id_info = stableAmodeIdInfo name (CLbl closure_label PtrRep) lf_info
139 %********************************************************
141 \subsection[non-top-level-closures]{Non top-level closures}
143 %********************************************************
145 For closures with free vars, allocate in heap.
147 ===================== OLD PROBABLY OUT OF DATE COMMENTS =============
149 -- Closures which (a) have no fvs and (b) have some args (i.e.
150 -- combinator functions), are allocated statically, just as if they
151 -- were top-level closures. We can't get a space leak that way
152 -- (because they are HNFs) and it saves allocation.
154 -- Lexical Scoping: Problem
155 -- These top level function closures will be inherited, possibly
156 -- to a different cost centre scope set before entering.
158 -- Evaluation Scoping: ok as already in HNF
160 -- Should rely on floating mechanism to achieve this floating to top level.
161 -- As let floating will avoid floating which breaks cost centre attribution
162 -- everything will be OK.
164 -- Disabled: because it breaks lexical-scoped cost centre semantics.
165 -- cgRhsClosure binder cc bi [] upd_flag args@(_:_) body
166 -- = cgTopRhsClosure binder cc bi upd_flag args body
168 ===================== END OF OLD PROBABLY OUT OF DATE COMMENTS =============
172 -> CostCentre -- Optional cost centre annotation
178 -> FCode (Id, CgIdInfo)
180 cgRhsClosure binder cc binder_info fvs args body lf_info
181 | maybeToBool maybe_std_thunk -- AHA! A STANDARD-FORM THUNK
182 -- ToDo: check non-primitiveness (ASSERT)
184 -- LAY OUT THE OBJECT
185 getArgAmodes std_thunk_payload `thenFC` \ amodes ->
187 (closure_info, amodes_w_offsets)
188 = layOutDynClosure binder getAmodeRep amodes lf_info
190 (use_cc, blame_cc) = chooseDynCostCentres cc args fvs body
193 allocDynClosure closure_info use_cc blame_cc amodes_w_offsets
195 `thenFC` \ heap_offset ->
198 returnFC (binder, heapIdInfo binder heap_offset lf_info)
201 maybe_std_thunk = getStandardFormThunkInfo lf_info
202 Just std_thunk_payload = maybe_std_thunk
205 Here's the general case.
207 cgRhsClosure binder cc binder_info fvs args body lf_info
209 -- LAY OUT THE OBJECT
211 -- If the binder is itself a free variable, then don't store
212 -- it in the closure. Instead, just bind it to Node on entry.
213 -- NB we can be sure that Node will point to it, because we
214 -- havn't told mkClosureLFInfo about this; so if the binder
215 -- *was* a free var of its RHS, mkClosureLFInfo thinks it *is*
216 -- stored in the closure itself, so it will make sure that
217 -- Node points to it...
219 is_elem = isIn "cgRhsClosure"
221 binder_is_a_fv = binder `is_elem` fvs
222 reduced_fvs = if binder_is_a_fv
223 then fvs `minusList` [binder]
226 mapFCs getCAddrModeAndInfo reduced_fvs `thenFC` \ amodes_and_info ->
228 fvs_w_amodes_and_info = reduced_fvs `zip` amodes_and_info
230 closure_info :: ClosureInfo
231 bind_details :: [((Id, (CAddrMode, LambdaFormInfo)), VirtualHeapOffset)]
233 (closure_info, bind_details)
234 = layOutDynClosure binder get_kind fvs_w_amodes_and_info lf_info
236 bind_fv ((id, (_, lf_info)), offset) = bindNewToNode id offset lf_info
238 amodes_w_offsets = [(amode,offset) | ((_, (amode,_)), offset) <- bind_details]
240 get_kind (id, amode_and_info) = idPrimRep id
242 -- BUILD ITS INFO TABLE AND CODE
245 mapCs bind_fv bind_details `thenC`
247 -- Bind the binder itself, if it is a free var
248 (if binder_is_a_fv then
249 bindNewToReg binder node lf_info
254 closureCodeBody binder_info closure_info cc args body
257 -- BUILD VAP INFO TABLES IF NECESSARY
258 -- Don't build Vap info tables etc for
259 -- a function whose result is an unboxed type,
260 -- because we can never have thunks with such a type.
261 (if closureReturnsUnboxedType closure_info then
264 cgVapInfoTables False {- Not top level -} nopC binder_info binder args lf_info
269 (use_cc, blame_cc) = chooseDynCostCentres cc args fvs body
271 allocDynClosure closure_info use_cc blame_cc amodes_w_offsets
272 ) `thenFC` \ heap_offset ->
275 returnFC (binder, heapIdInfo binder heap_offset lf_info)
278 @cgVapInfoTables@ generates both Vap info tables, if they are required
279 at all. It calls @cgVapInfoTable@ to generate each Vap info table,
280 along with its entry code.
283 -- Don't generate Vap info tables for thunks; only for functions
284 cgVapInfoTables top_level perhaps_bind_the_fun binder_info fun [{- no args; a thunk! -}] lf_info
287 cgVapInfoTables top_level perhaps_bind_the_fun binder_info fun args lf_info
288 = -- BUILD THE STANDARD VAP-ENTRY STUFF IF NECESSARY
289 (if stdVapRequired binder_info then
290 cgVapInfoTable perhaps_bind_the_fun Updatable fun args fun_in_payload lf_info
295 -- BUILD THE NO-UPDATE VAP-ENTRY STUFF IF NECESSARY
296 (if noUpdVapRequired binder_info then
297 cgVapInfoTable perhaps_bind_the_fun SingleEntry fun args fun_in_payload lf_info
303 fun_in_payload = not top_level
305 cgVapInfoTable perhaps_bind_the_fun upd_flag fun args fun_in_payload fun_lf_info
307 -- The vap_entry_rhs is a manufactured STG expression which
308 -- looks like the RHS of any binding which is going to use the vap-entry
309 -- point of the function. Each of these bindings will look like:
311 -- x = [a,b,c] \upd [] -> f a b c
313 -- If f is not top-level, then f is one of the free variables too,
314 -- hence "payload_ids" isn't the same as "arg_ids".
316 vap_entry_rhs = StgApp (StgVarArg fun) (map StgVarArg args) emptyIdSet
319 arg_ids_w_info = [(name,mkLFArgument) | name <- args]
320 payload_ids_w_info | fun_in_payload = (fun,fun_lf_info) : arg_ids_w_info
321 | otherwise = arg_ids_w_info
323 payload_ids | fun_in_payload = fun : args -- Sigh; needed for mkClosureLFInfo
326 vap_lf_info = mkClosureLFInfo False {-not top level-} payload_ids
327 upd_flag [] vap_entry_rhs
328 -- It's not top level, even if we're currently compiling a top-level
329 -- function, because any VAP *use* of this function will be for a
331 -- let x = f p q -- x isn't top level!
334 get_kind (id, info) = idPrimRep id
336 payload_bind_details :: [((Id, LambdaFormInfo), VirtualHeapOffset)]
337 (closure_info, payload_bind_details) = layOutDynClosure
339 get_kind payload_ids_w_info
341 -- The dodgy thing is that we use the "fun" as the
342 -- Id to give to layOutDynClosure. This Id gets embedded in
343 -- the closure_info it returns. But of course, the function doesn't
344 -- have the right type to match the Vap closure. Never mind,
345 -- a hack in closureType spots the special case. Otherwise that
346 -- Id is just used for label construction, which is OK.
348 bind_fv ((id,lf_info), offset) = bindNewToNode id offset lf_info
351 -- BUILD ITS INFO TABLE AND CODE
354 -- Bind the fvs; if the fun is not in the payload, then bind_the_fun tells
355 -- how to bind it. If it is in payload it'll be bound by payload_bind_details.
356 perhaps_bind_the_fun `thenC`
357 mapCs bind_fv payload_bind_details `thenC`
359 -- Generate the info table and code
360 closureCodeBody NoStgBinderInfo
363 [] -- No args; it's a thunk
367 %************************************************************************
369 \subsection[code-for-closures]{The code for closures}
371 %************************************************************************
374 closureCodeBody :: StgBinderInfo
375 -> ClosureInfo -- Lots of information about this closure
376 -> CostCentre -- Optional cost centre attached to closure
382 There are two main cases for the code for closures. If there are {\em
383 no arguments}, then the closure is a thunk, and not in normal form.
384 So it should set up an update frame (if it is shared). Also, it has
385 no argument satisfaction check, so fast and slow entry-point labels
389 closureCodeBody binder_info closure_info cc [] body
390 = -- thunks cannot have a primitive type!
394 = case (closureType closure_info) of
395 Nothing -> (False, panic "debug")
396 Just (tc,_,_) -> (True, tc)
398 if has_tycon && isPrimTyCon tycon then
399 pprPanic "closureCodeBody:thunk:prim type!" (ppr PprDebug tycon)
402 getAbsC body_code `thenFC` \ body_absC ->
403 moduleName `thenFC` \ mod_name ->
405 absC (CClosureInfoAndCode closure_info body_absC Nothing
406 stdUpd (cl_descr mod_name)
407 (dataConLiveness closure_info))
409 cl_descr mod_name = closureDescription mod_name (closureId closure_info) [] body
411 body_addr = CLbl (entryLabelFromCI closure_info) CodePtrRep
412 body_code = profCtrC SLIT("ENT_THK") [] `thenC`
413 enterCostCentreCode closure_info cc IsThunk `thenC`
414 thunkWrapper closure_info (cgExpr body)
416 stdUpd = CLbl mkErrorStdEntryLabel CodePtrRep
419 If there is {\em at least one argument}, then this closure is in
420 normal form, so there is no need to set up an update frame. On the
421 other hand, we do have to check that there are enough args, and
422 perform an update if not!
424 The Macros for GrAnSim are produced at the beginning of the
425 argSatisfactionCheck (by calling fetchAndReschedule). There info if
426 Node points to closure is available. -- HWL
429 closureCodeBody binder_info closure_info cc all_args body
430 = getEntryConvention id lf_info
431 (map idPrimRep all_args) `thenFC` \ entry_conv ->
433 is_concurrent = opt_ForConcurrent
435 stg_arity = length all_args
437 -- Arg mapping for standard (slow) entry point; all args on stack
438 (spA_all_args, spB_all_args, all_bxd_w_offsets, all_ubxd_w_offsets)
440 0 0 -- Initial virtual SpA, SpB
444 -- Arg mapping for the fast entry point; as many args as poss in
445 -- registers; the rest on the stack
446 -- arg_regs are the registers used for arg passing
447 -- stk_args are the args which are passed on the stack
449 arg_regs = case entry_conv of
450 DirectEntry lbl arity regs -> regs
451 ViaNode | is_concurrent -> []
452 other -> panic "closureCodeBody:arg_regs"
454 num_arg_regs = length arg_regs
456 (reg_args, stk_args) = splitAt num_arg_regs all_args
458 (spA_stk_args, spB_stk_args, stk_bxd_w_offsets, stk_ubxd_w_offsets)
460 0 0 -- Initial virtual SpA, SpB
464 -- HWL; Note: empty list of live regs in slow entry code
465 -- Old version (reschedule combined with heap check);
466 -- see argSatisfactionCheck for new version
467 --slow_entry_code = forceHeapCheck [node] True slow_entry_code'
468 -- where node = VanillaReg PtrRep 1
469 --slow_entry_code = forceHeapCheck [] True slow_entry_code'
472 = profCtrC SLIT("ENT_FUN_STD") [] `thenC`
474 -- Bind args, and record expected position of stk ptrs
475 mapCs bindNewToAStack all_bxd_w_offsets `thenC`
476 mapCs bindNewToBStack all_ubxd_w_offsets `thenC`
477 setRealAndVirtualSps spA_all_args spB_all_args `thenC`
479 argSatisfactionCheck closure_info all_args `thenC`
481 -- OK, so there are enough args. Now we need to stuff as
482 -- many of them in registers as the fast-entry code
483 -- expects Note that the zipWith will give up when it hits
484 -- the end of arg_regs.
486 mapFCs getCAddrMode all_args `thenFC` \ stk_amodes ->
487 absC (mkAbstractCs (zipWith assign_to_reg arg_regs stk_amodes)) `thenC`
489 -- Now adjust real stack pointers
490 adjustRealSps spA_stk_args spB_stk_args `thenC`
492 absC (CFallThrough (CLbl fast_label CodePtrRep))
494 assign_to_reg reg_id amode = CAssign (CReg reg_id) amode
497 -- Old version (reschedule combined with heap check);
498 -- see argSatisfactionCheck for new version
499 -- fast_entry_code = forceHeapCheck [] True fast_entry_code'
502 = profCtrC SLIT("ENT_FUN_DIRECT") [
503 CLbl (mkRednCountsLabel id) PtrRep,
504 CString (_PK_ (showId PprDebug id)),
505 mkIntCLit stg_arity, -- total # of args
506 mkIntCLit spA_stk_args, -- # passed on A stk
507 mkIntCLit spB_stk_args, -- B stk (rest in regs)
508 CString (_PK_ (map (showTypeCategory . idType) all_args)),
509 CString (_PK_ (show_wrapper_name wrapper_maybe)),
510 CString (_PK_ (show_wrapper_arg_kinds wrapper_maybe))
513 -- Bind args to regs/stack as appropriate, and
514 -- record expected position of sps
515 bindArgsToRegs reg_args arg_regs `thenC`
516 mapCs bindNewToAStack stk_bxd_w_offsets `thenC`
517 mapCs bindNewToBStack stk_ubxd_w_offsets `thenC`
518 setRealAndVirtualSps spA_stk_args spB_stk_args `thenC`
520 -- Enter the closures cc, if required
521 enterCostCentreCode closure_info cc IsFunction `thenC`
524 funWrapper closure_info arg_regs (cgExpr body)
526 -- Make a labelled code-block for the slow and fast entry code
527 forkAbsC (if slow_code_needed then slow_entry_code else absC AbsCNop)
528 `thenFC` \ slow_abs_c ->
529 forkAbsC fast_entry_code `thenFC` \ fast_abs_c ->
530 moduleName `thenFC` \ mod_name ->
532 -- Now either construct the info table, or put the fast code in alone
533 -- (We never have slow code without an info table)
535 if info_table_needed then
536 CClosureInfoAndCode closure_info slow_abs_c (Just fast_abs_c)
537 stdUpd (cl_descr mod_name)
538 (dataConLiveness closure_info)
540 CCodeBlock fast_label fast_abs_c
543 lf_info = closureLFInfo closure_info
545 cl_descr mod_name = closureDescription mod_name id all_args body
547 -- Figure out what is needed and what isn't
548 slow_code_needed = slowFunEntryCodeRequired id binder_info
549 info_table_needed = funInfoTableRequired id binder_info lf_info
551 -- Manufacture labels
552 id = closureId closure_info
554 fast_label = fastLabelFromCI closure_info
556 stdUpd = CLbl mkErrorStdEntryLabel CodePtrRep
558 wrapper_maybe = get_ultimate_wrapper Nothing id
560 get_ultimate_wrapper deflt x -- walk all the way up a "wrapper chain"
561 = case (myWrapperMaybe x) of
563 Just xx -> get_ultimate_wrapper (Just xx) xx
565 show_wrapper_name Nothing = ""
566 show_wrapper_name (Just xx) = showId PprDebug xx
568 show_wrapper_arg_kinds Nothing = ""
569 show_wrapper_arg_kinds (Just xx)
570 = case (getWrapperArgTypeCategories (idType xx) (getIdStrictness xx)) of
575 For lexically scoped profiling we have to load the cost centre from
576 the closure entered, if the costs are not supposed to be inherited.
577 This is done immediately on entering the fast entry point.
579 Load current cost centre from closure, if not inherited.
580 Node is guaranteed to point to it, if profiling and not inherited.
583 data IsThunk = IsThunk | IsFunction -- Bool-like, local
588 enterCostCentreCode :: ClosureInfo -> CostCentre -> IsThunk -> Code
590 enterCostCentreCode closure_info cc is_thunk
591 = costCentresFlag `thenFC` \ profiling_on ->
592 if not profiling_on then
595 ASSERT(not (noCostCentreAttached cc))
597 if costsAreSubsumed cc then
598 ASSERT(isToplevClosure closure_info)
599 ASSERT(is_thunk == IsFunction)
600 costCentresC SLIT("ENTER_CC_FSUB") []
602 else if currentOrSubsumedCosts cc then
603 -- i.e. current; subsumed dealt with above
604 -- get CCC out of the closure, where we put it when we alloc'd
606 IsThunk -> costCentresC SLIT("ENTER_CC_TCL") [CReg node]
607 IsFunction -> costCentresC SLIT("ENTER_CC_FCL") [CReg node]
609 else if isCafCC cc && isToplevClosure closure_info then
610 ASSERT(is_thunk == IsThunk)
611 costCentresC SLIT("ENTER_CC_CAF") [mkCCostCentre cc]
613 else -- we've got a "real" cost centre right here in our hands...
615 IsThunk -> costCentresC SLIT("ENTER_CC_T") [mkCCostCentre cc]
616 IsFunction -> if isCafCC cc || isDictCC cc
617 then costCentresC SLIT("ENTER_CC_FCAF") [mkCCostCentre cc]
618 else costCentresC SLIT("ENTER_CC_FLOAD") [mkCCostCentre cc]
621 %************************************************************************
623 \subsubsection[pre-closure-code-stuff]{Pre-closure-code code}
625 %************************************************************************
627 The argument-satisfaction check code is placed after binding
628 the arguments to their stack locations. Hence, the virtual stack
629 pointer is pointing after all the args, and virtual offset 1 means
630 the base of frame and hence most distant arg. Hence
631 virtual offset 0 is just beyond the most distant argument; the
632 relative offset of this word tells how many words of arguments
636 argSatisfactionCheck :: ClosureInfo -> [Id] -> Code
638 argSatisfactionCheck closure_info [] = nopC
640 argSatisfactionCheck closure_info args
641 = -- safest way to determine which stack last arg will be on:
642 -- look up CAddrMode that last arg is bound to;
644 -- check isFollowableRep.
646 nodeMustPointToIt (closureLFInfo closure_info) `thenFC` \ node_points ->
649 emit_gran_macros = opt_GranMacros
653 -- absC (CMacroStmt GRAN_FETCH []) `thenC`
654 -- forceHeapCheck [] node_points (absC AbsCNop) `thenC`
657 then fetchAndReschedule [] node_points
658 else yield [] node_points
659 else absC AbsCNop) `thenC`
661 getCAddrMode (last args) `thenFC` \ last_amode ->
663 if (isFollowableRep (getAmodeRep last_amode)) then
664 getSpARelOffset 0 `thenFC` \ (SpARel spA off) ->
666 a_rel_int = spARelToInt spA off
667 a_rel_arg = mkIntCLit a_rel_int
669 ASSERT(a_rel_int /= 0)
671 absC (CMacroStmt ARGS_CHK_A [a_rel_arg])
673 absC (CMacroStmt ARGS_CHK_A_LOAD_NODE [a_rel_arg, set_Node_to_this])
675 getSpBRelOffset 0 `thenFC` \ (SpBRel spB off) ->
677 b_rel_int = spBRelToInt spB off
678 b_rel_arg = mkIntCLit b_rel_int
680 ASSERT(b_rel_int /= 0)
682 absC (CMacroStmt ARGS_CHK_B [b_rel_arg])
684 absC (CMacroStmt ARGS_CHK_B_LOAD_NODE [b_rel_arg, set_Node_to_this])
686 -- We must tell the arg-satis macro whether Node is pointing to
687 -- the closure or not. If it isn't so pointing, then we give to
688 -- the macro the (static) address of the closure.
690 set_Node_to_this = CLbl (closureLabelFromCI closure_info) PtrRep
693 %************************************************************************
695 \subsubsection[closure-code-wrappers]{Wrappers around closure code}
697 %************************************************************************
700 thunkWrapper:: ClosureInfo -> Code -> Code
701 thunkWrapper closure_info thunk_code
702 = -- Stack and heap overflow checks
703 nodeMustPointToIt (closureLFInfo closure_info) `thenFC` \ node_points ->
706 emit_gran_macros = opt_GranMacros
708 -- HWL: insert macros for GrAnSim; 2 versions depending on liveness of node
709 -- (we prefer fetchAndReschedule-style context switches to yield ones)
712 then fetchAndReschedule [] node_points
713 else yield [] node_points
714 else absC AbsCNop) `thenC`
716 stackCheck closure_info [] node_points ( -- stackCheck *encloses* the rest
718 -- Must be after stackCheck: if stchk fails new stack
719 -- space has to be allocated from the heap
721 heapCheck [] node_points (
722 -- heapCheck *encloses* the rest
723 -- The "[]" says there are no live argument registers
725 -- Overwrite with black hole if necessary
726 blackHoleIt closure_info `thenC`
728 -- Push update frame if necessary
729 setupUpdate closure_info ( -- setupUpdate *encloses* the rest
733 funWrapper :: ClosureInfo -- Closure whose code body this is
734 -> [MagicId] -- List of argument registers (if any)
735 -> Code -- Body of function being compiled
737 funWrapper closure_info arg_regs fun_body
738 = -- Stack overflow check
739 nodeMustPointToIt (closureLFInfo closure_info) `thenFC` \ node_points ->
741 emit_gran_macros = opt_GranMacros
745 then yield arg_regs node_points
746 else absC AbsCNop) `thenC`
748 stackCheck closure_info arg_regs node_points ( -- stackCheck *encloses* the rest
750 -- Heap overflow check
751 heapCheck arg_regs node_points (
752 -- heapCheck *encloses* the rest
754 -- Finally, do the business
759 %************************************************************************
761 \subsubsubsection[overflow-checks]{Stack and heap overflow wrappers}
763 %************************************************************************
765 Assumption: virtual and real stack pointers are currently exactly aligned.
768 stackCheck :: ClosureInfo
769 -> [MagicId] -- Live registers
770 -> Bool -- Node required to point after check?
774 stackCheck closure_info regs node_reqd code
775 = getFinalStackHW (\ aHw -> \ bHw -> -- Both virtual stack offsets
777 getVirtSps `thenFC` \ (vSpA, vSpB) ->
779 let a_headroom_reqd = aHw - vSpA -- Virtual offsets are positive integers
780 b_headroom_reqd = bHw - vSpB
783 absC (if (a_headroom_reqd == 0 && b_headroom_reqd == 0) then
786 CMacroStmt STK_CHK [mkIntCLit liveness_mask,
787 mkIntCLit a_headroom_reqd,
788 mkIntCLit b_headroom_reqd,
791 mkIntCLit (if returns_prim_type then 1 else 0),
792 mkIntCLit (if node_reqd then 1 else 0)]
794 -- The test is *inside* the absC, to avoid black holes!
799 all_regs = if node_reqd then node:regs else regs
800 liveness_mask = mkLiveRegsMask all_regs
802 returns_prim_type = closureReturnsUnboxedType closure_info
805 %************************************************************************
807 \subsubsubsection[update-and-BHs]{Update and black-hole wrappers}
809 %************************************************************************
813 blackHoleIt :: ClosureInfo -> Code -- Only called for thunks
814 blackHoleIt closure_info
815 = noBlackHolingFlag `thenFC` \ no_black_holing ->
817 if (blackHoleOnEntry no_black_holing closure_info)
819 absC (if closureSingleEntry(closure_info) then
820 CMacroStmt UPD_BH_SINGLE_ENTRY [CReg node]
822 CMacroStmt UPD_BH_UPDATABLE [CReg node])
823 -- Node always points to it; see stg-details
829 setupUpdate :: ClosureInfo -> Code -> Code -- Only called for thunks
830 -- Nota Bene: this function does not change Node (even if it's a CAF),
831 -- so that the cost centre in the original closure can still be
832 -- extracted by a subsequent ENTER_CC_TCL
834 setupUpdate closure_info code
835 = if (closureUpdReqd closure_info) then
836 link_caf_if_needed `thenFC` \ update_closure ->
837 pushUpdateFrame update_closure vector code
839 profCtrC SLIT("UPDF_OMITTED") [] `thenC`
842 link_caf_if_needed :: FCode CAddrMode -- Returns amode for closure to be updated
844 = if not (isStaticClosure closure_info) then
848 -- First we must allocate a black hole, and link the
849 -- CAF onto the CAF list
851 -- Alloc black hole specifying CC_HDR(Node) as the cost centre
852 -- Hack Warning: Using a CLitLit to get CAddrMode !
854 use_cc = CLitLit SLIT("CC_HDR(R1.p)") PtrRep
857 allocDynClosure (blackHoleClosureInfo closure_info) use_cc blame_cc []
858 `thenFC` \ heap_offset ->
859 getHpRelOffset heap_offset `thenFC` \ hp_rel ->
860 let amode = CAddr hp_rel
862 absC (CMacroStmt UPD_CAF [CReg node, amode])
867 = case (closureType closure_info) of
868 Nothing -> CReg StdUpdRetVecReg
869 Just (spec_tycon, _, spec_datacons) ->
870 case (ctrlReturnConvAlg spec_tycon) of
871 UnvectoredReturn 1 ->
873 spec_data_con = head spec_datacons
874 only_tag = dataConTag spec_data_con
876 direct = case (dataReturnConvAlg spec_data_con) of
877 ReturnInRegs _ -> mkConUpdCodePtrVecLabel spec_tycon only_tag
878 ReturnInHeap -> mkStdUpdCodePtrVecLabel spec_tycon only_tag
880 vectored = mkStdUpdVecTblLabel spec_tycon
882 CUnVecLbl direct vectored
884 UnvectoredReturn _ -> CReg StdUpdRetVecReg
885 VectoredReturn _ -> CLbl (mkStdUpdVecTblLabel spec_tycon) DataPtrRep
888 %************************************************************************
890 \subsection[CgClosure-Description]{Profiling Closure Description.}
892 %************************************************************************
894 For "global" data constructors the description is simply occurrence
895 name of the data constructor itself (see \ref{CgConTbls-info-tables}).
897 Otherwise it is determind by @closureDescription@ from the let
901 closureDescription :: FAST_STRING -- Module
902 -> Id -- Id of closure binding
907 -- Not called for StgRhsCon which have global info tables built in
908 -- CgConTbls.lhs with a description generated from the data constructor
910 closureDescription mod_name name args body
911 = uppShow 0 (prettyToUn (
912 ppBesides [ppChar '<',
920 chooseDynCostCentres cc args fvs body
922 use_cc -- cost-centre we record in the object
923 = if currentOrSubsumedCosts cc
924 then CReg CurCostCentre
925 else mkCCostCentre cc
927 blame_cc -- cost-centre on whom we blame the allocation
928 = case (args, fvs, body) of
929 ([], [just1], StgApp (StgVarArg fun) [{-no args-}] _)
931 -> mkCCostCentre overheadCostCentre
934 -- if it's an utterly trivial RHS, then it must be
935 -- one introduced by boxHigherOrderArgs for profiling,
936 -- so we charge it to "OVERHEAD".