bec9b40d7a70b0436fcce135a96ed716af25a0d5
[ghc-hetmet.git] / ghc / rts / Updates.cmm
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 1998-2004
4  *
5  * Code to perform updates.
6  *
7  * This file is written in a subset of C--, extended with various
8  * features specific to GHC.  It is compiled by GHC directly.  For the
9  * syntax of .cmm files, see the parser in ghc/compiler/cmm/CmmParse.y.
10  *
11  * ---------------------------------------------------------------------------*/
12
13 #include "Cmm.h"
14 #include "Updates.h"
15 #include "StgLdvProf.h"
16
17 /*
18   The update frame return address must be *polymorphic*, that means
19   we have to cope with both vectored and non-vectored returns.  This
20   is done by putting the return vector right before the info table, and
21   having a standard direct return address after the info table (pointed
22   to by the return address itself, as usual).
23
24   Each entry in the vector table points to a specialised entry code fragment
25   that knows how to return after doing the update.  It would be possible to
26   use a single generic piece of code that simply entered the return value
27   to return, but it's quicker this way.  The direct return code of course
28   just does another direct return when it's finished.
29 */
30
31 /* on entry to the update code
32    (1) R1 points to the closure being returned
33    (2) Sp points to the update frame
34 */
35
36 /* The update fragment has been tuned so as to generate good
37    code with gcc, which accounts for some of the strangeness in the
38    way it is written.  
39
40    In particular, the JMP_(ret) bit is passed down and pinned on the
41    end of each branch (there end up being two major branches in the
42    code), since we don't mind duplicating this jump.
43 */
44
45 #define UPD_FRAME_ENTRY_TEMPLATE(label,ind_info,ret)                    \
46         label                                                           \
47         {                                                               \
48           W_ updatee;                                                   \
49                                                                         \
50           updatee = StgUpdateFrame_updatee(Sp);                         \
51                                                                         \
52           /* remove the update frame from the stack */                  \
53           Sp = Sp + SIZEOF_StgUpdateFrame;                              \
54                                                                         \
55           /* ToDo: it might be a PAP, so we should check... */          \
56           TICK_UPD_CON_IN_NEW(sizeW_fromITBL(%GET_STD_INFO(updatee)));  \
57                                                                         \
58           NOBH_UPD_SPEC_IND(updatee, ind_info, R1, jump (ret));         \
59         }
60
61 UPD_FRAME_ENTRY_TEMPLATE(stg_upd_frame_0_ret,stg_IND_0_info,%RET_VEC(Sp(0),0))
62 UPD_FRAME_ENTRY_TEMPLATE(stg_upd_frame_1_ret,stg_IND_1_info,%RET_VEC(Sp(0),1))
63 UPD_FRAME_ENTRY_TEMPLATE(stg_upd_frame_2_ret,stg_IND_2_info,%RET_VEC(Sp(0),2))
64 UPD_FRAME_ENTRY_TEMPLATE(stg_upd_frame_3_ret,stg_IND_3_info,%RET_VEC(Sp(0),3))
65 UPD_FRAME_ENTRY_TEMPLATE(stg_upd_frame_4_ret,stg_IND_4_info,%RET_VEC(Sp(0),4))
66 UPD_FRAME_ENTRY_TEMPLATE(stg_upd_frame_5_ret,stg_IND_5_info,%RET_VEC(Sp(0),5))
67 UPD_FRAME_ENTRY_TEMPLATE(stg_upd_frame_6_ret,stg_IND_6_info,%RET_VEC(Sp(0),6))
68 UPD_FRAME_ENTRY_TEMPLATE(stg_upd_frame_7_ret,stg_IND_7_info,%RET_VEC(Sp(0),7))
69
70 #if MAX_VECTORED_RTN > 8
71 #error MAX_VECTORED_RTN has changed: please modify stg_upd_frame too.
72 #endif
73
74 /*
75   Make sure this table is big enough to handle the maximum vectored
76   return size!
77   */
78
79 #if defined(PROFILING)
80 #define UPD_FRAME_BITMAP 3
81 #define UPD_FRAME_WORDS  3
82 #else
83 #define UPD_FRAME_BITMAP 0
84 #define UPD_FRAME_WORDS  1
85 #endif
86
87 /* this bitmap indicates that the first word of an update frame is a
88  * non-pointer - this is the update frame link.  (for profiling,
89  * there's a cost-centre-stack in there too).
90  */
91
92 INFO_TABLE_RET( stg_upd_frame, 
93             UPD_FRAME_WORDS, UPD_FRAME_BITMAP, UPDATE_FRAME,
94             stg_upd_frame_0_ret,
95             stg_upd_frame_1_ret,
96             stg_upd_frame_2_ret,
97             stg_upd_frame_3_ret,
98             stg_upd_frame_4_ret,
99             stg_upd_frame_5_ret,
100             stg_upd_frame_6_ret,
101             stg_upd_frame_7_ret
102             )
103 UPD_FRAME_ENTRY_TEMPLATE(,stg_IND_direct_info,%ENTRY_CODE(Sp(0)))
104
105 /* -----------------------------------------------------------------------------
106  * Update frames that check for BLACKHOLE_BQ
107  *
108  * A normal update frame doesn't check whether the closure has been
109  * overwritten with BLACKHOLE_BQ, because it can't until lazy blackholing
110  * has happened.  Lazy blackholing overwrites the update frames pointing
111  * to BLACKHOLE with stg_bh_upd_frame_info.
112  * -------------------------------------------------------------------------- */
113
114 #define BH_UPD_FRAME_ENTRY_TEMPLATE(label,ind_info,ret)                 \
115         label                                                           \
116         {                                                               \
117           W_ updatee;                                                   \
118                                                                         \
119           updatee = StgUpdateFrame_updatee(Sp);                         \
120                                                                         \
121           /* remove the update frame from the stack */                  \
122           Sp = Sp + SIZEOF_StgUpdateFrame;                              \
123                                                                         \
124           /* ToDo: it might be a PAP, so we should check... */          \
125           TICK_UPD_CON_IN_NEW(sizeW_fromITBL(%GET_STD_INFO(updatee)));  \
126                                                                         \
127           UPD_SPEC_IND(updatee, ind_info, R1, jump (ret));              \
128         }
129
130 BH_UPD_FRAME_ENTRY_TEMPLATE(stg_bh_upd_frame_0_ret,stg_IND_0_info,%RET_VEC(Sp(0),0))
131 BH_UPD_FRAME_ENTRY_TEMPLATE(stg_bh_upd_frame_1_ret,stg_IND_1_info,%RET_VEC(Sp(0),1))
132 BH_UPD_FRAME_ENTRY_TEMPLATE(stg_bh_upd_frame_2_ret,stg_IND_2_info,%RET_VEC(Sp(0),2))
133 BH_UPD_FRAME_ENTRY_TEMPLATE(stg_bh_upd_frame_3_ret,stg_IND_3_info,%RET_VEC(Sp(0),3))
134 BH_UPD_FRAME_ENTRY_TEMPLATE(stg_bh_upd_frame_4_ret,stg_IND_4_info,%RET_VEC(Sp(0),4))
135 BH_UPD_FRAME_ENTRY_TEMPLATE(stg_bh_upd_frame_5_ret,stg_IND_5_info,%RET_VEC(Sp(0),5))
136 BH_UPD_FRAME_ENTRY_TEMPLATE(stg_bh_upd_frame_6_ret,stg_IND_6_info,%RET_VEC(Sp(0),6))
137 BH_UPD_FRAME_ENTRY_TEMPLATE(stg_bh_upd_frame_7_ret,stg_IND_7_info,%RET_VEC(Sp(0),7))
138
139 #if MAX_VECTORED_RTN > 8
140 #error MAX_VECTORED_RTN has changed: please modify stg_bh_upd_frame too.
141 #endif
142
143 /* this bitmap indicates that the first word of an update frame is a
144  * non-pointer - this is the update frame link.  (for profiling,
145  * there's a cost-centre-stack in there too).
146  */
147
148 INFO_TABLE_RET( stg_bh_upd_frame, 
149             UPD_FRAME_WORDS, UPD_FRAME_BITMAP, UPDATE_FRAME,
150             stg_bh_upd_frame_0_ret,
151             stg_bh_upd_frame_1_ret,
152             stg_bh_upd_frame_2_ret,
153             stg_bh_upd_frame_3_ret,
154             stg_bh_upd_frame_4_ret,
155             stg_bh_upd_frame_5_ret,
156             stg_bh_upd_frame_6_ret,
157             stg_bh_upd_frame_7_ret
158             )
159 UPD_FRAME_ENTRY_TEMPLATE(,stg_IND_direct_info,%ENTRY_CODE(Sp(0)))
160
161 /*-----------------------------------------------------------------------------
162   Seq frames 
163
164   We don't have a primitive seq# operator: it is just a 'case'
165   expression whose scrutinee has either a polymorphic or function type
166   (constructor types can be handled by normal 'case' expressions).
167
168   To handle a polymorphic/function typed seq, we push a SEQ frame on
169   the stack.  This is a polymorphic activation record that just pops
170   itself and returns (in a non-vectored way) when entered.  The
171   purpose of the SEQ frame is to avoid having to make a polymorphic return
172   point for each polymorphic case expression.  
173
174   Another way of looking at it: the SEQ frame turns a vectored return
175   into a direct one.
176   -------------------------------------------------------------------------- */
177
178 #if MAX_VECTORED_RTN > 8
179 #error MAX_VECTORED_RTN has changed: please modify stg_seq_frame too.
180 #endif
181
182 INFO_TABLE_RET( stg_seq_frame, 0/* words */, 0/* bitmap */, RET_SMALL,
183         RET_LBL(stg_seq_frame), /* 0 */
184         RET_LBL(stg_seq_frame), /* 1 */
185         RET_LBL(stg_seq_frame), /* 2 */
186         RET_LBL(stg_seq_frame), /* 3 */
187         RET_LBL(stg_seq_frame), /* 4 */
188         RET_LBL(stg_seq_frame), /* 5 */
189         RET_LBL(stg_seq_frame), /* 6 */
190         RET_LBL(stg_seq_frame)  /* 7 */
191         )
192 {
193    Sp_adj(1);
194    jump %ENTRY_CODE(Sp(0));
195 }