[project @ 2000-07-11 16:03:37 by simonmar]
[ghc-hetmet.git] / ghc / compiler / codeGen / CgUpdate.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[CgUpdate]{Manipulating update frames}
5
6 \begin{code}
7 module CgUpdate ( pushUpdateFrame, reserveSeqFrame, pushSeqFrame ) where
8
9 #include "HsVersions.h"
10
11 import CgMonad
12 import AbsCSyn
13
14 import CgStackery       ( allocStackTop, updateFrameSize, seqFrameSize )
15 import CgUsages         ( getVirtSp, getSpRelOffset )
16 import Panic            ( assertPanic )
17 \end{code}
18
19
20 %********************************************************
21 %*                                                      *
22 %*              Setting up update frames                *
23 %*                                                      *
24 %********************************************************
25 \subsection[setting-update-frames]{Setting up update frames}
26
27 @pushUpdateFrame@ $updatee$ pushes a general update frame which
28 points to $updatee$ as the thing to be updated.  It is only used
29 when a thunk has just been entered, so the (real) stack pointers
30 are guaranteed to be nicely aligned with the top of stack.
31 @pushUpdateFrame@ adjusts the virtual and tail stack pointers
32 to reflect the frame pushed.
33
34 \begin{code}
35 pushUpdateFrame :: CAddrMode -> Code -> Code
36
37 pushUpdateFrame updatee code
38   = 
39 #ifdef DEBUG
40     getEndOfBlockInfo                   `thenFC` \ eob_info ->
41     ASSERT(case eob_info of { EndOfBlockInfo _ (OnStack _) -> True; 
42                               _ -> False})
43 #endif
44
45     allocStackTop updateFrameSize       `thenFC` \ _ ->
46     getVirtSp                           `thenFC` \ vsp ->
47
48     setEndOfBlockInfo (EndOfBlockInfo vsp UpdateCode) (
49
50                 -- Emit the push macro
51             absC (CMacroStmt PUSH_UPD_FRAME [
52                         updatee,
53                         int_CLit0  -- we just entered a closure, so must be zero
54             ])
55             `thenC` code
56     )
57
58 int_CLit0 = mkIntCLit 0 -- out here to avoid pushUpdateFrame CAF (sigh)
59 \end{code}
60
61 We push a SEQ frame just before evaluating the scrutinee of a case, if
62 the scrutinee has a polymorphic or function type.  The SEQ frame acts
63 as a barrier in case the scrutinee evaluates to a partial application.
64
65 reserveSeqFrame takes the EndOfBlockInfo for the case expression and
66 updates the sequel to a SeqFrame, reserving room for the frame at
67 args_sp.  When the scrutinee comes around to pushing a return address,
68 it will also push the SEQ frame, using pushSeqFrame.
69
70 \begin{code}
71 reserveSeqFrame :: EndOfBlockInfo -> EndOfBlockInfo
72 reserveSeqFrame (EndOfBlockInfo args_sp (CaseAlts amode stuff)) 
73   = EndOfBlockInfo (args_sp + seqFrameSize) (SeqFrame amode stuff)
74
75 pushSeqFrame :: VirtualSpOffset -> FCode VirtualSpOffset
76 pushSeqFrame args_sp
77   = getSpRelOffset args_sp  `thenFC` \ sp_rel ->
78     absC (CMacroStmt PUSH_SEQ_FRAME [CAddr sp_rel]) `thenC`
79     returnFC (args_sp - seqFrameSize)
80 \end{code}