2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1995
4 \section[CgUsages]{Accessing and modifying stacks and heap usage info}
6 This module provides the functions to access (\tr{get*} functions) and
7 modify (\tr{set*} functions) the stacks and heap usage information.
11 initHeapUsage, setVirtHp, getVirtAndRealHp, setRealHp,
16 getHpRelOffset, getSpARelOffset, getSpBRelOffset,
22 import CgLoop1 -- here for paranoia-checking
24 import AbsCSyn ( RegRelative(..), AbstractC, CAddrMode )
26 import HeapOffs ( zeroOff,
27 VirtualHeapOffset(..),
31 import Id ( IdEnv(..) )
34 %************************************************************************
36 \subsection[CgUsages-heapery]{Monad things for fiddling with heap usage}
38 %************************************************************************
40 @initHeapUsage@ applies a function to the amount of heap that it uses.
41 It initialises the heap usage to zeros, and passes on an unchanged
44 It is usually a prelude to performing a GC check, so everything must
45 be in a tidy and consistent state.
48 initHeapUsage :: (VirtualHeapOffset -> Code) -> Code
50 initHeapUsage fcode info_down (MkCgState absC binds (a_usage, b_usage, heap_usage))
53 state1 = MkCgState absC binds (a_usage, b_usage, (zeroOff, zeroOff))
54 state2 = fcode (heapHWM heap_usage2) info_down state1
55 (MkCgState absC2 binds2 (a_usage2, b_usage2, heap_usage2)) = state2
56 state3 = MkCgState absC2
58 (a_usage2, b_usage2, heap_usage {- unchanged -})
62 setVirtHp :: VirtualHeapOffset -> Code
63 setVirtHp new_virtHp info_down
64 state@(MkCgState absC binds (a_stk, b_stk, (virtHp, realHp)))
65 = MkCgState absC binds (a_stk, b_stk, (new_virtHp, realHp))
69 getVirtAndRealHp :: FCode (VirtualHeapOffset, VirtualHeapOffset)
70 getVirtAndRealHp info_down state@(MkCgState _ _ (au, bu, (virtHp, realHp)))
71 = ((virtHp, realHp), state)
75 setRealHp :: VirtualHeapOffset -> Code
76 setRealHp realHp info_down (MkCgState absC binds (au, bu, (vHp, _)))
77 = MkCgState absC binds (au, bu, (vHp, realHp))
81 getHpRelOffset :: VirtualHeapOffset -> FCode RegRelative
82 getHpRelOffset virtual_offset info_down state@(MkCgState _ _ (_,_,(_,realHp)))
83 = (HpRel realHp virtual_offset, state)
86 The heap high water mark is the larger of virtHp and hwHp. The latter is
87 only records the high water marks of forked-off branches, so to find the
88 heap high water mark you have to take the max of virtHp and hwHp. Remember,
89 virtHp never retreats!
92 heapHWM (virtHp, realHp) = virtHp
95 %************************************************************************
97 \subsection[CgUsages-stackery]{Monad things for fiddling with stack usage}
99 %************************************************************************
101 @setRealAndVirtualSps@ sets into the environment the offsets of the
102 current position of the real and virtual stack pointers in the current
103 stack frame. The high-water mark is set too. It generates no code.
104 It is used to initialise things at the beginning of a closure body.
107 setRealAndVirtualSps :: VirtualSpAOffset -- New real SpA
108 -> VirtualSpBOffset -- Ditto B stack
111 setRealAndVirtualSps spA spB info_down (MkCgState absC binds
112 ((vspA,fA,realSpA,hwspA),
113 (vspB,fB,realSpB,hwspB),
115 = MkCgState absC binds new_usage
117 new_usage = ((spA, fA, spA, spA),
123 getVirtSps :: FCode (VirtualSpAOffset,VirtualSpBOffset)
124 getVirtSps info_down state@(MkCgState absC binds ((virtSpA,_,_,_), (virtSpB,_,_,_), _))
125 = ((virtSpA,virtSpB), state)
129 getSpARelOffset :: VirtualSpAOffset -> FCode RegRelative
130 getSpARelOffset virtual_offset info_down state@(MkCgState _ _ ((_,_,realSpA,_),_,_))
131 = (SpARel realSpA virtual_offset, state)
133 getSpBRelOffset :: VirtualSpBOffset -> FCode RegRelative
134 getSpBRelOffset virtual_offset info_down state@(MkCgState _ _ (_,(_,_,realSpB,_),_))
135 = (SpBRel realSpB virtual_offset, state)
139 freeBStkSlot :: VirtualSpBOffset -> Code
140 freeBStkSlot b_slot info_down
141 state@(MkCgState absC binds (spa_usage, (virtSpB,free_b,realSpB,hwSpB), heap_usage))
142 = MkCgState absC binds (spa_usage, (virtSpB,new_free_b,realSpB,hwSpB), heap_usage)
144 new_free_b = addFreeBSlots free_b [b_slot]