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