2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
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, getSpRelOffset
19 #include "HsVersions.h"
21 import AbsCSyn ( RegRelative(..), VirtualHeapOffset, VirtualSpOffset,
26 %************************************************************************
28 \subsection[CgUsages-heapery]{Monad things for fiddling with heap usage}
30 %************************************************************************
32 @initHeapUsage@ applies a function to the amount of heap that it uses.
33 It initialises the heap usage to zeros, and passes on an unchanged
36 It is usually a prelude to performing a GC check, so everything must
37 be in a tidy and consistent state.
40 initHeapUsage :: (VirtualHeapOffset -> Code) -> Code
42 initHeapUsage fcode info_down (MkCgState absC binds (stk_usage, heap_usage))
45 state1 = MkCgState absC binds (stk_usage, (0, 0))
46 state2 = fcode (heapHWM heap_usage2) info_down state1
47 (MkCgState absC2 binds2 (stk_usage2, heap_usage2)) = state2
48 state3 = MkCgState absC2
50 (stk_usage2, heap_usage {- unchanged -})
54 setVirtHp :: VirtualHeapOffset -> Code
55 setVirtHp new_virtHp info_down
56 state@(MkCgState absC binds (stk, (virtHp, realHp)))
57 = MkCgState absC binds (stk, (new_virtHp, realHp))
61 getVirtAndRealHp :: FCode (VirtualHeapOffset, VirtualHeapOffset)
62 getVirtAndRealHp info_down state@(MkCgState _ _ (_, (virtHp, realHp)))
63 = ((virtHp, realHp), state)
67 setRealHp :: VirtualHeapOffset -> Code
68 setRealHp realHp info_down (MkCgState absC binds (stk_usage, (vHp, _)))
69 = MkCgState absC binds (stk_usage, (vHp, realHp))
73 getHpRelOffset :: VirtualHeapOffset -> FCode RegRelative
74 getHpRelOffset virtual_offset info_down state@(MkCgState _ _ (_,(_,realHp)))
75 = (hpRel realHp virtual_offset, state)
78 The heap high water mark is the larger of virtHp and hwHp. The latter is
79 only records the high water marks of forked-off branches, so to find the
80 heap high water mark you have to take the max of virtHp and hwHp. Remember,
81 virtHp never retreats!
84 heapHWM (virtHp, realHp) = virtHp
87 %************************************************************************
89 \subsection[CgUsages-stackery]{Monad things for fiddling with stack usage}
91 %************************************************************************
93 @setRealAndVirtualSp@ sets into the environment the offsets of the
94 current position of the real and virtual stack pointers in the current
95 stack frame. The high-water mark is set too. It generates no code.
96 It is used to initialise things at the beginning of a closure body.
99 setRealAndVirtualSp :: VirtualSpOffset -- New real Sp
102 setRealAndVirtualSp sp info_down (MkCgState absC binds
103 ((vsp,f,realSp,hwsp), h_usage))
104 = MkCgState absC binds new_usage
106 new_usage = ((sp, f, sp, sp), h_usage)
110 getVirtSp :: FCode VirtualSpOffset
111 getVirtSp info_down state@(MkCgState absC binds ((virtSp,_,_,_), _))
114 getRealSp :: FCode VirtualSpOffset
115 getRealSp info_down state@(MkCgState absC binds ((_,_,realSp,_),_))
120 getSpRelOffset :: VirtualSpOffset -> FCode RegRelative
121 getSpRelOffset virtual_offset info_down state@(MkCgState _ _ ((_,_,realSp,_),_))
122 = (spRel realSp virtual_offset, state)