2 % (c) The Univserity of Glasgow 1992-2004
5 Data structures which describe closures, and
6 operations over those data structures
8 Nothing monadic in here
10 Much of the rationale for these things is in the ``details'' part of
15 ClosureInfo, LambdaFormInfo, SMRep, -- all abstract
18 ArgDescr(..), Liveness(..),
21 mkLFThunk, mkLFReEntrant, mkConLFInfo, mkSelectorLFInfo,
22 mkApLFInfo, mkLFImported, mkLFArgument, mkLFLetNoEscape,
24 mkClosureInfo, mkConInfo,
26 closureSize, closureNonHdrSize,
27 closureGoodStuffSize, closurePtrsSize,
30 closureName, infoTableLabelFromCI,
31 closureLabelFromCI, closureSRT,
32 closureLFInfo, closureSMRep, closureUpdReqd,
34 closureSingleEntry, closureReEntrant, isConstrClosure_maybe,
35 closureFunInfo, isStandardFormThunk, isKnownFun,
37 enterIdLabel, enterLocalIdLabel, enterReturnPtLabel,
40 CallMethod(..), getCallMethod,
44 staticClosureRequired,
48 closureValDescr, closureTypeDescr, -- profiling
51 cafBlackHoleClosureInfo, seCafBlackHoleClosureInfo,
53 staticClosureNeedsLink,
56 #include "../includes/MachDeps.h"
57 #include "HsVersions.h"
60 import SMRep -- all of it
64 import Constants ( mIN_UPD_SIZE, mIN_SIZE_NonUpdHeapObject )
65 import Packages ( isDllName )
66 import CmdLineOpts ( DynFlags, opt_SccProfilingOn, opt_OmitBlackHoling,
67 opt_Parallel, opt_DoTickyProfiling,
69 import Id ( Id, idType, idArity, idName )
70 import DataCon ( DataCon, dataConTyCon, isNullaryRepDataCon, dataConName )
71 import Name ( Name, nameUnique, getOccName, getOccString )
72 import OccName ( occNameUserString )
73 import Type ( isUnLiftedType, Type, repType, splitTyConApp_maybe )
74 import TcType ( tcSplitSigmaTy )
75 import TyCon ( isFunTyCon, isAbstractTyCon )
76 import BasicTypes ( TopLevelFlag(..), isNotTopLevel, isTopLevel, ipNameName )
81 import TypeRep -- TEMP
85 %************************************************************************
87 \subsection[ClosureInfo-datatypes]{Data types for closure information}
89 %************************************************************************
91 Information about a closure, from the code generator's point of view.
93 A ClosureInfo decribes the info pointer of a closure. It has
95 a) to construct the info table itself
96 b) to allocate a closure containing that info pointer (i.e.
97 it knows the info table label)
99 We make a ClosureInfo for
100 - each let binding (both top level and not)
101 - each data constructor (for its shared static and
107 closureName :: !Name, -- The thing bound to this closure
108 closureLFInfo :: !LambdaFormInfo, -- NOTE: not an LFCon (see below)
109 closureSMRep :: !SMRep, -- representation used by storage mgr
110 closureSRT :: !C_SRT, -- What SRT applies to this closure
111 closureType :: !Type, -- Type of closure (ToDo: remove)
112 closureDescr :: !String -- closure description (for profiling)
115 -- Constructor closures don't have a unique info table label (they use
116 -- the constructor's info table), and they don't have an SRT.
118 closureCon :: !DataCon,
119 closureSMRep :: !SMRep,
120 closureDllCon :: !Bool -- is in a separate DLL
123 -- C_SRT is what StgSyn.SRT gets translated to...
124 -- we add a label for the table, and expect only the 'offset/length' form
127 | C_SRT !CLabel !WordOff !StgHalfWord {-bitmap or escape-}
129 needsSRT :: C_SRT -> Bool
130 needsSRT NoC_SRT = False
131 needsSRT (C_SRT _ _ _) = True
134 %************************************************************************
136 \subsubsection[LambdaFormInfo-datatype]{@LambdaFormInfo@: source-derivable info}
138 %************************************************************************
140 Information about an identifier, from the code generator's point of
141 view. Every identifier is bound to a LambdaFormInfo in the
142 environment, which gives the code generator enough info to be able to
143 tail call or return that identifier.
145 Note that a closure is usually bound to an identifier, so a
146 ClosureInfo contains a LambdaFormInfo.
150 = LFReEntrant -- Reentrant closure (a function)
151 TopLevelFlag -- True if top level
152 !Int -- Arity. Invariant: always > 0
153 !Bool -- True <=> no fvs
154 ArgDescr -- Argument descriptor (should reall be in ClosureInfo)
156 | LFCon -- A saturated constructor application
157 DataCon -- The constructor
159 | LFThunk -- Thunk (zero arity)
161 !Bool -- True <=> no free vars
162 !Bool -- True <=> updatable (i.e., *not* single-entry)
164 !Bool -- True <=> *might* be a function type
166 | LFUnknown -- Used for function arguments and imported things.
167 -- We know nothing about this closure. Treat like
168 -- updatable "LFThunk"...
169 -- Imported things which we do know something about use
170 -- one of the other LF constructors (eg LFReEntrant for
172 !Bool -- True <=> *might* be a function type
174 | LFLetNoEscape -- See LetNoEscape module for precise description of
178 | LFBlackHole -- Used for the closures allocated to hold the result
179 -- of a CAF. We want the target of the update frame to
180 -- be in the heap, so we make a black hole to hold it.
181 CLabel -- Flavour (info label, eg CAF_BLACKHOLE_info).
184 -------------------------
185 -- An ArgDsecr describes the argument pattern of a function
188 = ArgSpec -- Fits one of the standard patterns
189 !Int -- RTS type identifier ARG_P, ARG_N, ...
191 | ArgGen -- General case
192 Liveness -- Details about the arguments
195 -------------------------
196 -- We represent liveness bitmaps as a Bitmap (whose internal
197 -- representation really is a bitmap). These are pinned onto case return
198 -- vectors to indicate the state of the stack for the garbage collector.
200 -- In the compiled program, liveness bitmaps that fit inside a single
201 -- word (StgWord) are stored as a single word, while larger bitmaps are
202 -- stored as a pointer to an array of words.
205 = SmallLiveness -- Liveness info that fits in one word
206 StgWord -- Here's the bitmap
208 | BigLiveness -- Liveness info witha a multi-word bitmap
209 CLabel -- Label for the bitmap
212 -------------------------
213 -- StandardFormInfo tells whether this thunk has one of
214 -- a small number of standard forms
216 data StandardFormInfo
218 -- Not of of the standard forms
221 -- A SelectorThunk is of form
223 -- con a1,..,an -> ak
224 -- and the constructor is from a single-constr type.
225 WordOff -- 0-origin offset of ak within the "goods" of
226 -- constructor (Recall that the a1,...,an may be laid
227 -- out in the heap in a non-obvious order.)
230 -- An ApThunk is of form
232 -- The code for the thunk just pushes x2..xn on the stack and enters x1.
233 -- There are a few of these (for 1 <= n <= MAX_SPEC_AP_SIZE) pre-compiled
234 -- in the RTS to save space.
238 %************************************************************************
240 \subsection[ClosureInfo-construction]{Functions which build LFInfos}
242 %************************************************************************
245 mkLFReEntrant :: TopLevelFlag -- True of top level
248 -> ArgDescr -- Argument descriptor
251 mkLFReEntrant top fvs args arg_descr
252 = LFReEntrant top (length args) (null fvs) arg_descr
254 mkLFThunk thunk_ty top fvs upd_flag
255 = ASSERT( not (isUpdatable upd_flag) || not (isUnLiftedType thunk_ty) )
256 LFThunk top (null fvs)
257 (isUpdatable upd_flag)
259 (might_be_a_function thunk_ty)
261 might_be_a_function :: Type -> Bool
262 might_be_a_function ty
263 | Just (tc,_) <- splitTyConApp_maybe (repType ty),
264 not (isFunTyCon tc) && not (isAbstractTyCon tc) = False
265 -- don't forget to check for abstract types, which might
270 @mkConLFInfo@ is similar, for constructors.
273 mkConLFInfo :: DataCon -> LambdaFormInfo
274 mkConLFInfo con = LFCon con
276 mkSelectorLFInfo id offset updatable
277 = LFThunk NotTopLevel False updatable (SelectorThunk offset)
278 (might_be_a_function (idType id))
280 mkApLFInfo id upd_flag arity
281 = LFThunk NotTopLevel (arity == 0) (isUpdatable upd_flag) (ApThunk arity)
282 (might_be_a_function (idType id))
285 Miscellaneous LF-infos.
288 mkLFArgument id = LFUnknown (might_be_a_function (idType id))
290 mkLFLetNoEscape = LFLetNoEscape
292 mkLFImported :: Id -> LambdaFormInfo
295 n | n > 0 -> LFReEntrant TopLevel n True (panic "arg_descr") -- n > 0
296 other -> mkLFArgument id -- Not sure of exact arity
299 %************************************************************************
301 Building ClosureInfos
303 %************************************************************************
306 mkClosureInfo :: Bool -- Is static
309 -> Int -> Int -- Total and pointer words
311 -> String -- String descriptor
313 mkClosureInfo is_static id lf_info tot_wds ptr_wds srt_info descr
314 = ClosureInfo { closureName = name,
315 closureLFInfo = lf_info,
316 closureSMRep = sm_rep,
317 closureSRT = srt_info,
318 closureType = idType id,
319 closureDescr = descr }
322 sm_rep = chooseSMRep is_static lf_info tot_wds ptr_wds
324 mkConInfo :: DynFlags
327 -> Int -> Int -- Total and pointer words
329 mkConInfo dflags is_static data_con tot_wds ptr_wds
330 = ConInfo { closureSMRep = sm_rep,
331 closureCon = data_con,
332 closureDllCon = isDllName dflags (dataConName data_con) }
334 sm_rep = chooseSMRep is_static (mkConLFInfo data_con) tot_wds ptr_wds
337 %************************************************************************
339 \subsection[ClosureInfo-sizes]{Functions about closure {\em sizes}}
341 %************************************************************************
344 closureSize :: ClosureInfo -> WordOff
345 closureSize cl_info = fixedHdrSize + closureNonHdrSize cl_info
347 closureNonHdrSize :: ClosureInfo -> WordOff
348 closureNonHdrSize cl_info
349 = tot_wds + computeSlopSize tot_wds
350 (closureSMRep cl_info)
351 (closureNeedsUpdSpace cl_info)
353 tot_wds = closureGoodStuffSize cl_info
355 -- we leave space for an update if either (a) the closure is updatable
356 -- or (b) it is a static thunk. This is because a static thunk needs
357 -- a static link field in a predictable place (after the slop), regardless
358 -- of whether it is updatable or not.
359 closureNeedsUpdSpace (ClosureInfo { closureLFInfo =
360 LFThunk TopLevel _ _ _ _ }) = True
361 closureNeedsUpdSpace cl_info = closureUpdReqd cl_info
363 slopSize :: ClosureInfo -> WordOff
365 = computeSlopSize (closureGoodStuffSize cl_info)
366 (closureSMRep cl_info)
367 (closureNeedsUpdSpace cl_info)
369 closureGoodStuffSize :: ClosureInfo -> WordOff
370 closureGoodStuffSize cl_info
371 = let (ptrs, nonptrs) = sizes_from_SMRep (closureSMRep cl_info)
374 closurePtrsSize :: ClosureInfo -> WordOff
375 closurePtrsSize cl_info
376 = let (ptrs, _) = sizes_from_SMRep (closureSMRep cl_info)
380 sizes_from_SMRep :: SMRep -> (WordOff,WordOff)
381 sizes_from_SMRep (GenericRep _ ptrs nonptrs _) = (ptrs, nonptrs)
382 sizes_from_SMRep BlackHoleRep = (0, 0)
385 Computing slop size. WARNING: this looks dodgy --- it has deep
386 knowledge of what the storage manager does with the various
392 Updateable closures must be @mIN_UPD_SIZE@.
395 Indirections require 1 word
397 Appels collector indirections 2 words
399 THEREFORE: @mIN_UPD_SIZE = 2@.
402 Collectable closures which are allocated in the heap
403 must be @mIN_SIZE_NonUpdHeapObject@.
405 Copying collector forward pointer requires 1 word
407 THEREFORE: @mIN_SIZE_NonUpdHeapObject = 1@
410 Static closures have an extra ``static link field'' at the end, but we
411 don't bother taking that into account here.
414 computeSlopSize :: WordOff -> SMRep -> Bool -> WordOff
416 computeSlopSize tot_wds (GenericRep _ _ _ _) True -- Updatable
417 = max 0 (mIN_UPD_SIZE - tot_wds)
419 computeSlopSize tot_wds (GenericRep True _ _ _) False -- Non updatable
422 computeSlopSize tot_wds (GenericRep False _ _ _) False -- Non updatable
423 = max 0 (mIN_SIZE_NonUpdHeapObject - tot_wds) -- Dynamic
425 computeSlopSize tot_wds BlackHoleRep _ -- Updatable
426 = max 0 (mIN_UPD_SIZE - tot_wds)
429 %************************************************************************
431 \subsection[SMreps]{Choosing SM reps}
433 %************************************************************************
437 :: Bool -- True <=> static closure
439 -> WordOff -> WordOff -- Tot wds, ptr wds
442 chooseSMRep is_static lf_info tot_wds ptr_wds
444 nonptr_wds = tot_wds - ptr_wds
445 closure_type = getClosureType is_static ptr_wds lf_info
447 GenericRep is_static ptr_wds nonptr_wds closure_type
449 -- We *do* get non-updatable top-level thunks sometimes. eg. f = g
450 -- gets compiled to a jump to g (if g has non-zero arity), instead of
451 -- messing around with update frames and PAPs. We set the closure type
452 -- to FUN_STATIC in this case.
454 getClosureType :: Bool -> WordOff -> LambdaFormInfo -> ClosureType
455 getClosureType is_static ptr_wds lf_info
457 LFCon con | is_static && ptr_wds == 0 -> ConstrNoCaf
458 | otherwise -> Constr
459 LFReEntrant _ _ _ _ -> Fun
460 LFThunk _ _ _ (SelectorThunk _) _ -> ThunkSelector
461 LFThunk _ _ _ _ _ -> Thunk
462 _ -> panic "getClosureType"
465 %************************************************************************
467 \subsection[ClosureInfo-4-questions]{Four major questions about @ClosureInfo@}
469 %************************************************************************
471 Be sure to see the stg-details notes about these...
474 nodeMustPointToIt :: LambdaFormInfo -> Bool
475 nodeMustPointToIt (LFReEntrant top _ no_fvs _)
476 = not no_fvs || -- Certainly if it has fvs we need to point to it
478 -- If it is not top level we will point to it
479 -- We can have a \r closure with no_fvs which
480 -- is not top level as special case cgRhsClosure
481 -- has been dissabled in favour of let floating
483 -- For lex_profiling we also access the cost centre for a
484 -- non-inherited function i.e. not top level
485 -- the not top case above ensures this is ok.
487 nodeMustPointToIt (LFCon _) = True
489 -- Strictly speaking, the above two don't need Node to point
490 -- to it if the arity = 0. But this is a *really* unlikely
491 -- situation. If we know it's nil (say) and we are entering
492 -- it. Eg: let x = [] in x then we will certainly have inlined
493 -- x, since nil is a simple atom. So we gain little by not
494 -- having Node point to known zero-arity things. On the other
495 -- hand, we do lose something; Patrick's code for figuring out
496 -- when something has been updated but not entered relies on
497 -- having Node point to the result of an update. SLPJ
500 nodeMustPointToIt (LFThunk _ no_fvs updatable NonStandardThunk _)
501 = updatable || not no_fvs || opt_SccProfilingOn
502 -- For the non-updatable (single-entry case):
504 -- True if has fvs (in which case we need access to them, and we
505 -- should black-hole it)
506 -- or profiling (in which case we need to recover the cost centre
509 nodeMustPointToIt (LFThunk _ no_fvs updatable some_standard_form_thunk _)
510 = True -- Node must point to any standard-form thunk
512 nodeMustPointToIt (LFUnknown _) = True
513 nodeMustPointToIt (LFBlackHole _) = True -- BH entry may require Node to point
514 nodeMustPointToIt (LFLetNoEscape _) = False
517 The entry conventions depend on the type of closure being entered,
518 whether or not it has free variables, and whether we're running
519 sequentially or in parallel.
521 \begin{tabular}{lllll}
522 Closure Characteristics & Parallel & Node Req'd & Argument Passing & Enter Via \\
523 Unknown & no & yes & stack & node \\
524 Known fun ($\ge$ 1 arg), no fvs & no & no & registers & fast entry (enough args) \\
525 \ & \ & \ & \ & slow entry (otherwise) \\
526 Known fun ($\ge$ 1 arg), fvs & no & yes & registers & fast entry (enough args) \\
527 0 arg, no fvs @\r,\s@ & no & no & n/a & direct entry \\
528 0 arg, no fvs @\u@ & no & yes & n/a & node \\
529 0 arg, fvs @\r,\s@ & no & yes & n/a & direct entry \\
530 0 arg, fvs @\u@ & no & yes & n/a & node \\
532 Unknown & yes & yes & stack & node \\
533 Known fun ($\ge$ 1 arg), no fvs & yes & no & registers & fast entry (enough args) \\
534 \ & \ & \ & \ & slow entry (otherwise) \\
535 Known fun ($\ge$ 1 arg), fvs & yes & yes & registers & node \\
536 0 arg, no fvs @\r,\s@ & yes & no & n/a & direct entry \\
537 0 arg, no fvs @\u@ & yes & yes & n/a & node \\
538 0 arg, fvs @\r,\s@ & yes & yes & n/a & node \\
539 0 arg, fvs @\u@ & yes & yes & n/a & node\\
542 When black-holing, single-entry closures could also be entered via node
543 (rather than directly) to catch double-entry.
547 = EnterIt -- no args, not a function
549 | JumpToIt CLabel -- no args, not a function, but we
550 -- know what its entry code is
552 | ReturnIt -- it's a function, but we have
553 -- zero args to apply to it, so just
556 | ReturnCon DataCon -- It's a data constructor, just return it
558 | SlowCall -- Unknown fun, or known fun with
561 | DirectEntry -- Jump directly, with args in regs
562 CLabel -- The code label
565 getCallMethod :: DynFlags
566 -> Name -- Function being applied
567 -> LambdaFormInfo -- Its info
568 -> Int -- Number of available arguments
571 getCallMethod dflags name lf_info n_args
572 | nodeMustPointToIt lf_info && opt_Parallel
573 = -- If we're parallel, then we must always enter via node.
574 -- The reason is that the closure may have been
575 -- fetched since we allocated it.
578 getCallMethod dflags name (LFReEntrant _ arity _ _) n_args
579 | n_args == 0 = ASSERT( arity /= 0 )
580 ReturnIt -- No args at all
581 | n_args < arity = SlowCall -- Not enough args
582 | otherwise = DirectEntry (enterIdLabel dflags name) arity
584 getCallMethod dflags name (LFCon con) n_args
585 = ASSERT( n_args == 0 )
588 getCallMethod dflags name (LFThunk _ _ updatable std_form_info is_fun) n_args
589 | is_fun -- Must always "call" a function-typed
590 = SlowCall -- thing, cannot just enter it [in eval/apply, the entry code
591 -- is the fast-entry code]
593 | updatable || opt_DoTickyProfiling -- to catch double entry
594 || opt_SMP -- Always enter via node on SMP, since the
595 -- thunk might have been blackholed in the
597 = ASSERT( n_args == 0 ) EnterIt
599 | otherwise -- Jump direct to code for single-entry thunks
600 = ASSERT( n_args == 0 )
601 JumpToIt (thunkEntryLabel dflags name std_form_info updatable)
603 getCallMethod dflags name (LFUnknown True) n_args
604 = SlowCall -- might be a function
606 getCallMethod dflags name (LFUnknown False) n_args
607 = ASSERT2 ( n_args == 0, ppr name <+> ppr n_args )
608 EnterIt -- Not a function
610 getCallMethod dflags name (LFBlackHole _) n_args
611 = SlowCall -- Presumably the black hole has by now
612 -- been updated, but we don't know with
613 -- what, so we slow call it
615 getCallMethod dflags name (LFLetNoEscape 0) n_args
616 = JumpToIt (enterReturnPtLabel (nameUnique name))
618 getCallMethod dflags name (LFLetNoEscape arity) n_args
619 | n_args == arity = DirectEntry (enterReturnPtLabel (nameUnique name)) arity
620 | otherwise = pprPanic "let-no-escape: " (ppr name <+> ppr arity)
622 blackHoleOnEntry :: ClosureInfo -> Bool
623 -- Static closures are never themselves black-holed.
624 -- Updatable ones will be overwritten with a CAFList cell, which points to a
626 -- Single-entry ones have no fvs to plug, and we trust they don't form part
629 blackHoleOnEntry ConInfo{} = False
630 blackHoleOnEntry (ClosureInfo { closureLFInfo = lf_info, closureSMRep = rep })
632 = False -- Never black-hole a static closure
636 LFReEntrant _ _ _ _ -> False
637 LFLetNoEscape _ -> False
638 LFThunk _ no_fvs updatable _ _
640 then not opt_OmitBlackHoling
641 else opt_DoTickyProfiling || not no_fvs
642 -- the former to catch double entry,
643 -- and the latter to plug space-leaks. KSW/SDM 1999-04.
645 other -> panic "blackHoleOnEntry" -- Should never happen
647 isStandardFormThunk :: LambdaFormInfo -> Bool
648 isStandardFormThunk (LFThunk _ _ _ (SelectorThunk _) _) = True
649 isStandardFormThunk (LFThunk _ _ _ (ApThunk _) _) = True
650 isStandardFormThunk other_lf_info = False
652 isKnownFun :: LambdaFormInfo -> Bool
653 isKnownFun (LFReEntrant _ _ _ _) = True
654 isKnownFun (LFLetNoEscape _) = True
658 -----------------------------------------------------------------------------
662 staticClosureNeedsLink :: ClosureInfo -> Bool
663 -- A static closure needs a link field to aid the GC when traversing
664 -- the static closure graph. But it only needs such a field if either
666 -- b) it's a constructor with one or more pointer fields
667 -- In case (b), the constructor's fields themselves play the role
669 staticClosureNeedsLink (ClosureInfo { closureSRT = srt })
671 staticClosureNeedsLink (ConInfo { closureSMRep = sm_rep, closureCon = con })
672 = not (isNullaryRepDataCon con) && not_nocaf_constr
676 GenericRep _ _ _ ConstrNoCaf -> False
680 Avoiding generating entries and info tables
681 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
682 At present, for every function we generate all of the following,
683 just in case. But they aren't always all needed, as noted below:
685 [NB1: all of this applies only to *functions*. Thunks always
686 have closure, info table, and entry code.]
688 [NB2: All are needed if the function is *exported*, just to play safe.]
691 * Fast-entry code ALWAYS NEEDED
694 Needed iff (a) we have any un-saturated calls to the function
695 OR (b) the function is passed as an arg
696 OR (c) we're in the parallel world and the function has free vars
697 [Reason: in parallel world, we always enter functions
698 with free vars via the closure.]
700 * The function closure
701 Needed iff (a) we have any un-saturated calls to the function
702 OR (b) the function is passed as an arg
703 OR (c) if the function has free vars (ie not top level)
705 Why case (a) here? Because if the arg-satis check fails,
706 UpdatePAP stuffs a pointer to the function closure in the PAP.
707 [Could be changed; UpdatePAP could stuff in a code ptr instead,
708 but doesn't seem worth it.]
710 [NB: these conditions imply that we might need the closure
711 without the slow-entry code. Here's how.
713 f x y = let g w = ...x..y..w...
717 Here we need a closure for g which contains x and y,
718 but since the calls are all saturated we just jump to the
719 fast entry point for g, with R1 pointing to the closure for g.]
722 * Standard info table
723 Needed iff (a) we have any un-saturated calls to the function
724 OR (b) the function is passed as an arg
725 OR (c) the function has free vars (ie not top level)
727 NB. In the sequential world, (c) is only required so that the function closure has
728 an info table to point to, to keep the storage manager happy.
729 If (c) alone is true we could fake up an info table by choosing
730 one of a standard family of info tables, whose entry code just
733 [NB In the parallel world (c) is needed regardless because
734 we enter functions with free vars via the closure.]
736 If (c) is retained, then we'll sometimes generate an info table
737 (for storage mgr purposes) without slow-entry code. Then we need
738 to use an error label in the info table to substitute for the absent
742 staticClosureRequired
747 staticClosureRequired binder bndr_info
748 (LFReEntrant top_level _ _ _) -- It's a function
749 = ASSERT( isTopLevel top_level )
750 -- Assumption: it's a top-level, no-free-var binding
751 not (satCallsOnly bndr_info)
753 staticClosureRequired binder other_binder_info other_lf_info = True
756 %************************************************************************
758 \subsection[ClosureInfo-misc-funs]{Misc functions about @ClosureInfo@, etc.}
760 %************************************************************************
764 isStaticClosure :: ClosureInfo -> Bool
765 isStaticClosure cl_info = isStaticRep (closureSMRep cl_info)
767 closureUpdReqd :: ClosureInfo -> Bool
768 closureUpdReqd (ClosureInfo { closureLFInfo = LFThunk _ _ upd _ _ }) = upd
769 closureUpdReqd (ClosureInfo { closureLFInfo = LFBlackHole _ }) = True
770 -- Black-hole closures are allocated to receive the results of an
771 -- alg case with a named default... so they need to be updated.
772 closureUpdReqd other_closure = False
774 closureSingleEntry :: ClosureInfo -> Bool
775 closureSingleEntry (ClosureInfo { closureLFInfo = LFThunk _ _ upd _ _}) = not upd
776 closureSingleEntry other_closure = False
778 closureReEntrant :: ClosureInfo -> Bool
779 closureReEntrant (ClosureInfo { closureLFInfo = LFReEntrant _ _ _ _ }) = True
780 closureReEntrant other_closure = False
782 isConstrClosure_maybe :: ClosureInfo -> Maybe DataCon
783 isConstrClosure_maybe (ConInfo { closureCon = data_con }) = Just data_con
784 isConstrClosure_maybe _ = Nothing
786 closureFunInfo :: ClosureInfo -> Maybe (Int, ArgDescr)
787 closureFunInfo (ClosureInfo { closureLFInfo = LFReEntrant _ arity _ arg_desc})
788 = Just (arity, arg_desc)
794 isToplevClosure :: ClosureInfo -> Bool
795 isToplevClosure (ClosureInfo { closureLFInfo = lf_info })
797 LFReEntrant TopLevel _ _ _ -> True
798 LFThunk TopLevel _ _ _ _ -> True
800 isToplevClosure _ = False
806 infoTableLabelFromCI :: ClosureInfo -> CLabel
807 infoTableLabelFromCI (ClosureInfo { closureName = name,
808 closureLFInfo = lf_info,
809 closureSMRep = rep })
811 LFBlackHole info -> info
813 LFThunk _ _ upd_flag (SelectorThunk offset) _ ->
814 mkSelectorInfoLabel upd_flag offset
816 LFThunk _ _ upd_flag (ApThunk arity) _ ->
817 mkApInfoTableLabel upd_flag arity
819 LFThunk{} -> mkLocalInfoTableLabel name
821 LFReEntrant _ _ _ _ -> mkLocalInfoTableLabel name
823 other -> panic "infoTableLabelFromCI"
825 infoTableLabelFromCI (ConInfo { closureCon = con,
827 closureDllCon = dll })
828 | isStaticRep rep = mkStaticInfoTableLabel name dll
829 | otherwise = mkConInfoTableLabel name dll
831 name = dataConName con
833 -- ClosureInfo for a closure (as opposed to a constructor) is always local
834 closureLabelFromCI (ClosureInfo { closureName = nm }) = mkLocalClosureLabel nm
835 closureLabelFromCI _ = panic "closureLabelFromCI"
837 -- thunkEntryLabel is a local help function, not exported. It's used from both
838 -- entryLabelFromCI and getCallMethod.
840 thunkEntryLabel dflags thunk_id (ApThunk arity) is_updatable
841 = enterApLabel is_updatable arity
842 thunkEntryLabel dflags thunk_id (SelectorThunk offset) upd_flag
843 = enterSelectorLabel upd_flag offset
844 thunkEntryLabel dflags thunk_id _ is_updatable
845 = enterIdLabel dflags thunk_id
847 enterApLabel is_updatable arity
848 | tablesNextToCode = mkApInfoTableLabel is_updatable arity
849 | otherwise = mkApEntryLabel is_updatable arity
851 enterSelectorLabel upd_flag offset
852 | tablesNextToCode = mkSelectorInfoLabel upd_flag offset
853 | otherwise = mkSelectorEntryLabel upd_flag offset
855 enterIdLabel dflags id
856 | tablesNextToCode = mkInfoTableLabel dflags id
857 | otherwise = mkEntryLabel dflags id
860 | tablesNextToCode = mkLocalInfoTableLabel id
861 | otherwise = mkLocalEntryLabel id
863 enterReturnPtLabel name
864 | tablesNextToCode = mkReturnInfoLabel name
865 | otherwise = mkReturnPtLabel name
869 We need a black-hole closure info to pass to @allocDynClosure@ when we
870 want to allocate the black hole on entry to a CAF. These are the only
871 ways to build an LFBlackHole, maintaining the invariant that it really
872 is a black hole and not something else.
875 cafBlackHoleClosureInfo (ClosureInfo { closureName = nm,
877 = ClosureInfo { closureName = nm,
878 closureLFInfo = LFBlackHole mkCAFBlackHoleInfoTableLabel,
879 closureSMRep = BlackHoleRep,
880 closureSRT = NoC_SRT,
883 cafBlackHoleClosureInfo _ = panic "cafBlackHoleClosureInfo"
885 seCafBlackHoleClosureInfo (ClosureInfo { closureName = nm,
887 = ClosureInfo { closureName = nm,
888 closureLFInfo = LFBlackHole mkSECAFBlackHoleInfoTableLabel,
889 closureSMRep = BlackHoleRep,
890 closureSRT = NoC_SRT,
893 seCafBlackHoleClosureInfo _ = panic "seCafBlackHoleClosureInfo"
896 %************************************************************************
898 \subsection[ClosureInfo-Profiling-funs]{Misc functions about for profiling info.}
900 %************************************************************************
902 Profiling requires two pieces of information to be determined for
903 each closure's info table --- description and type.
905 The description is stored directly in the @CClosureInfoTable@ when the
908 The type is determined from the type information stored with the @Id@
909 in the closure info using @closureTypeDescr@.
912 closureValDescr, closureTypeDescr :: ClosureInfo -> String
913 closureValDescr (ClosureInfo {closureDescr = descr})
915 closureValDescr (ConInfo {closureCon = con})
916 = occNameUserString (getOccName con)
918 closureTypeDescr (ClosureInfo { closureType = ty })
919 = getTyDescription ty
920 closureTypeDescr (ConInfo { closureCon = data_con })
921 = occNameUserString (getOccName (dataConTyCon data_con))
923 getTyDescription :: Type -> String
925 = case (tcSplitSigmaTy ty) of { (_, _, tau_ty) ->
928 AppTy fun _ -> getTyDescription fun
929 FunTy _ res -> '-' : '>' : fun_result res
930 TyConApp tycon _ -> getOccString tycon
931 NoteTy (FTVNote _) ty -> getTyDescription ty
932 NoteTy (SynNote ty1) _ -> getTyDescription ty1
933 PredTy sty -> getPredTyDescription sty
934 ForAllTy _ ty -> getTyDescription ty
937 fun_result (FunTy _ res) = '>' : fun_result res
938 fun_result other = getTyDescription other
940 getPredTyDescription (ClassP cl tys) = getOccString cl
941 getPredTyDescription (IParam ip ty) = getOccString (ipNameName ip)