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,
17 --UNUSED: getVirtSpRelOffsets,
21 -- and to make the interface self-sufficient...
22 AbstractC, HeapOffset, RegRelative, CgState
30 %************************************************************************
32 \subsection[CgUsages-heapery]{Monad things for fiddling with heap usage}
34 %************************************************************************
36 @initHeapUsage@ applies a function to the amount of heap that it uses.
37 It initialises the heap usage to zeros, and passes on an unchanged
40 It is usually a prelude to performing a GC check, so everything must
41 be in a tidy and consistent state.
44 initHeapUsage :: (VirtualHeapOffset -> Code) -> Code
46 initHeapUsage fcode info_down (MkCgState absC binds (a_usage, b_usage, heap_usage))
49 state1 = MkCgState absC binds (a_usage, b_usage, (zeroOff, zeroOff))
50 state2 = fcode (heapHWM heap_usage2) info_down state1
51 (MkCgState absC2 binds2 (a_usage2, b_usage2, heap_usage2)) = state2
52 state3 = MkCgState absC2
54 (a_usage2, b_usage2, heap_usage {- unchanged -})
58 setVirtHp :: VirtualHeapOffset -> Code
59 setVirtHp new_virtHp info_down
60 state@(MkCgState absC binds (a_stk, b_stk, (virtHp, realHp)))
61 = MkCgState absC binds (a_stk, b_stk, (new_virtHp, realHp))
65 getVirtAndRealHp :: FCode (VirtualHeapOffset, VirtualHeapOffset)
66 getVirtAndRealHp info_down state@(MkCgState _ _ (au, bu, (virtHp, realHp)))
67 = ((virtHp, realHp), state)
71 setRealHp :: VirtualHeapOffset -> Code
72 setRealHp realHp info_down (MkCgState absC binds (au, bu, (vHp, _)))
73 = MkCgState absC binds (au, bu, (vHp, realHp))
77 getHpRelOffset :: VirtualHeapOffset -> FCode RegRelative
78 getHpRelOffset virtual_offset info_down state@(MkCgState _ _ (_,_,(_,realHp)))
79 = (HpRel realHp virtual_offset, state)
82 The heap high water mark is the larger of virtHp and hwHp. The latter is
83 only records the high water marks of forked-off branches, so to find the
84 heap high water mark you have to take the max of virtHp and hwHp. Remember,
85 virtHp never retreats!
88 heapHWM (virtHp, realHp) = virtHp
91 %************************************************************************
93 \subsection[CgUsages-stackery]{Monad things for fiddling with stack usage}
95 %************************************************************************
97 @setRealAndVirtualSps@ sets into the environment the offsets of the
98 current position of the real and virtual stack pointers in the current
99 stack frame. The high-water mark is set too. It generates no code.
100 It is used to initialise things at the beginning of a closure body.
103 setRealAndVirtualSps :: VirtualSpAOffset -- New real SpA
104 -> VirtualSpBOffset -- Ditto B stack
107 setRealAndVirtualSps spA spB info_down (MkCgState absC binds
108 ((vspA,fA,realSpA,hwspA),
109 (vspB,fB,realSpB,hwspB),
111 = MkCgState absC binds new_usage
113 new_usage = ((spA, fA, spA, spA),
119 getVirtSps :: FCode (VirtualSpAOffset,VirtualSpBOffset)
120 getVirtSps info_down state@(MkCgState absC binds ((virtSpA,_,_,_), (virtSpB,_,_,_), _))
121 = ((virtSpA,virtSpB), state)
125 getSpARelOffset :: VirtualSpAOffset -> FCode RegRelative
126 getSpARelOffset virtual_offset info_down state@(MkCgState _ _ ((_,_,realSpA,_),_,_))
127 = (SpARel realSpA virtual_offset, state)
129 getSpBRelOffset :: VirtualSpBOffset -> FCode RegRelative
130 getSpBRelOffset virtual_offset info_down state@(MkCgState _ _ (_,(_,_,realSpB,_),_))
131 = (SpBRel realSpB virtual_offset, state)
137 getVirtSpRelOffsets :: FCode (RegRelative, RegRelative)
138 getVirtSpRelOffsets info_down
139 state@(MkCgState absC binds ((virtSpA,_,realSpA,_), (virtSpB,_,realSpB,_), _))
140 = ((SpARel realSpA virtSpA, SpBRel realSpB virtSpB), state)
145 freeBStkSlot :: VirtualSpBOffset -> Code
146 freeBStkSlot b_slot info_down
147 state@(MkCgState absC binds (spa_usage, (virtSpB,free_b,realSpB,hwSpB), heap_usage))
148 = MkCgState absC binds (spa_usage, (virtSpB,new_free_b,realSpB,hwSpB), heap_usage)
150 new_free_b = addFreeBSlots free_b [b_slot]