1 -----------------------------------------------------------------------------
3 -- Stg to C-- code generation:
5 -- The types LambdaFormInfo
8 -- Nothing monadic in here!
10 -- (c) The University of Glasgow 2004-2006
12 -----------------------------------------------------------------------------
15 module StgCmmClosure (
17 DynTag, tagForCon, isSmallFamily,
20 ArgDescr(..), Liveness(..),
23 isVoidRep, isGcPtrRep, addIdReps, addArgReps,
26 LambdaFormInfo, -- Abstract
27 StandardFormInfo, -- ...ditto...
28 mkLFThunk, mkLFReEntrant, mkConLFInfo, mkSelectorLFInfo,
29 mkApLFInfo, mkLFImported, mkLFArgument, mkLFLetNoEscape,
33 mkClosureInfo, mkConInfo, maybeIsLFCon,
35 closureSize, closureNonHdrSize,
36 closureGoodStuffSize, closurePtrsSize,
39 closureName, infoTableLabelFromCI,
42 closureLFInfo, isLFThunk,closureSMRep, closureUpdReqd,
43 closureNeedsUpdSpace, closureIsThunk,
44 closureSingleEntry, closureReEntrant, isConstrClosure_maybe,
45 closureFunInfo, isStandardFormThunk, isKnownFun,
48 enterIdLabel, enterLocalIdLabel,
51 CallMethod(..), getCallMethod,
58 closureValDescr, closureTypeDescr, -- profiling
61 cafBlackHoleClosureInfo, seCafBlackHoleClosureInfo,
63 staticClosureNeedsLink, clHasCafRefs
66 #include "../includes/MachDeps.h"
68 #define FAST_STRING_NOT_NEEDED
69 #include "HsVersions.h"
71 import ClosureInfo (ArgDescr(..), C_SRT(..), Liveness(..))
72 -- XXX temporary becuase FunInfo needs this one
76 import Cmm ( ClosureTypeInfo(..) )
95 -----------------------------------------------------------------------------
97 -----------------------------------------------------------------------------
99 addIdReps :: [Id] -> [(PrimRep, Id)]
100 addIdReps ids = [(idPrimRep id, id) | id <- ids]
102 addArgReps :: [StgArg] -> [(PrimRep, StgArg)]
103 addArgReps args = [(argPrimRep arg, arg) | arg <- args]
105 argPrimRep :: StgArg -> PrimRep
106 argPrimRep arg = typePrimRep (stgArgType arg)
108 isVoidRep :: PrimRep -> Bool
109 isVoidRep VoidRep = True
110 isVoidRep _other = False
112 isGcPtrRep :: PrimRep -> Bool
113 isGcPtrRep PtrRep = True
117 -----------------------------------------------------------------------------
119 -----------------------------------------------------------------------------
121 -- Information about an identifier, from the code generator's point of
122 -- view. Every identifier is bound to a LambdaFormInfo in the
123 -- environment, which gives the code generator enough info to be able to
124 -- tail call or return that identifier.
127 = LFReEntrant -- Reentrant closure (a function)
128 TopLevelFlag -- True if top level
129 !Int -- Arity. Invariant: always > 0
130 !Bool -- True <=> no fvs
131 ArgDescr -- Argument descriptor (should really be in ClosureInfo)
133 | LFThunk -- Thunk (zero arity)
135 !Bool -- True <=> no free vars
136 !Bool -- True <=> updatable (i.e., *not* single-entry)
138 !Bool -- True <=> *might* be a function type
140 | LFCon -- A saturated constructor application
141 DataCon -- The constructor
143 | LFUnknown -- Used for function arguments and imported things.
144 -- We know nothing about this closure.
145 -- Treat like updatable "LFThunk"...
146 -- Imported things which we *do* know something about use
147 -- one of the other LF constructors (eg LFReEntrant for
149 !Bool -- True <=> *might* be a function type
150 -- The False case is good when we want to enter it,
151 -- because then we know the entry code will do
152 -- For a function, the entry code is the fast entry point
154 | LFUnLifted -- A value of unboxed type;
155 -- always a value, neeeds evaluation
157 | LFLetNoEscape -- See LetNoEscape module for precise description
159 | LFBlackHole -- Used for the closures allocated to hold the result
160 -- of a CAF. We want the target of the update frame to
161 -- be in the heap, so we make a black hole to hold it.
162 CLabel -- Flavour (info label, eg CAF_BLACKHOLE_info).
165 -------------------------
166 -- An ArgDsecr describes the argument pattern of a function
168 {- XXX -- imported from old ClosureInfo for now
170 = ArgSpec -- Fits one of the standard patterns
171 !StgHalfWord -- RTS type identifier ARG_P, ARG_N, ...
173 | ArgGen -- General case
174 Liveness -- Details about the arguments
177 {- XXX -- imported from old ClosureInfo for now
178 -------------------------
179 -- We represent liveness bitmaps as a Bitmap (whose internal
180 -- representation really is a bitmap). These are pinned onto case return
181 -- vectors to indicate the state of the stack for the garbage collector.
183 -- In the compiled program, liveness bitmaps that fit inside a single
184 -- word (StgWord) are stored as a single word, while larger bitmaps are
185 -- stored as a pointer to an array of words.
188 = SmallLiveness -- Liveness info that fits in one word
189 StgWord -- Here's the bitmap
191 | BigLiveness -- Liveness info witha a multi-word bitmap
192 CLabel -- Label for the bitmap
195 -------------------------
196 -- StandardFormInfo tells whether this thunk has one of
197 -- a small number of standard forms
199 data StandardFormInfo
201 -- Not of of the standard forms
204 -- A SelectorThunk is of form
206 -- con a1,..,an -> ak
207 -- and the constructor is from a single-constr type.
208 WordOff -- 0-origin offset of ak within the "goods" of
209 -- constructor (Recall that the a1,...,an may be laid
210 -- out in the heap in a non-obvious order.)
213 -- An ApThunk is of form
215 -- The code for the thunk just pushes x2..xn on the stack and enters x1.
216 -- There are a few of these (for 1 <= n <= MAX_SPEC_AP_SIZE) pre-compiled
217 -- in the RTS to save space.
221 ------------------------------------------------------
222 -- Building LambdaFormInfo
223 ------------------------------------------------------
225 mkLFArgument :: Id -> LambdaFormInfo
227 | isUnLiftedType ty = LFUnLifted
228 | might_be_a_function ty = LFUnknown True
229 | otherwise = LFUnknown False
234 mkLFLetNoEscape :: LambdaFormInfo
235 mkLFLetNoEscape = LFLetNoEscape
238 mkLFReEntrant :: TopLevelFlag -- True of top level
241 -> ArgDescr -- Argument descriptor
244 mkLFReEntrant top fvs args arg_descr
245 = LFReEntrant top (length args) (null fvs) arg_descr
248 mkLFThunk :: Type -> TopLevelFlag -> [Id] -> UpdateFlag -> LambdaFormInfo
249 mkLFThunk thunk_ty top fvs upd_flag
250 = ASSERT( not (isUpdatable upd_flag) || not (isUnLiftedType thunk_ty) )
251 LFThunk top (null fvs)
252 (isUpdatable upd_flag)
254 (might_be_a_function thunk_ty)
257 might_be_a_function :: Type -> Bool
258 -- Return False only if we are *sure* it's a data type
259 -- Look through newtypes etc as much as poss
260 might_be_a_function ty
261 = case splitTyConApp_maybe (repType ty) of
262 Just (tc, _) -> not (isDataTyCon tc)
266 mkConLFInfo :: DataCon -> LambdaFormInfo
267 mkConLFInfo con = LFCon con
270 mkSelectorLFInfo :: Id -> Int -> Bool -> LambdaFormInfo
271 mkSelectorLFInfo id offset updatable
272 = LFThunk NotTopLevel False updatable (SelectorThunk offset)
273 (might_be_a_function (idType id))
276 mkApLFInfo :: Id -> UpdateFlag -> Arity -> LambdaFormInfo
277 mkApLFInfo id upd_flag arity
278 = LFThunk NotTopLevel (arity == 0) (isUpdatable upd_flag) (ApThunk arity)
279 (might_be_a_function (idType id))
282 mkLFImported :: Id -> LambdaFormInfo
284 | Just con <- isDataConWorkId_maybe id
285 , isNullaryRepDataCon con
286 = LFCon con -- An imported nullary constructor
287 -- We assume that the constructor is evaluated so that
288 -- the id really does point directly to the constructor
291 = LFReEntrant TopLevel arity True (panic "arg_descr")
294 = mkLFArgument id -- Not sure of exact arity
298 -----------------------------------------------------
299 -- Dynamic pointer tagging
300 -----------------------------------------------------
302 type ConTagZ = Int -- A *zero-indexed* contructor tag
304 type DynTag = Int -- The tag on a *pointer*
305 -- (from the dynamic-tagging paper)
307 {- Note [Data constructor dynamic tags]
308 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
309 The family size of a data type (the number of constructors)
311 * small, if the family size < 2**tag_bits
314 Small families can have the constructor tag in the tag bits.
315 Big families only use the tag value 1 to represent evaluatedness. -}
317 isSmallFamily :: Int -> Bool
318 isSmallFamily fam_size = fam_size <= mAX_PTR_TAG
320 -- We keep the *zero-indexed* tag in the srt_len field of the info
321 -- table of a data constructor.
322 dataConTagZ :: DataCon -> ConTagZ
323 dataConTagZ con = dataConTag con - fIRST_TAG
325 tagForCon :: DataCon -> DynTag
327 | isSmallFamily fam_size = con_tag + 1
330 con_tag = dataConTagZ con
331 fam_size = tyConFamilySize (dataConTyCon con)
333 tagForArity :: Int -> DynTag
334 tagForArity arity | isSmallFamily arity = arity
337 lfDynTag :: LambdaFormInfo -> DynTag
338 lfDynTag (LFCon con) = tagForCon con
339 lfDynTag (LFReEntrant _ arity _ _) = tagForArity arity
343 -----------------------------------------------------------------------------
344 -- Observing LambdaFormInfo
345 -----------------------------------------------------------------------------
348 maybeIsLFCon :: LambdaFormInfo -> Maybe DataCon
349 maybeIsLFCon (LFCon con) = Just con
350 maybeIsLFCon _ = Nothing
353 isLFThunk :: LambdaFormInfo -> Bool
354 isLFThunk (LFThunk _ _ _ _ _) = True
355 isLFThunk (LFBlackHole _) = True
356 -- return True for a blackhole: this function is used to determine
357 -- whether to use the thunk header in SMP mode, and a blackhole
362 -----------------------------------------------------------------------------
364 -----------------------------------------------------------------------------
367 :: Bool -- True <=> static closure
369 -> WordOff -> WordOff -- Tot wds, ptr wds
372 chooseSMRep is_static lf_info tot_wds ptr_wds
374 nonptr_wds = tot_wds - ptr_wds
375 closure_type = getClosureType is_static ptr_wds lf_info
377 GenericRep is_static ptr_wds nonptr_wds closure_type
379 -- We *do* get non-updatable top-level thunks sometimes. eg. f = g
380 -- gets compiled to a jump to g (if g has non-zero arity), instead of
381 -- messing around with update frames and PAPs. We set the closure type
382 -- to FUN_STATIC in this case.
384 getClosureType :: Bool -> WordOff -> LambdaFormInfo -> ClosureType
385 getClosureType is_static ptr_wds lf_info
387 LFCon {} | is_static && ptr_wds == 0 -> ConstrNoCaf
388 | otherwise -> Constr
389 LFReEntrant {} -> Fun
390 LFThunk _ _ _ (SelectorThunk {}) _ -> ThunkSelector
392 _ -> panic "getClosureType"
395 -----------------------------------------------------------------------------
397 -----------------------------------------------------------------------------
399 -- Be sure to see the stg-details notes about these...
401 nodeMustPointToIt :: LambdaFormInfo -> Bool
402 nodeMustPointToIt (LFReEntrant top _ no_fvs _)
403 = not no_fvs || -- Certainly if it has fvs we need to point to it
405 -- If it is not top level we will point to it
406 -- We can have a \r closure with no_fvs which
407 -- is not top level as special case cgRhsClosure
408 -- has been dissabled in favour of let floating
410 -- For lex_profiling we also access the cost centre for a
411 -- non-inherited function i.e. not top level
412 -- the not top case above ensures this is ok.
414 nodeMustPointToIt (LFCon _) = True
416 -- Strictly speaking, the above two don't need Node to point
417 -- to it if the arity = 0. But this is a *really* unlikely
418 -- situation. If we know it's nil (say) and we are entering
419 -- it. Eg: let x = [] in x then we will certainly have inlined
420 -- x, since nil is a simple atom. So we gain little by not
421 -- having Node point to known zero-arity things. On the other
422 -- hand, we do lose something; Patrick's code for figuring out
423 -- when something has been updated but not entered relies on
424 -- having Node point to the result of an update. SLPJ
427 nodeMustPointToIt (LFThunk _ no_fvs updatable NonStandardThunk _)
428 = updatable || not no_fvs || opt_SccProfilingOn
429 -- For the non-updatable (single-entry case):
431 -- True if has fvs (in which case we need access to them, and we
432 -- should black-hole it)
433 -- or profiling (in which case we need to recover the cost centre
436 nodeMustPointToIt (LFThunk {}) -- Node must point to a standard-form thunk
439 nodeMustPointToIt (LFUnknown _) = True
440 nodeMustPointToIt LFUnLifted = False
441 nodeMustPointToIt (LFBlackHole _) = True -- BH entry may require Node to point
442 nodeMustPointToIt LFLetNoEscape = False
444 -----------------------------------------------------------------------------
446 -----------------------------------------------------------------------------
448 {- The entry conventions depend on the type of closure being entered,
449 whether or not it has free variables, and whether we're running
450 sequentially or in parallel.
452 Closure Node Argument Enter
453 Characteristics Par Req'd Passing Via
454 -------------------------------------------------------------------------------
455 Unknown & no & yes & stack & node
456 Known fun (>1 arg), no fvs & no & no & registers & fast entry (enough args)
457 & slow entry (otherwise)
458 Known fun (>1 arg), fvs & no & yes & registers & fast entry (enough args)
459 0 arg, no fvs \r,\s & no & no & n/a & direct entry
460 0 arg, no fvs \u & no & yes & n/a & node
461 0 arg, fvs \r,\s & no & yes & n/a & direct entry
462 0 arg, fvs \u & no & yes & n/a & node
464 Unknown & yes & yes & stack & node
465 Known fun (>1 arg), no fvs & yes & no & registers & fast entry (enough args)
466 & slow entry (otherwise)
467 Known fun (>1 arg), fvs & yes & yes & registers & node
468 0 arg, no fvs \r,\s & yes & no & n/a & direct entry
469 0 arg, no fvs \u & yes & yes & n/a & node
470 0 arg, fvs \r,\s & yes & yes & n/a & node
471 0 arg, fvs \u & yes & yes & n/a & node
474 When black-holing, single-entry closures could also be entered via node
475 (rather than directly) to catch double-entry. -}
478 = EnterIt -- No args, not a function
480 | JumpToIt -- A join point
482 | ReturnIt -- It's a value (function, unboxed value,
483 -- or constructor), so just return it.
485 | SlowCall -- Unknown fun, or known fun with
488 | DirectEntry -- Jump directly, with args in regs
489 CLabel -- The code label
492 getCallMethod :: Name -- Function being applied
493 -> CafInfo -- Can it refer to CAF's?
494 -> LambdaFormInfo -- Its info
495 -> Int -- Number of available arguments
498 getCallMethod _name _ lf_info _n_args
499 | nodeMustPointToIt lf_info && opt_Parallel
500 = -- If we're parallel, then we must always enter via node.
501 -- The reason is that the closure may have been
502 -- fetched since we allocated it.
505 getCallMethod name caf (LFReEntrant _ arity _ _) n_args
506 | n_args == 0 = ASSERT( arity /= 0 )
507 ReturnIt -- No args at all
508 | n_args < arity = SlowCall -- Not enough args
509 | otherwise = DirectEntry (enterIdLabel name caf) arity
511 getCallMethod _name _ LFUnLifted n_args
512 = ASSERT( n_args == 0 ) ReturnIt
514 getCallMethod _name _ (LFCon _) n_args
515 = ASSERT( n_args == 0 ) ReturnIt
517 getCallMethod name caf (LFThunk _ _ updatable std_form_info is_fun) n_args
518 | is_fun -- it *might* be a function, so we must "call" it (which is always safe)
519 = SlowCall -- We cannot just enter it [in eval/apply, the entry code
520 -- is the fast-entry code]
522 -- Since is_fun is False, we are *definitely* looking at a data value
523 | updatable || opt_DoTickyProfiling -- to catch double entry
525 I decided to remove this, because in SMP mode it doesn't matter
526 if we enter the same thunk multiple times, so the optimisation
527 of jumping directly to the entry code is still valid. --SDM
530 -- We used to have ASSERT( n_args == 0 ), but actually it is
531 -- possible for the optimiser to generate
532 -- let bot :: Int = error Int "urk"
533 -- in (bot `cast` unsafeCoerce Int (Int -> Int)) 3
534 -- This happens as a result of the case-of-error transformation
535 -- So the right thing to do is just to enter the thing
537 | otherwise -- Jump direct to code for single-entry thunks
538 = ASSERT( n_args == 0 )
539 DirectEntry (thunkEntryLabel name caf std_form_info updatable) 0
541 getCallMethod _name _ (LFUnknown True) _n_args
542 = SlowCall -- might be a function
544 getCallMethod name _ (LFUnknown False) n_args
545 = ASSERT2 ( n_args == 0, ppr name <+> ppr n_args )
546 EnterIt -- Not a function
548 getCallMethod _name _ (LFBlackHole _) _n_args
549 = SlowCall -- Presumably the black hole has by now
550 -- been updated, but we don't know with
551 -- what, so we slow call it
553 getCallMethod _name _ LFLetNoEscape _n_args
556 isStandardFormThunk :: LambdaFormInfo -> Bool
557 isStandardFormThunk (LFThunk _ _ _ (SelectorThunk _) _) = True
558 isStandardFormThunk (LFThunk _ _ _ (ApThunk _) _) = True
559 isStandardFormThunk _other_lf_info = False
561 isKnownFun :: LambdaFormInfo -> Bool
562 isKnownFun (LFReEntrant _ _ _ _) = True
563 isKnownFun LFLetNoEscape = True
566 -----------------------------------------------------------------------------
567 -- staticClosureRequired
568 -----------------------------------------------------------------------------
570 {- staticClosureRequired is never called (hence commented out)
572 SimonMar writes (Sept 07) It's an optimisation we used to apply at
573 one time, I believe, but it got lost probably in the rewrite of
574 the RTS/code generator. I left that code there to remind me to
575 look into whether it was worth doing sometime
577 {- Avoiding generating entries and info tables
578 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
579 At present, for every function we generate all of the following,
580 just in case. But they aren't always all needed, as noted below:
582 [NB1: all of this applies only to *functions*. Thunks always
583 have closure, info table, and entry code.]
585 [NB2: All are needed if the function is *exported*, just to play safe.]
587 * Fast-entry code ALWAYS NEEDED
590 Needed iff (a) we have any un-saturated calls to the function
591 OR (b) the function is passed as an arg
592 OR (c) we're in the parallel world and the function has free vars
593 [Reason: in parallel world, we always enter functions
594 with free vars via the closure.]
596 * The function closure
597 Needed iff (a) we have any un-saturated calls to the function
598 OR (b) the function is passed as an arg
599 OR (c) if the function has free vars (ie not top level)
601 Why case (a) here? Because if the arg-satis check fails,
602 UpdatePAP stuffs a pointer to the function closure in the PAP.
603 [Could be changed; UpdatePAP could stuff in a code ptr instead,
604 but doesn't seem worth it.]
606 [NB: these conditions imply that we might need the closure
607 without the slow-entry code. Here's how.
609 f x y = let g w = ...x..y..w...
613 Here we need a closure for g which contains x and y,
614 but since the calls are all saturated we just jump to the
615 fast entry point for g, with R1 pointing to the closure for g.]
618 * Standard info table
619 Needed iff (a) we have any un-saturated calls to the function
620 OR (b) the function is passed as an arg
621 OR (c) the function has free vars (ie not top level)
623 NB. In the sequential world, (c) is only required so that the function closure has
624 an info table to point to, to keep the storage manager happy.
625 If (c) alone is true we could fake up an info table by choosing
626 one of a standard family of info tables, whose entry code just
629 [NB In the parallel world (c) is needed regardless because
630 we enter functions with free vars via the closure.]
632 If (c) is retained, then we'll sometimes generate an info table
633 (for storage mgr purposes) without slow-entry code. Then we need
634 to use an error label in the info table to substitute for the absent
638 staticClosureRequired
643 staticClosureRequired binder bndr_info
644 (LFReEntrant top_level _ _ _) -- It's a function
645 = ASSERT( isTopLevel top_level )
646 -- Assumption: it's a top-level, no-free-var binding
647 not (satCallsOnly bndr_info)
649 staticClosureRequired binder other_binder_info other_lf_info = True
652 -----------------------------------------------------------------------------
653 -- Data types for closure information}
654 -----------------------------------------------------------------------------
657 {- Information about a closure, from the code generator's point of view.
659 A ClosureInfo decribes the info pointer of a closure. It has
661 a) to construct the info table itself
662 b) to allocate a closure containing that info pointer (i.e.
663 it knows the info table label)
665 We make a ClosureInfo for
666 - each let binding (both top level and not)
667 - each data constructor (for its shared static and
673 closureName :: !Name, -- The thing bound to this closure
674 closureLFInfo :: !LambdaFormInfo, -- NOTE: not an LFCon (see below)
675 closureSMRep :: !SMRep, -- representation used by storage mgr
676 closureSRT :: !C_SRT, -- What SRT applies to this closure
677 closureType :: !Type, -- Type of closure (ToDo: remove)
678 closureDescr :: !String -- closure description (for profiling)
681 -- Constructor closures don't have a unique info table label (they use
682 -- the constructor's info table), and they don't have an SRT.
684 closureCon :: !DataCon,
685 closureSMRep :: !SMRep
688 {- XXX temp imported from old ClosureInfo
689 -- C_SRT is what StgSyn.SRT gets translated to...
690 -- we add a label for the table, and expect only the 'offset/length' form
693 | C_SRT !CLabel !WordOff !StgHalfWord {-bitmap or escape-}
696 instance Outputable C_SRT where
697 ppr (NoC_SRT) = ptext SLIT("_no_srt_")
698 ppr (C_SRT label off bitmap) = parens (ppr label <> comma <> ppr off <> comma <> text (show bitmap))
701 needsSRT :: C_SRT -> Bool
702 needsSRT NoC_SRT = False
703 needsSRT (C_SRT _ _ _) = True
706 --------------------------------------
707 -- Building ClosureInfos
708 --------------------------------------
710 mkClosureInfo :: Bool -- Is static
713 -> Int -> Int -- Total and pointer words
715 -> String -- String descriptor
717 mkClosureInfo is_static id lf_info tot_wds ptr_wds srt_info descr
718 = ClosureInfo { closureName = name,
719 closureLFInfo = lf_info,
720 closureSMRep = sm_rep,
721 closureSRT = srt_info,
722 closureType = idType id,
723 closureDescr = descr }
726 sm_rep = chooseSMRep is_static lf_info tot_wds ptr_wds
728 mkConInfo :: Bool -- Is static
730 -> Int -> Int -- Total and pointer words
732 mkConInfo is_static data_con tot_wds ptr_wds
733 = ConInfo { closureSMRep = sm_rep,
734 closureCon = data_con }
736 sm_rep = chooseSMRep is_static (mkConLFInfo data_con) tot_wds ptr_wds
739 -- We need a black-hole closure info to pass to @allocDynClosure@ when we
740 -- want to allocate the black hole on entry to a CAF. These are the only
741 -- ways to build an LFBlackHole, maintaining the invariant that it really
742 -- is a black hole and not something else.
744 cafBlackHoleClosureInfo :: ClosureInfo -> ClosureInfo
745 cafBlackHoleClosureInfo (ClosureInfo { closureName = nm,
747 = ClosureInfo { closureName = nm,
748 closureLFInfo = LFBlackHole mkCAFBlackHoleInfoTableLabel,
749 closureSMRep = BlackHoleRep,
750 closureSRT = NoC_SRT,
753 cafBlackHoleClosureInfo _ = panic "cafBlackHoleClosureInfo"
755 seCafBlackHoleClosureInfo :: ClosureInfo -> ClosureInfo
756 seCafBlackHoleClosureInfo (ClosureInfo { closureName = nm,
758 = ClosureInfo { closureName = nm,
759 closureLFInfo = LFBlackHole mkSECAFBlackHoleInfoTableLabel,
760 closureSMRep = BlackHoleRep,
761 closureSRT = NoC_SRT,
764 seCafBlackHoleClosureInfo _ = panic "seCafBlackHoleClosureInfo"
766 --------------------------------------
767 -- Extracting ClosureTypeInfo
768 --------------------------------------
770 closureTypeInfo :: ClosureInfo -> ClosureTypeInfo
771 closureTypeInfo cl_info
773 ConInfo { closureCon = con }
774 -> ConstrInfo (ptrs, nptrs)
775 (fromIntegral (dataConTagZ con))
778 con_name = panic "closureTypeInfo"
780 -- cstr <- mkByteStringCLit $ dataConIdentity con
781 -- con_name = makeRelativeRefTo info_lbl cstr
783 ClosureInfo { closureName = name,
784 closureLFInfo = LFReEntrant _ arity _ arg_descr,
786 -> FunInfo (ptrs, nptrs)
790 (CmmLabel (mkSlowEntryLabel name (clHasCafRefs cl_info)))
792 ClosureInfo { closureLFInfo = LFThunk _ _ _ (SelectorThunk offset) _,
794 -> ThunkSelectorInfo (fromIntegral offset) srt
796 ClosureInfo { closureLFInfo = LFThunk {},
798 -> ThunkInfo (ptrs, nptrs) srt
800 _ -> panic "unexpected lambda form in mkCmmInfo"
802 -- info_lbl = infoTableLabelFromCI cl_info
803 ptrs = fromIntegral $ closurePtrsSize cl_info
804 size = fromIntegral $ closureNonHdrSize cl_info
807 --------------------------------------
808 -- Functions about closure *sizes*
809 --------------------------------------
811 closureSize :: ClosureInfo -> WordOff
812 closureSize cl_info = hdr_size + closureNonHdrSize cl_info
813 where hdr_size | closureIsThunk cl_info = thunkHdrSize
814 | otherwise = fixedHdrSize
815 -- All thunks use thunkHdrSize, even if they are non-updatable.
816 -- this is because we don't have separate closure types for
817 -- updatable vs. non-updatable thunks, so the GC can't tell the
818 -- difference. If we ever have significant numbers of non-
819 -- updatable thunks, it might be worth fixing this.
821 closureNonHdrSize :: ClosureInfo -> WordOff
822 closureNonHdrSize cl_info
823 = tot_wds + computeSlopSize tot_wds cl_info
825 tot_wds = closureGoodStuffSize cl_info
827 closureGoodStuffSize :: ClosureInfo -> WordOff
828 closureGoodStuffSize cl_info
829 = let (ptrs, nonptrs) = sizes_from_SMRep (closureSMRep cl_info)
832 closurePtrsSize :: ClosureInfo -> WordOff
833 closurePtrsSize cl_info
834 = let (ptrs, _) = sizes_from_SMRep (closureSMRep cl_info)
838 sizes_from_SMRep :: SMRep -> (WordOff,WordOff)
839 sizes_from_SMRep (GenericRep _ ptrs nonptrs _) = (ptrs, nonptrs)
840 sizes_from_SMRep BlackHoleRep = (0, 0)
842 -- Computing slop size. WARNING: this looks dodgy --- it has deep
843 -- knowledge of what the storage manager does with the various
844 -- representations...
846 -- Slop Requirements: every thunk gets an extra padding word in the
847 -- header, which takes the the updated value.
849 slopSize :: ClosureInfo -> WordOff
850 slopSize cl_info = computeSlopSize payload_size cl_info
851 where payload_size = closureGoodStuffSize cl_info
853 computeSlopSize :: WordOff -> ClosureInfo -> WordOff
854 computeSlopSize payload_size cl_info
855 = max 0 (minPayloadSize smrep updatable - payload_size)
857 smrep = closureSMRep cl_info
858 updatable = closureNeedsUpdSpace cl_info
860 closureNeedsUpdSpace :: ClosureInfo -> Bool
861 -- We leave space for an update if either (a) the closure is updatable
862 -- or (b) it is a static thunk. This is because a static thunk needs
863 -- a static link field in a predictable place (after the slop), regardless
864 -- of whether it is updatable or not.
865 closureNeedsUpdSpace (ClosureInfo { closureLFInfo =
866 LFThunk TopLevel _ _ _ _ }) = True
867 closureNeedsUpdSpace cl_info = closureUpdReqd cl_info
869 minPayloadSize :: SMRep -> Bool -> WordOff
870 minPayloadSize smrep updatable
872 BlackHoleRep -> min_upd_size
873 GenericRep _ _ _ _ | updatable -> min_upd_size
874 GenericRep True _ _ _ -> 0 -- static
875 GenericRep False _ _ _ -> mIN_PAYLOAD_SIZE
879 ASSERT(mIN_PAYLOAD_SIZE <= sIZEOF_StgSMPThunkHeader)
880 0 -- check that we already have enough
881 -- room for mIN_SIZE_NonUpdHeapObject,
882 -- due to the extra header word in SMP
884 --------------------------------------
885 -- Other functions over ClosureInfo
886 --------------------------------------
888 blackHoleOnEntry :: ClosureInfo -> Bool
889 -- Static closures are never themselves black-holed.
890 -- Updatable ones will be overwritten with a CAFList cell, which points to a
892 -- Single-entry ones have no fvs to plug, and we trust they don't form part
895 blackHoleOnEntry ConInfo{} = False
896 blackHoleOnEntry (ClosureInfo { closureLFInfo = lf_info, closureSMRep = rep })
898 = False -- Never black-hole a static closure
902 LFReEntrant _ _ _ _ -> False
903 LFLetNoEscape -> False
904 LFThunk _ no_fvs updatable _ _
906 then not opt_OmitBlackHoling
907 else opt_DoTickyProfiling || not no_fvs
908 -- the former to catch double entry,
909 -- and the latter to plug space-leaks. KSW/SDM 1999-04.
911 _other -> panic "blackHoleOnEntry" -- Should never happen
914 staticClosureNeedsLink :: ClosureInfo -> Bool
915 -- A static closure needs a link field to aid the GC when traversing
916 -- the static closure graph. But it only needs such a field if either
918 -- b) it's a constructor with one or more pointer fields
919 -- In case (b), the constructor's fields themselves play the role
921 staticClosureNeedsLink (ClosureInfo { closureSRT = srt })
923 staticClosureNeedsLink (ConInfo { closureSMRep = sm_rep, closureCon = con })
924 = not (isNullaryRepDataCon con) && not_nocaf_constr
928 GenericRep _ _ _ ConstrNoCaf -> False
931 isStaticClosure :: ClosureInfo -> Bool
932 isStaticClosure cl_info = isStaticRep (closureSMRep cl_info)
934 closureUpdReqd :: ClosureInfo -> Bool
935 closureUpdReqd ClosureInfo{ closureLFInfo = lf_info } = lfUpdatable lf_info
936 closureUpdReqd ConInfo{} = False
938 lfUpdatable :: LambdaFormInfo -> Bool
939 lfUpdatable (LFThunk _ _ upd _ _) = upd
940 lfUpdatable (LFBlackHole _) = True
941 -- Black-hole closures are allocated to receive the results of an
942 -- alg case with a named default... so they need to be updated.
943 lfUpdatable _ = False
945 closureIsThunk :: ClosureInfo -> Bool
946 closureIsThunk ClosureInfo{ closureLFInfo = lf_info } = isLFThunk lf_info
947 closureIsThunk ConInfo{} = False
949 closureSingleEntry :: ClosureInfo -> Bool
950 closureSingleEntry (ClosureInfo { closureLFInfo = LFThunk _ _ upd _ _}) = not upd
951 closureSingleEntry _ = False
953 closureReEntrant :: ClosureInfo -> Bool
954 closureReEntrant (ClosureInfo { closureLFInfo = LFReEntrant _ _ _ _ }) = True
955 closureReEntrant _ = False
957 isConstrClosure_maybe :: ClosureInfo -> Maybe DataCon
958 isConstrClosure_maybe (ConInfo { closureCon = data_con }) = Just data_con
959 isConstrClosure_maybe _ = Nothing
961 closureFunInfo :: ClosureInfo -> Maybe (Int, ArgDescr)
962 closureFunInfo (ClosureInfo { closureLFInfo = lf_info }) = lfFunInfo lf_info
963 closureFunInfo _ = Nothing
965 lfFunInfo :: LambdaFormInfo -> Maybe (Int, ArgDescr)
966 lfFunInfo (LFReEntrant _ arity _ arg_desc) = Just (arity, arg_desc)
967 lfFunInfo _ = Nothing
969 funTag :: ClosureInfo -> DynTag
970 funTag (ClosureInfo { closureLFInfo = lf_info }) = lfDynTag lf_info
971 funTag (ConInfo {}) = panic "funTag"
973 isToplevClosure :: ClosureInfo -> Bool
974 isToplevClosure (ClosureInfo { closureLFInfo = lf_info })
976 LFReEntrant TopLevel _ _ _ -> True
977 LFThunk TopLevel _ _ _ _ -> True
979 isToplevClosure _ = False
981 --------------------------------------
983 --------------------------------------
985 infoTableLabelFromCI :: ClosureInfo -> CLabel
986 infoTableLabelFromCI cl@(ClosureInfo { closureName = name,
987 closureLFInfo = lf_info })
989 LFBlackHole info -> info
991 LFThunk _ _ upd_flag (SelectorThunk offset) _ ->
992 mkSelectorInfoLabel upd_flag offset
994 LFThunk _ _ upd_flag (ApThunk arity) _ ->
995 mkApInfoTableLabel upd_flag arity
997 LFThunk{} -> mkLocalInfoTableLabel name $ clHasCafRefs cl
999 LFReEntrant _ _ _ _ -> mkLocalInfoTableLabel name $ clHasCafRefs cl
1001 _other -> panic "infoTableLabelFromCI"
1003 infoTableLabelFromCI cl@(ConInfo { closureCon = con, closureSMRep = rep })
1004 | isStaticRep rep = mkStaticInfoTableLabel name $ clHasCafRefs cl
1005 | otherwise = mkConInfoTableLabel name $ clHasCafRefs cl
1007 name = dataConName con
1009 -- ClosureInfo for a closure (as opposed to a constructor) is always local
1010 closureLabelFromCI :: ClosureInfo -> CLabel
1011 closureLabelFromCI cl@(ClosureInfo { closureName = nm }) =
1012 mkLocalClosureLabel nm $ clHasCafRefs cl
1013 closureLabelFromCI _ = panic "closureLabelFromCI"
1015 thunkEntryLabel :: Name -> CafInfo -> StandardFormInfo -> Bool -> CLabel
1016 -- thunkEntryLabel is a local help function, not exported. It's used from both
1017 -- entryLabelFromCI and getCallMethod.
1018 thunkEntryLabel _thunk_id _ (ApThunk arity) upd_flag
1019 = enterApLabel upd_flag arity
1020 thunkEntryLabel _thunk_id _ (SelectorThunk offset) upd_flag
1021 = enterSelectorLabel upd_flag offset
1022 thunkEntryLabel thunk_id c _ _
1023 = enterIdLabel thunk_id c
1025 enterApLabel :: Bool -> Arity -> CLabel
1026 enterApLabel is_updatable arity
1027 | tablesNextToCode = mkApInfoTableLabel is_updatable arity
1028 | otherwise = mkApEntryLabel is_updatable arity
1030 enterSelectorLabel :: Bool -> WordOff -> CLabel
1031 enterSelectorLabel upd_flag offset
1032 | tablesNextToCode = mkSelectorInfoLabel upd_flag offset
1033 | otherwise = mkSelectorEntryLabel upd_flag offset
1035 enterIdLabel :: Name -> CafInfo -> CLabel
1037 | tablesNextToCode = mkInfoTableLabel id c
1038 | otherwise = mkEntryLabel id c
1040 enterLocalIdLabel :: Name -> CafInfo -> CLabel
1041 enterLocalIdLabel id c
1042 | tablesNextToCode = mkLocalInfoTableLabel id c
1043 | otherwise = mkLocalEntryLabel id c
1046 --------------------------------------
1048 --------------------------------------
1050 -- Profiling requires two pieces of information to be determined for
1051 -- each closure's info table --- description and type.
1053 -- The description is stored directly in the @CClosureInfoTable@ when the
1054 -- info table is built.
1056 -- The type is determined from the type information stored with the @Id@
1057 -- in the closure info using @closureTypeDescr@.
1059 closureValDescr, closureTypeDescr :: ClosureInfo -> String
1060 closureValDescr (ClosureInfo {closureDescr = descr})
1062 closureValDescr (ConInfo {closureCon = con})
1063 = occNameString (getOccName con)
1065 closureTypeDescr (ClosureInfo { closureType = ty })
1066 = getTyDescription ty
1067 closureTypeDescr (ConInfo { closureCon = data_con })
1068 = occNameString (getOccName (dataConTyCon data_con))
1070 getTyDescription :: Type -> String
1072 = case (tcSplitSigmaTy ty) of { (_, _, tau_ty) ->
1075 AppTy fun _ -> getTyDescription fun
1076 FunTy _ res -> '-' : '>' : fun_result res
1077 TyConApp tycon _ -> getOccString tycon
1078 PredTy sty -> getPredTyDescription sty
1079 ForAllTy _ ty -> getTyDescription ty
1082 fun_result (FunTy _ res) = '>' : fun_result res
1083 fun_result other = getTyDescription other
1085 getPredTyDescription :: PredType -> String
1086 getPredTyDescription (ClassP cl _) = getOccString cl
1087 getPredTyDescription (IParam ip _) = getOccString (ipNameName ip)
1088 getPredTyDescription (EqPred ty1 _ty2) = getTyDescription ty1 -- Urk?
1091 --------------------------------------
1093 --------------------------------------
1095 -- This is horrible, but we need to know whether a closure may have CAFs.
1096 clHasCafRefs :: ClosureInfo -> CafInfo
1097 clHasCafRefs (ClosureInfo {closureSRT = srt}) =
1098 case srt of NoC_SRT -> NoCafRefs
1100 clHasCafRefs (ConInfo {}) = NoCafRefs