[project @ 2003-06-27 18:28:31 by sof]
[ghc-hetmet.git] / ghc / rts / Updates.hc
1 /* -----------------------------------------------------------------------------
2  * $Id: Updates.hc,v 1.40 2003/05/14 09:14:00 simonmar Exp $
3  *
4  * (c) The GHC Team, 1998-2002
5  *
6  * Code to perform updates.
7  *
8  * ---------------------------------------------------------------------------*/
9
10 #include "Stg.h"
11 #include "Rts.h"
12 #include "RtsUtils.h"
13 #include "RtsFlags.h"
14 #include "Storage.h"
15 #if defined(GRAN) || defined(PAR)
16 # include "FetchMe.h"
17 #endif
18
19 /*
20   The update frame return address must be *polymorphic*, that means
21   we have to cope with both vectored and non-vectored returns.  This
22   is done by putting the return vector right before the info table, and
23   having a standard direct return address after the info table (pointed
24   to by the return address itself, as usual).
25
26   Each entry in the vector table points to a specialised entry code fragment
27   that knows how to return after doing the update.  It would be possible to
28   use a single generic piece of code that simply entered the return value
29   to return, but it's quicker this way.  The direct return code of course
30   just does another direct return when it's finished.
31 */
32
33 /* on entry to the update code
34    (1) R1 points to the closure being returned
35    (2) Sp points to the update frame
36    */
37
38 /* The update fragment has been tuned so as to generate reasonable
39    code with gcc, which accounts for some of the strangeness in the
40    way it is written.  
41
42    In particular, the JMP_(ret) bit is passed down and pinned on the
43    end of each branch (there end up being two major branches in the
44    code), since we don't mind duplicating this jump.
45 */
46
47 #define UPD_FRAME_ENTRY_TEMPLATE(label,ind_info,ret)                    \
48         STGFUN(label);                                                  \
49         STGFUN(label)                                                   \
50         {                                                               \
51           StgClosure *updatee;                                          \
52           FB_                                                           \
53                                                                         \
54           updatee = ((StgUpdateFrame *)Sp)->updatee;                    \
55                                                                         \
56           /* remove the update frame from the stack */                  \
57           Sp += sizeofW(StgUpdateFrame);                                \
58                                                                         \
59           /* Tick - it must be a con, all the paps are handled          \
60            * in stg_upd_PAP and PAP_entry below                         \
61            */                                                           \
62           TICK_UPD_CON_IN_NEW(sizeW_fromITBL(get_itbl(updatee)));       \
63                                                                         \
64           UPD_SPEC_IND(updatee, ind_info, R1.cl, JMP_(ret));            \
65           FE_                                                           \
66         }
67
68 UPD_FRAME_ENTRY_TEMPLATE(stg_upd_frame_ret,&stg_IND_direct_info,ENTRY_CODE(Sp[0]));
69 UPD_FRAME_ENTRY_TEMPLATE(stg_upd_frame_0_ret,&stg_IND_0_info,RET_VEC(Sp[0],0));
70 UPD_FRAME_ENTRY_TEMPLATE(stg_upd_frame_1_ret,&stg_IND_1_info,RET_VEC(Sp[0],1));
71 UPD_FRAME_ENTRY_TEMPLATE(stg_upd_frame_2_ret,&stg_IND_2_info,RET_VEC(Sp[0],2));
72 UPD_FRAME_ENTRY_TEMPLATE(stg_upd_frame_3_ret,&stg_IND_3_info,RET_VEC(Sp[0],3));
73 UPD_FRAME_ENTRY_TEMPLATE(stg_upd_frame_4_ret,&stg_IND_4_info,RET_VEC(Sp[0],4));
74 UPD_FRAME_ENTRY_TEMPLATE(stg_upd_frame_5_ret,&stg_IND_5_info,RET_VEC(Sp[0],5));
75 UPD_FRAME_ENTRY_TEMPLATE(stg_upd_frame_6_ret,&stg_IND_6_info,RET_VEC(Sp[0],6));
76 UPD_FRAME_ENTRY_TEMPLATE(stg_upd_frame_7_ret,&stg_IND_7_info,RET_VEC(Sp[0],7));
77
78 /*
79   Make sure this table is big enough to handle the maximum vectored
80   return size!
81   */
82
83 #if defined(PROFILING)
84 #define UPD_FRAME_BITMAP 3
85 #define UPD_FRAME_WORDS  3
86 #else
87 #define UPD_FRAME_BITMAP 0
88 #define UPD_FRAME_WORDS  1
89 #endif
90
91 /* this bitmap indicates that the first word of an update frame is a
92  * non-pointer - this is the update frame link.  (for profiling,
93  * there's a cost-centre-stack in there too).
94  */
95
96 VEC_POLY_INFO_TABLE( stg_upd_frame, 
97                      MK_SMALL_BITMAP(UPD_FRAME_WORDS, UPD_FRAME_BITMAP),
98                      0/*srt*/, 0/*srt_off*/, 0/*srt_bitmap*/,
99                      UPDATE_FRAME,, EF_);
100
101 /*-----------------------------------------------------------------------------
102   Seq frames 
103
104   We don't have a primitive seq# operator: it is just a 'case'
105   expression whose scrutinee has either a polymorphic or function type
106   (constructor types can be handled by normal 'case' expressions).
107
108   To handle a polymorphic/function typed seq, we push a SEQ frame on
109   the stack.  This is a polymorphic activation record that just pops
110   itself and returns (in a non-vectored way) when entered.  The
111   purpose of the SEQ frame is to avoid having to make a polymorphic return
112   point for each polymorphic case expression.  
113
114   Another way of looking at it: the SEQ frame turns a vectored return
115   into a direct one.
116   -------------------------------------------------------------------------- */
117
118 IF_(stg_seq_frame_ret);
119
120 #define stg_seq_frame_0_ret stg_seq_frame_ret
121 #define stg_seq_frame_1_ret stg_seq_frame_ret
122 #define stg_seq_frame_2_ret stg_seq_frame_ret
123 #define stg_seq_frame_3_ret stg_seq_frame_ret
124 #define stg_seq_frame_4_ret stg_seq_frame_ret
125 #define stg_seq_frame_5_ret stg_seq_frame_ret
126 #define stg_seq_frame_6_ret stg_seq_frame_ret
127 #define stg_seq_frame_7_ret stg_seq_frame_ret
128
129 VEC_POLY_INFO_TABLE( stg_seq_frame,
130                      MK_SMALL_BITMAP(0, 0),
131                      0/*srt*/, 0/*srt_off*/, 0/*srt_bitmap*/,
132                      RET_SMALL,, EF_);
133
134 IF_(stg_seq_frame_ret)
135 {
136    FB_
137    Sp ++;
138    JMP_(ENTRY_CODE(Sp[0]));
139    FE_
140 }