[project @ 1998-12-02 13:17:09 by simonm]
[ghc-hetmet.git] / ghc / compiler / codeGen / CgUsages.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[CgUsages]{Accessing and modifying stacks and heap usage info}
5
6 This module provides the functions to access (\tr{get*} functions) and
7 modify (\tr{set*} functions) the stacks and heap usage information.
8
9 \begin{code}
10 module CgUsages (
11         initHeapUsage, setVirtHp, getVirtAndRealHp, setRealHp,
12         setRealAndVirtualSp,
13
14         getVirtSp, getRealSp,
15
16         getHpRelOffset, getSpRelOffset
17     ) where
18
19 #include "HsVersions.h"
20
21 import AbsCSyn          ( RegRelative(..), VirtualHeapOffset, VirtualSpOffset,
22                           hpRel, spRel )
23 import CgMonad
24 \end{code}
25
26 %************************************************************************
27 %*                                                                      *
28 \subsection[CgUsages-heapery]{Monad things for fiddling with heap usage}
29 %*                                                                      *
30 %************************************************************************
31
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
34 heap usage.
35
36 It is usually a prelude to performing a GC check, so everything must
37 be in a tidy and consistent state.
38
39 \begin{code}
40 initHeapUsage :: (VirtualHeapOffset -> Code) -> Code
41
42 initHeapUsage fcode info_down (MkCgState absC binds (stk_usage, heap_usage))
43   = state3
44   where
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
49                         binds2
50                         (stk_usage2, heap_usage {- unchanged -})
51 \end{code}
52
53 \begin{code}
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))
58 \end{code}
59
60 \begin{code}
61 getVirtAndRealHp :: FCode (VirtualHeapOffset, VirtualHeapOffset)
62 getVirtAndRealHp info_down state@(MkCgState _ _ (_, (virtHp, realHp)))
63   = ((virtHp, realHp), state)
64 \end{code}
65
66 \begin{code}
67 setRealHp ::  VirtualHeapOffset -> Code
68 setRealHp realHp info_down (MkCgState absC binds (stk_usage, (vHp, _)))
69   = MkCgState absC binds (stk_usage, (vHp, realHp))
70 \end{code}
71
72 \begin{code}
73 getHpRelOffset :: VirtualHeapOffset -> FCode RegRelative
74 getHpRelOffset virtual_offset info_down state@(MkCgState _ _ (_,(_,realHp)))
75   = (hpRel realHp virtual_offset, state)
76 \end{code}
77
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!
82
83 \begin{code}
84 heapHWM (virtHp, realHp) = virtHp
85 \end{code}
86
87 %************************************************************************
88 %*                                                                      *
89 \subsection[CgUsages-stackery]{Monad things for fiddling with stack usage}
90 %*                                                                      *
91 %************************************************************************
92
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.
97
98 \begin{code}
99 setRealAndVirtualSp :: VirtualSpOffset  -- New real Sp
100                      -> Code
101
102 setRealAndVirtualSp sp info_down (MkCgState absC binds
103                                         ((vsp,f,realSp,hwsp), h_usage))
104   = MkCgState absC binds new_usage
105   where
106     new_usage = ((sp, f, sp, sp), h_usage)
107 \end{code}
108
109 \begin{code}
110 getVirtSp :: FCode VirtualSpOffset
111 getVirtSp info_down state@(MkCgState absC binds ((virtSp,_,_,_), _))
112   = (virtSp, state)
113
114 getRealSp :: FCode VirtualSpOffset
115 getRealSp info_down state@(MkCgState absC binds ((_,_,realSp,_),_)) 
116   = (realSp,state)
117 \end{code}
118
119 \begin{code}
120 getSpRelOffset :: VirtualSpOffset -> FCode RegRelative
121 getSpRelOffset virtual_offset info_down state@(MkCgState _ _ ((_,_,realSp,_),_))
122   = (spRel realSp virtual_offset, state)
123 \end{code}