b7cca48f5a42bb532210ea9a81de6e6dc30bbe19
[ghc.git] / compiler / codeGen / StgCmmHeap.hs
1 -----------------------------------------------------------------------------
2 --
3 -- Stg to C--: heap management functions
4 --
5 -- (c) The University of Glasgow 2004-2006
6 --
7 -----------------------------------------------------------------------------
8
9 module StgCmmHeap (
10 getVirtHp, setVirtHp, setRealHp,
11 getHpRelOffset, hpRel,
12
13 entryHeapCheck, altHeapCheck, noEscapeHeapCheck, altHeapCheckReturnsTo,
14
15 mkVirtHeapOffsets, mkVirtConstrOffsets,
16 mkStaticClosureFields, mkStaticClosure,
17
18 allocDynClosure, allocDynClosureCmm,
19 emitSetDynHdr
20 ) where
21
22 #include "HsVersions.h"
23
24 import StgSyn
25 import CLabel
26 import StgCmmLayout
27 import StgCmmUtils
28 import StgCmmMonad
29 import StgCmmProf
30 import StgCmmTicky
31 import StgCmmGran
32 import StgCmmClosure
33 import StgCmmEnv
34
35 import MkGraph
36
37 import Hoopl
38 import SMRep
39 import Cmm
40 import CmmUtils
41 import CostCentre
42 import Outputable
43 import IdInfo( CafInfo(..), mayHaveCafRefs )
44 import Module
45 import DynFlags
46 import FastString( mkFastString, fsLit )
47 import Util
48
49 import Control.Monad (when)
50
51 -----------------------------------------------------------
52 -- Initialise dynamic heap objects
53 -----------------------------------------------------------
54
55 allocDynClosure
56 :: CmmInfoTable
57 -> LambdaFormInfo
58 -> CmmExpr -- Cost Centre to stick in the object
59 -> CmmExpr -- Cost Centre to blame for this alloc
60 -- (usually the same; sometimes "OVERHEAD")
61
62 -> [(NonVoid StgArg, VirtualHpOffset)] -- Offsets from start of object
63 -- ie Info ptr has offset zero.
64 -- No void args in here
65 -> FCode CmmExpr -- returns Hp+n
66
67 allocDynClosureCmm
68 :: CmmInfoTable -> LambdaFormInfo -> CmmExpr -> CmmExpr
69 -> [(CmmExpr, VirtualHpOffset)]
70 -> FCode CmmExpr -- returns Hp+n
71
72 -- allocDynClosure allocates the thing in the heap,
73 -- and modifies the virtual Hp to account for this.
74 -- The second return value is the graph that sets the value of the
75 -- returned LocalReg, which should point to the closure after executing
76 -- the graph.
77
78 -- allocDynClosure returns an (Hp+8) CmmExpr, and hence the result is
79 -- only valid until Hp is changed. The caller should assign the
80 -- result to a LocalReg if it is required to remain live.
81 --
82 -- The reason we don't assign it to a LocalReg here is that the caller
83 -- is often about to call regIdInfo, which immediately assigns the
84 -- result of allocDynClosure to a new temp in order to add the tag.
85 -- So by not generating a LocalReg here we avoid a common source of
86 -- new temporaries and save some compile time. This can be quite
87 -- significant - see test T4801.
88
89
90 allocDynClosure info_tbl lf_info use_cc _blame_cc args_w_offsets
91 = do { let (args, offsets) = unzip args_w_offsets
92 ; cmm_args <- mapM getArgAmode args -- No void args
93 ; allocDynClosureCmm info_tbl lf_info
94 use_cc _blame_cc (zip cmm_args offsets)
95 }
96
97 allocDynClosureCmm info_tbl lf_info use_cc _blame_cc amodes_w_offsets
98 = do { virt_hp <- getVirtHp
99
100 -- SAY WHAT WE ARE ABOUT TO DO
101 ; let rep = cit_rep info_tbl
102 ; tickyDynAlloc rep lf_info
103 ; profDynAlloc rep use_cc
104
105 -- FIND THE OFFSET OF THE INFO-PTR WORD
106 ; let info_offset = virt_hp + 1
107 -- info_offset is the VirtualHpOffset of the first
108 -- word of the new object
109 -- Remember, virtHp points to last allocated word,
110 -- ie 1 *before* the info-ptr word of new object.
111
112 info_ptr = CmmLit (CmmLabel (cit_lbl info_tbl))
113
114 -- ALLOCATE THE OBJECT
115 ; base <- getHpRelOffset info_offset
116 ; emitComment $ mkFastString "allocDynClosure"
117 ; emitSetDynHdr base info_ptr use_cc
118 ; let (cmm_args, offsets) = unzip amodes_w_offsets
119 ; hpStore base cmm_args offsets
120
121 -- BUMP THE VIRTUAL HEAP POINTER
122 ; dflags <- getDynFlags
123 ; setVirtHp (virt_hp + heapClosureSize dflags rep)
124
125 ; getHpRelOffset info_offset
126 }
127
128 emitSetDynHdr :: CmmExpr -> CmmExpr -> CmmExpr -> FCode ()
129 emitSetDynHdr base info_ptr ccs
130 = do dflags <- getDynFlags
131 hpStore base (header dflags) [0..]
132 where
133 header :: DynFlags -> [CmmExpr]
134 header dflags = [info_ptr] ++ dynProfHdr dflags ccs
135 -- ToDo: Gransim stuff
136 -- ToDo: Parallel stuff
137 -- No ticky header
138
139 hpStore :: CmmExpr -> [CmmExpr] -> [VirtualHpOffset] -> FCode ()
140 -- Store the item (expr,off) in base[off]
141 hpStore base vals offs
142 = do dflags <- getDynFlags
143 let mk_store val off = mkStore (cmmOffsetW dflags base off) val
144 emit (catAGraphs (zipWith mk_store vals offs))
145
146
147 -----------------------------------------------------------
148 -- Layout of static closures
149 -----------------------------------------------------------
150
151 -- Make a static closure, adding on any extra padding needed for CAFs,
152 -- and adding a static link field if necessary.
153
154 mkStaticClosureFields
155 :: DynFlags
156 -> CmmInfoTable
157 -> CostCentreStack
158 -> CafInfo
159 -> [CmmLit] -- Payload
160 -> [CmmLit] -- The full closure
161 mkStaticClosureFields dflags info_tbl ccs caf_refs payload
162 = mkStaticClosure dflags info_lbl ccs payload padding
163 static_link_field saved_info_field
164 where
165 info_lbl = cit_lbl info_tbl
166
167 -- CAFs must have consistent layout, regardless of whether they
168 -- are actually updatable or not. The layout of a CAF is:
169 --
170 -- 3 saved_info
171 -- 2 static_link
172 -- 1 indirectee
173 -- 0 info ptr
174 --
175 -- the static_link and saved_info fields must always be in the
176 -- same place. So we use isThunkRep rather than closureUpdReqd
177 -- here:
178
179 is_caf = isThunkRep (cit_rep info_tbl)
180
181 padding
182 | not is_caf = []
183 | otherwise = ASSERT(null payload) [mkIntCLit dflags 0]
184
185 static_link_field
186 | is_caf || staticClosureNeedsLink (mayHaveCafRefs caf_refs) info_tbl
187 = [static_link_value]
188 | otherwise
189 = []
190
191 saved_info_field
192 | is_caf = [mkIntCLit dflags 0]
193 | otherwise = []
194
195 -- For a static constructor which has NoCafRefs, we set the
196 -- static link field to a non-zero value so the garbage
197 -- collector will ignore it.
198 static_link_value
199 | mayHaveCafRefs caf_refs = mkIntCLit dflags 0
200 | otherwise = mkIntCLit dflags 1 -- No CAF refs
201
202
203 mkStaticClosure :: DynFlags -> CLabel -> CostCentreStack -> [CmmLit]
204 -> [CmmLit] -> [CmmLit] -> [CmmLit] -> [CmmLit]
205 mkStaticClosure dflags info_lbl ccs payload padding static_link_field saved_info_field
206 = [CmmLabel info_lbl]
207 ++ variable_header_words
208 ++ concatMap (padLitToWord dflags) payload
209 ++ padding
210 ++ static_link_field
211 ++ saved_info_field
212 where
213 variable_header_words
214 = staticGranHdr
215 ++ staticParHdr
216 ++ staticProfHdr dflags ccs
217 ++ staticTickyHdr
218
219 -- JD: Simon had ellided this padding, but without it the C back end asserts
220 -- failure. Maybe it's a bad assertion, and this padding is indeed unnecessary?
221 padLitToWord :: DynFlags -> CmmLit -> [CmmLit]
222 padLitToWord dflags lit = lit : padding pad_length
223 where width = typeWidth (cmmLitType dflags lit)
224 pad_length = wORD_SIZE dflags - widthInBytes width :: Int
225
226 padding n | n <= 0 = []
227 | n `rem` 2 /= 0 = CmmInt 0 W8 : padding (n-1)
228 | n `rem` 4 /= 0 = CmmInt 0 W16 : padding (n-2)
229 | n `rem` 8 /= 0 = CmmInt 0 W32 : padding (n-4)
230 | otherwise = CmmInt 0 W64 : padding (n-8)
231
232 -----------------------------------------------------------
233 -- Heap overflow checking
234 -----------------------------------------------------------
235
236 {- Note [Heap checks]
237 ~~~~~~~~~~~~~~~~~~
238 Heap checks come in various forms. We provide the following entry
239 points to the runtime system, all of which use the native C-- entry
240 convention.
241
242 * gc() performs garbage collection and returns
243 nothing to its caller
244
245 * A series of canned entry points like
246 r = gc_1p( r )
247 where r is a pointer. This performs gc, and
248 then returns its argument r to its caller.
249
250 * A series of canned entry points like
251 gcfun_2p( f, x, y )
252 where f is a function closure of arity 2
253 This performs garbage collection, keeping alive the
254 three argument ptrs, and then tail-calls f(x,y)
255
256 These are used in the following circumstances
257
258 * entryHeapCheck: Function entry
259 (a) With a canned GC entry sequence
260 f( f_clo, x:ptr, y:ptr ) {
261 Hp = Hp+8
262 if Hp > HpLim goto L
263 ...
264 L: HpAlloc = 8
265 jump gcfun_2p( f_clo, x, y ) }
266 Note the tail call to the garbage collector;
267 it should do no register shuffling
268
269 (b) No canned sequence
270 f( f_clo, x:ptr, y:ptr, ...etc... ) {
271 T: Hp = Hp+8
272 if Hp > HpLim goto L
273 ...
274 L: HpAlloc = 8
275 call gc() -- Needs an info table
276 goto T }
277
278 * altHeapCheck: Immediately following an eval
279 Started as
280 case f x y of r { (p,q) -> rhs }
281 (a) With a canned sequence for the results of f
282 (which is the very common case since
283 all boxed cases return just one pointer
284 ...
285 r = f( x, y )
286 K: -- K needs an info table
287 Hp = Hp+8
288 if Hp > HpLim goto L
289 ...code for rhs...
290
291 L: r = gc_1p( r )
292 goto K }
293
294 Here, the info table needed by the call
295 to gc_1p should be the *same* as the
296 one for the call to f; the C-- optimiser
297 spots this sharing opportunity)
298
299 (b) No canned sequence for results of f
300 Note second info table
301 ...
302 (r1,r2,r3) = call f( x, y )
303 K:
304 Hp = Hp+8
305 if Hp > HpLim goto L
306 ...code for rhs...
307
308 L: call gc() -- Extra info table here
309 goto K
310
311 * generalHeapCheck: Anywhere else
312 e.g. entry to thunk
313 case branch *not* following eval,
314 or let-no-escape
315 Exactly the same as the previous case:
316
317 K: -- K needs an info table
318 Hp = Hp+8
319 if Hp > HpLim goto L
320 ...
321
322 L: call gc()
323 goto K
324 -}
325
326 --------------------------------------------------------------
327 -- A heap/stack check at a function or thunk entry point.
328
329 entryHeapCheck :: ClosureInfo
330 -> Maybe LocalReg -- Function (closure environment)
331 -> Int -- Arity -- not same as len args b/c of voids
332 -> [LocalReg] -- Non-void args (empty for thunk)
333 -> FCode ()
334 -> FCode ()
335
336 entryHeapCheck cl_info nodeSet arity args code
337 = do dflags <- getDynFlags
338 let is_thunk = arity == 0
339 is_fastf = case closureFunInfo cl_info of
340 Just (_, ArgGen _) -> False
341 _otherwise -> True
342
343 args' = map (CmmReg . CmmLocal) args
344 node = case nodeSet of
345 Just r -> CmmReg (CmmLocal r)
346 Nothing -> CmmLit (CmmLabel $ staticClosureLabel cl_info)
347 stg_gc_fun = CmmReg (CmmGlobal GCFun)
348 stg_gc_enter1 = CmmReg (CmmGlobal GCEnter1)
349
350 {- Thunks: jump stg_gc_enter_1
351
352 Function (fast): call (NativeNode) stg_gc_fun(fun, args)
353
354 Function (slow): R1 = fun
355 call (slow) stg_gc_fun(args)
356 XXX: this is a bit naughty, we should really pass R1 as an
357 argument and use a special calling convention.
358 -}
359 gc_call upd
360 | is_thunk
361 = mkJump dflags stg_gc_enter1 [node] upd
362
363 | is_fastf
364 = mkJump dflags stg_gc_fun (node : args') upd
365
366 | otherwise
367 = mkAssign nodeReg node <*>
368 mkForeignJump dflags Slow stg_gc_fun args' upd
369
370 updfr_sz <- getUpdFrameOff
371
372 loop_id <- newLabelC
373 emitLabel loop_id
374 heapCheck True True (gc_call updfr_sz <*> mkBranch loop_id) code
375
376 {-
377 -- This code is slightly outdated now and we could easily keep the above
378 -- GC methods. However, there may be some performance gains to be made by
379 -- using more specialised GC entry points. Since the semi generic GCFun
380 -- entry needs to check the node and figure out what registers to save...
381 -- if we provided and used more specialised GC entry points then these
382 -- runtime decisions could be turned into compile time decisions.
383
384 args' = case fun of Just f -> f : args
385 Nothing -> args
386 arg_exprs = map (CmmReg . CmmLocal) args'
387 gc_call updfr_sz
388 | arity == 0 = mkJumpGC (CmmReg (CmmGlobal GCEnter1)) arg_exprs updfr_sz
389 | otherwise =
390 case gc_lbl args' of
391 Just _lbl -> panic "StgCmmHeap.entryHeapCheck: not finished"
392 -- mkJumpGC (CmmLit (CmmLabel (mkRtsCodeLabel lbl)))
393 -- arg_exprs updfr_sz
394 Nothing -> mkCall generic_gc (GC, GC) [] [] updfr_sz
395
396 gc_lbl :: [LocalReg] -> Maybe FastString
397 gc_lbl [reg]
398 | isGcPtrType ty = Just (sLit "stg_gc_unpt_r1") -- "stg_gc_fun_1p"
399 | isFloatType ty = case width of
400 W32 -> Just (sLit "stg_gc_f1")
401 W64 -> Just (sLit "stg_gc_d1")
402 _other -> Nothing
403 | width == wordWidth dflags = Just (mkGcLabel "stg_gc_unbx_r1")
404 | width == W64 = Just (mkGcLabel "stg_gc_l1")
405 | otherwise = Nothing
406 where
407 ty = localRegType reg
408 width = typeWidth ty
409
410 gc_lbl regs = gc_lbl_ptrs (map (isGcPtrType . localRegType) regs)
411
412 gc_lbl_ptrs :: [Bool] -> Maybe FastString
413 -- JD: TEMPORARY -- UNTIL THESE FUNCTIONS EXIST...
414 --gc_lbl_ptrs [True,True] = Just (sLit "stg_gc_fun_2p")
415 --gc_lbl_ptrs [True,True,True] = Just (sLit "stg_gc_fun_3p")
416 gc_lbl_ptrs _ = Nothing
417 -}
418
419
420 -- ------------------------------------------------------------
421 -- A heap/stack check in a case alternative
422
423
424 -- If there are multiple alts and we need to GC, but don't have a
425 -- continuation already (the scrut was simple), then we should
426 -- pre-generate the continuation. (if there are multiple alts it is
427 -- always a canned GC point).
428
429 -- altHeapCheck:
430 -- If we have a return continuation,
431 -- then if it is a canned GC pattern,
432 -- then we do mkJumpReturnsTo
433 -- else we do a normal call to stg_gc_noregs
434 -- else if it is a canned GC pattern,
435 -- then generate the continuation and do mkCallReturnsTo
436 -- else we do a normal call to stg_gc_noregs
437
438 altHeapCheck :: [LocalReg] -> FCode a -> FCode a
439 altHeapCheck regs code = altOrNoEscapeHeapCheck False regs code
440
441 altOrNoEscapeHeapCheck :: Bool -> [LocalReg] -> FCode a -> FCode a
442 altOrNoEscapeHeapCheck checkYield regs code = do
443 dflags <- getDynFlags
444 case cannedGCEntryPoint dflags regs of
445 Nothing -> genericGC checkYield code
446 Just gc -> do
447 lret <- newLabelC
448 let (off, copyin) = copyInOflow dflags NativeReturn (Young lret) regs
449 lcont <- newLabelC
450 emitOutOfLine lret (copyin <*> mkBranch lcont)
451 emitLabel lcont
452 cannedGCReturnsTo checkYield False gc regs lret off code
453
454 altHeapCheckReturnsTo :: [LocalReg] -> Label -> ByteOff -> FCode a -> FCode a
455 altHeapCheckReturnsTo regs lret off code
456 = do dflags <- getDynFlags
457 case cannedGCEntryPoint dflags regs of
458 Nothing -> genericGC False code
459 Just gc -> cannedGCReturnsTo False True gc regs lret off code
460
461 -- noEscapeHeapCheck is implemented identically to altHeapCheck (which
462 -- is more efficient), but cannot be optimized away in the non-allocating
463 -- case because it may occur in a loop
464 noEscapeHeapCheck :: [LocalReg] -> FCode a -> FCode a
465 noEscapeHeapCheck regs code = altOrNoEscapeHeapCheck True regs code
466
467 cannedGCReturnsTo :: Bool -> Bool -> CmmExpr -> [LocalReg] -> Label -> ByteOff
468 -> FCode a
469 -> FCode a
470 cannedGCReturnsTo checkYield cont_on_stack gc regs lret off code
471 = do dflags <- getDynFlags
472 updfr_sz <- getUpdFrameOff
473 heapCheck False checkYield (gc_call dflags gc updfr_sz) code
474 where
475 reg_exprs = map (CmmReg . CmmLocal) regs
476 -- Note [stg_gc arguments]
477
478 gc_call dflags label sp
479 | cont_on_stack = mkJumpReturnsTo dflags label GC reg_exprs lret off sp
480 | otherwise = mkCallReturnsTo dflags label GC reg_exprs lret off sp (0,[])
481
482 genericGC :: Bool -> FCode a -> FCode a
483 genericGC checkYield code
484 = do updfr_sz <- getUpdFrameOff
485 lretry <- newLabelC
486 emitLabel lretry
487 call <- mkCall generic_gc (GC, GC) [] [] updfr_sz (0,[])
488 heapCheck False checkYield (call <*> mkBranch lretry) code
489
490 cannedGCEntryPoint :: DynFlags -> [LocalReg] -> Maybe CmmExpr
491 cannedGCEntryPoint dflags regs
492 = case regs of
493 [] -> Just (mkGcLabel "stg_gc_noregs")
494 [reg]
495 | isGcPtrType ty -> Just (mkGcLabel "stg_gc_unpt_r1")
496 | isFloatType ty -> case width of
497 W32 -> Just (mkGcLabel "stg_gc_f1")
498 W64 -> Just (mkGcLabel "stg_gc_d1")
499 _ -> Nothing
500
501 | width == wordWidth dflags -> Just (mkGcLabel "stg_gc_unbx_r1")
502 | width == W64 -> Just (mkGcLabel "stg_gc_l1")
503 | otherwise -> Nothing
504 where
505 ty = localRegType reg
506 width = typeWidth ty
507 _otherwise -> Nothing
508
509 -- Note [stg_gc arguments]
510 -- It might seem that we could avoid passing the arguments to the
511 -- stg_gc function, because they are already in the right registers.
512 -- While this is usually the case, it isn't always. Sometimes the
513 -- code generator has cleverly avoided the eval in a case, e.g. in
514 -- ffi/should_run/4221.hs we found
515 --
516 -- case a_r1mb of z
517 -- FunPtr x y -> ...
518 --
519 -- where a_r1mb is bound a top-level constructor, and is known to be
520 -- evaluated. The codegen just assigns x, y and z, and continues;
521 -- R1 is never assigned.
522 --
523 -- So we'll have to rely on optimisations to eliminatethese
524 -- assignments where possible.
525
526
527 -- | The generic GC procedure; no params, no results
528 generic_gc :: CmmExpr
529 generic_gc = mkGcLabel "stg_gc_noregs"
530
531 -- | Create a CLabel for calling a garbage collector entry point
532 mkGcLabel :: String -> CmmExpr
533 mkGcLabel s = CmmLit (CmmLabel (mkCmmCodeLabel rtsPackageId (fsLit s)))
534
535 -------------------------------
536 heapCheck :: Bool -> Bool -> CmmAGraph -> FCode a -> FCode a
537 heapCheck checkStack checkYield do_gc code
538 = getHeapUsage $ \ hpHw ->
539 -- Emit heap checks, but be sure to do it lazily so
540 -- that the conditionals on hpHw don't cause a black hole
541 do { codeOnly $ do_checks checkStack checkYield hpHw do_gc
542 ; tickyAllocHeap hpHw
543 ; doGranAllocate hpHw
544 ; setRealHp hpHw
545 ; code }
546
547 do_checks :: Bool -- Should we check the stack?
548 -> Bool -- Should we check for preemption?
549 -> WordOff -- Heap headroom
550 -> CmmAGraph -- What to do on failure
551 -> FCode ()
552 do_checks checkStack checkYield alloc do_gc = do
553 dflags <- getDynFlags
554 let
555 alloc_lit = mkIntExpr dflags (alloc * wORD_SIZE dflags) -- Bytes
556 bump_hp = cmmOffsetExprB dflags (CmmReg hpReg) alloc_lit
557
558 -- Sp overflow if (Sp - CmmHighStack < SpLim)
559 sp_oflo = CmmMachOp (mo_wordULt dflags)
560 [CmmMachOp (MO_Sub (typeWidth (cmmRegType dflags spReg)))
561 [CmmReg spReg, CmmLit CmmHighStackMark],
562 CmmReg spLimReg]
563
564 -- Hp overflow if (Hp > HpLim)
565 -- (Hp has been incremented by now)
566 -- HpLim points to the LAST WORD of valid allocation space.
567 hp_oflo = CmmMachOp (mo_wordUGt dflags)
568 [CmmReg hpReg, CmmReg (CmmGlobal HpLim)]
569
570 -- Yielding if HpLim == 0
571 yielding = CmmMachOp (mo_wordEq dflags)
572 [CmmReg (CmmGlobal HpLim), CmmLit (zeroCLit dflags)]
573
574 alloc_n = mkAssign (CmmGlobal HpAlloc) alloc_lit
575 gc_id <- newLabelC
576
577 when checkStack $ do
578 emit =<< mkCmmIfGoto sp_oflo gc_id
579
580 if (alloc /= 0)
581 then do
582 emitAssign hpReg bump_hp
583 emit =<< mkCmmIfThen hp_oflo (alloc_n <*> mkBranch gc_id)
584 else do
585 when (not (dopt Opt_OmitYields dflags) && checkYield) (emit =<< mkCmmIfGoto yielding gc_id)
586
587 emitOutOfLine gc_id $
588 do_gc -- this is expected to jump back somewhere
589
590 -- Test for stack pointer exhaustion, then
591 -- bump heap pointer, and test for heap exhaustion
592 -- Note that we don't move the heap pointer unless the
593 -- stack check succeeds. Otherwise we might end up
594 -- with slop at the end of the current block, which can
595 -- confuse the LDV profiler.
596
597 {-
598
599 {- Unboxed tuple alternatives and let-no-escapes (the two most annoying
600 constructs to generate code for!) For unboxed tuple returns, there
601 are an arbitrary number of possibly unboxed return values, some of
602 which will be in registers, and the others will be on the stack. We
603 always organise the stack-resident fields into pointers &
604 non-pointers, and pass the number of each to the heap check code. -}
605
606 unbxTupleHeapCheck
607 :: [(Id, GlobalReg)] -- Live registers
608 -> WordOff -- no. of stack slots containing ptrs
609 -> WordOff -- no. of stack slots containing nonptrs
610 -> CmmAGraph -- code to insert in the failure path
611 -> FCode ()
612 -> FCode ()
613
614 unbxTupleHeapCheck regs ptrs nptrs fail_code code
615 -- We can't manage more than 255 pointers/non-pointers
616 -- in a generic heap check.
617 | ptrs > 255 || nptrs > 255 = panic "altHeapCheck"
618 | otherwise
619 = initHeapUsage $ \ hpHw -> do
620 { codeOnly $ do { do_checks 0 {- no stack check -} hpHw
621 full_fail_code rts_label
622 ; tickyAllocHeap hpHw }
623 ; setRealHp hpHw
624 ; code }
625 where
626 full_fail_code = fail_code `plusStmts` oneStmt assign_liveness
627 assign_liveness = CmmAssign (CmmGlobal (VanillaReg 9)) -- Ho ho ho!
628 (CmmLit (mkWordCLit liveness))
629 liveness = mkRegLiveness regs ptrs nptrs
630 rts_label = CmmLit (CmmLabel (mkRtsCodeLabel (sLit "stg_gc_ut")))
631
632
633 {- Old Gransim com -- I have no idea whether it still makes sense (SLPJ Sep07)
634 For GrAnSim the code for doing a heap check and doing a context switch
635 has been separated. Especially, the HEAP_CHK macro only performs a
636 heap check. THREAD_CONTEXT_SWITCH should be used for doing a context
637 switch. GRAN_FETCH_AND_RESCHEDULE must be put at the beginning of
638 every slow entry code in order to simulate the fetching of
639 closures. If fetching is necessary (i.e. current closure is not local)
640 then an automatic context switch is done. -}
641
642
643 When failing a check, we save a return address on the stack and
644 jump to a pre-compiled code fragment that saves the live registers
645 and returns to the scheduler.
646
647 The return address in most cases will be the beginning of the basic
648 block in which the check resides, since we need to perform the check
649 again on re-entry because someone else might have stolen the resource
650 in the meantime.
651
652 %************************************************************************
653 %* *
654 Generic Heap/Stack Checks - used in the RTS
655 %* *
656 %************************************************************************
657
658 \begin{code}
659 hpChkGen :: CmmExpr -> CmmExpr -> CmmExpr -> FCode ()
660 hpChkGen bytes liveness reentry
661 = do_checks' bytes True assigns stg_gc_gen
662 where
663 assigns = mkStmts [
664 CmmAssign (CmmGlobal (VanillaReg 9)) liveness,
665 CmmAssign (CmmGlobal (VanillaReg 10)) reentry
666 ]
667
668 -- a heap check where R1 points to the closure to enter on return, and
669 -- we want to assign to Sp[0] on failure (used in AutoApply.cmm:BUILD_PAP).
670 hpChkNodePointsAssignSp0 :: CmmExpr -> CmmExpr -> FCode ()
671 hpChkNodePointsAssignSp0 bytes sp0
672 = do_checks' bytes True assign stg_gc_enter1
673 where assign = oneStmt (CmmStore (CmmReg spReg) sp0)
674
675 stg_gc_gen = CmmLit (CmmLabel (mkRtsCodeLabel (sLit "stg_gc_gen")))
676 \end{code}
677
678 -}