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.
10 #include "HsVersions.h"
13 initHeapUsage, setVirtHp, getVirtAndRealHp, setRealHp,
18 getHpRelOffset, getSpARelOffset, getSpBRelOffset,
24 #if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ <= 201
25 IMPORT_DELOOPER(CgLoop1) -- here for paranoia-checking
28 import AbsCSyn ( RegRelative(..), AbstractC, CAddrMode )
30 import HeapOffs ( zeroOff,
31 SYN_IE(VirtualHeapOffset),
32 SYN_IE(VirtualSpAOffset),
33 SYN_IE(VirtualSpBOffset)
35 import Id ( SYN_IE(IdEnv) )
38 %************************************************************************
40 \subsection[CgUsages-heapery]{Monad things for fiddling with heap usage}
42 %************************************************************************
44 @initHeapUsage@ applies a function to the amount of heap that it uses.
45 It initialises the heap usage to zeros, and passes on an unchanged
48 It is usually a prelude to performing a GC check, so everything must
49 be in a tidy and consistent state.
52 initHeapUsage :: (VirtualHeapOffset -> Code) -> Code
54 initHeapUsage fcode info_down (MkCgState absC binds (a_usage, b_usage, heap_usage))
57 state1 = MkCgState absC binds (a_usage, b_usage, (zeroOff, zeroOff))
58 state2 = fcode (heapHWM heap_usage2) info_down state1
59 (MkCgState absC2 binds2 (a_usage2, b_usage2, heap_usage2)) = state2
60 state3 = MkCgState absC2
62 (a_usage2, b_usage2, heap_usage {- unchanged -})
66 setVirtHp :: VirtualHeapOffset -> Code
67 setVirtHp new_virtHp info_down
68 state@(MkCgState absC binds (a_stk, b_stk, (virtHp, realHp)))
69 = MkCgState absC binds (a_stk, b_stk, (new_virtHp, realHp))
73 getVirtAndRealHp :: FCode (VirtualHeapOffset, VirtualHeapOffset)
74 getVirtAndRealHp info_down state@(MkCgState _ _ (au, bu, (virtHp, realHp)))
75 = ((virtHp, realHp), state)
79 setRealHp :: VirtualHeapOffset -> Code
80 setRealHp realHp info_down (MkCgState absC binds (au, bu, (vHp, _)))
81 = MkCgState absC binds (au, bu, (vHp, realHp))
85 getHpRelOffset :: VirtualHeapOffset -> FCode RegRelative
86 getHpRelOffset virtual_offset info_down state@(MkCgState _ _ (_,_,(_,realHp)))
87 = (HpRel realHp virtual_offset, state)
90 The heap high water mark is the larger of virtHp and hwHp. The latter is
91 only records the high water marks of forked-off branches, so to find the
92 heap high water mark you have to take the max of virtHp and hwHp. Remember,
93 virtHp never retreats!
96 heapHWM (virtHp, realHp) = virtHp
99 %************************************************************************
101 \subsection[CgUsages-stackery]{Monad things for fiddling with stack usage}
103 %************************************************************************
105 @setRealAndVirtualSps@ sets into the environment the offsets of the
106 current position of the real and virtual stack pointers in the current
107 stack frame. The high-water mark is set too. It generates no code.
108 It is used to initialise things at the beginning of a closure body.
111 setRealAndVirtualSps :: VirtualSpAOffset -- New real SpA
112 -> VirtualSpBOffset -- Ditto B stack
115 setRealAndVirtualSps spA spB info_down (MkCgState absC binds
116 ((vspA,fA,realSpA,hwspA),
117 (vspB,fB,realSpB,hwspB),
119 = MkCgState absC binds new_usage
121 new_usage = ((spA, fA, spA, spA),
127 getVirtSps :: FCode (VirtualSpAOffset,VirtualSpBOffset)
128 getVirtSps info_down state@(MkCgState absC binds ((virtSpA,_,_,_), (virtSpB,_,_,_), _))
129 = ((virtSpA,virtSpB), state)
133 getSpARelOffset :: VirtualSpAOffset -> FCode RegRelative
134 getSpARelOffset virtual_offset info_down state@(MkCgState _ _ ((_,_,realSpA,_),_,_))
135 = (SpARel realSpA virtual_offset, state)
137 getSpBRelOffset :: VirtualSpBOffset -> FCode RegRelative
138 getSpBRelOffset virtual_offset info_down state@(MkCgState _ _ (_,(_,_,realSpB,_),_))
139 = (SpBRel realSpB virtual_offset, state)
143 freeBStkSlot :: VirtualSpBOffset -> Code
144 freeBStkSlot b_slot info_down
145 state@(MkCgState absC binds (spa_usage, (virtSpB,free_b,realSpB,hwSpB), heap_usage))
146 = MkCgState absC binds (spa_usage, (virtSpB,new_free_b,realSpB,hwSpB), heap_usage)
148 new_free_b = addFreeBSlots free_b [b_slot]